---
id: product-ingestion
name: Product Ingestion
version: 1.0.0
summary: >-
  Catalog API persists product changes in PostgreSQL; Scheduler triggers Catalog
  Ingestion over Kafka every five minutes to refresh or remove Qdrant vectors and
  acknowledge unchanged revisions.
owners:
  - catalog-experience-team
badges:
  - content: Background Processing
    backgroundColor: blue
    textColor: blue
steps:
  - id: maintain-product
    title: Catalog administrator updates products
    actor:
      name: Catalog Administrator
    next_step:
      id: catalog-api
      label: Create, update, or soft-delete catalog entities
  - id: catalog-api
    title: Catalog API validates and saves changes
    summary: >-
      Saves validated book, author, category, and publisher changes without
      blocking API requests on AI embedding generation.
    service:
      id: catalog-api
      version: 1.0.0
    next_step:
      id: persist-and-enqueue
      label: Commit catalog changes
  - id: persist-and-enqueue
    title: Catalog Database - persist and queue revision
    summary: >-
      Database triggers atomically coalesce affected book IDs into the pending
      vector queue with a fresh revision token. Repeated edits replace the
      queued revision so only the latest state is indexed.
    container:
      id: catalog-database
      version: 1.0.0
    next_step:
      id: scheduler
      label: Queued work awaits scheduled batch trigger
  - id: scheduler
    title: Scheduler triggers periodic vector batch
    summary: >-
      Fires every five minutes in UTC independently of API traffic, disallowing
      concurrent job runs and recovering misfires.
    service:
      id: scheduler-service
      version: 1.0.0
    next_step:
      id: request-vector-batch
      label: Publish batch trigger event
  - id: request-vector-batch
    title: ProcessVectorBatchIntegrationEvent
    message:
      id: ProcessVectorBatchIntegrationEvent
      version: 1.0.0
    next_step:
      id: batch-channel
      label: Publish to scheduler.process-vector-batch
  - id: batch-channel
    title: Vector batch trigger channel
    channel:
      id: scheduler.process-vector-batch
      version: 1.0.0
    next_step:
      id: read-pending-batch
      label: Trigger Catalog Ingestion worker
  - id: read-pending-batch
    title: Catalog Database - acquire lock and read batch
    summary: >-
      Attempts an exclusive PostgreSQL advisory lock and reads up to 200
      queued revisions ordered by update time. Soft-deleted or missing books
      yield null content for vector removal.
    container:
      id: catalog-database
      version: 1.0.0
    next_steps:
      - id: process-batch
        label: Lock acquired and pending work exists
      - id: no-work
        label: Lock busy or queue empty
      - id: processing-failed
        label: Database operation fails
  - id: no-work
    title: No batch processed
    summary: >-
      Exits cleanly without modifying Qdrant and releases the advisory lock if
      held. A busy lock leaves pending work for the next scheduled run.
    custom:
      title: No work this invocation
      type: Outcome
      icon: CheckCircleIcon
      color: gray
  - id: process-batch
    title: Catalog Ingestion - generate embeddings
    summary: >-
      Ensures the Qdrant collection exists and generates embeddings for all
      active books in the batch. Incomplete embedding responses fail the batch
      before any vector writes occur.
    service:
      id: catalog-ingestion
      version: 1.0.0
    next_steps:
      - id: upsert-vector
        label: Active books with embeddings
      - id: delete-vector
        label: Missing or soft-deleted books
      - id: processing-failed
        label: Embedding generation fails
  - id: upsert-vector
    title: Catalog Vector - upsert active book vectors
    summary: >-
      Upserts searchable book text and embeddings keyed by book UUID into
      Qdrant.
    container:
      id: catalog-vector
      version: 1.0.0
    next_steps:
      - id: acknowledge-batch
        label: All vector operations succeed
      - id: processing-failed
        label: Vector upsert fails
  - id: delete-vector
    title: Catalog Vector - remove deleted book vectors
    summary: Deletes Qdrant records keyed by missing or soft-deleted book UUIDs.
    container:
      id: catalog-vector
      version: 1.0.0
    next_steps:
      - id: acknowledge-batch
        label: All vector operations succeed
      - id: processing-failed
        label: Vector deletion fails
  - id: acknowledge-batch
    title: Catalog Database - acknowledge unchanged revisions
    summary: >-
      Deletes only pending rows whose book ID and revision token still match
      the processed snapshot. Newer concurrent edits remain queued for the next
      cycle.
    container:
      id: catalog-database
      version: 1.0.0
    next_steps:
      - id: complete-batch
        label: Revision-checked acknowledgements complete
      - id: processing-failed
        label: Acknowledgement fails; rows survive
  - id: complete-batch
    title: Catalog Ingestion - complete batch
    summary: >-
      Releases the advisory lock. Concurrent edits and queues exceeding 200
      items await the next scheduled batch.
    service:
      id: catalog-ingestion
      version: 1.0.0
  - id: processing-failed
    title: Preserve pending work after failure
    summary: >-
      Releases the advisory lock and leaves unacknowledged rows in PostgreSQL so
      the next five-minute trigger safely retries the batch.
    custom:
      title: Processing failed
      type: Failure
      icon: ExclamationTriangleIcon
      color: red
    next_step:
      id: scheduler
      label: Retry on next scheduled invocation
---

## Trigger & Business Outcome

This workflow begins when a catalog administrator creates, updates, or soft-deletes a [[entity|book]], [[entity|author]], [[entity|category]], or [[entity|publisher]] through [[service|catalog-api]]. Success is confirmed when [[service|catalog-ingestion]] refreshes or removes the corresponding embeddings in [[container|catalog-vector]] and acknowledges only unchanged revision rows in [[container|catalog-database]], keeping semantic search eventually consistent without slowing catalog writes.

## Interactive Flow Visualization

<NodeGraph />

## Step-by-Step Walkthrough

<Steps title="Decoupled Catalog Mutation and Vector Indexing">
	<Step title="Persist catalog changes and coalesce pending work">
		[[service|catalog-api]] commits validated catalog changes to [[container|catalog-database]]. In
		the same transaction, database triggers atomically coalesce each affected book ID into the
		pending vector queue with a new revision token, collapsing rapid edits into one indexing pass.
	</Step>
	<Step title="Emit periodic batch trigger">
		Every five minutes in UTC, [[service|scheduler-service]] publishes
		[[event|ProcessVectorBatchIntegrationEvent]] via its transactional outbox to
		[[channel|scheduler.process-vector-batch]].
	</Step>
	<Step title="Acquire exclusive lock and read bounded batch">
		[[service|catalog-ingestion]] acquires an exclusive PostgreSQL advisory lock so only one worker
		processes the queue at a time, then reads up to 200 queued revisions with current book metadata.
	</Step>
	<Step title="Generate embeddings and update Qdrant">
		[[service|catalog-ingestion]] generates batch embeddings for active books, upserts active
		records into [[container|catalog-vector]], and deletes vectors for missing or soft-deleted
		books.
	</Step>
	<Step title="Acknowledge unchanged revisions">
		Only after all vector operations succeed, [[service|catalog-ingestion]] deletes pending rows
		whose book ID and revision token match the processed snapshot, preserving any concurrent edits
		made during processing.
	</Step>
</Steps>

## Failure, Retry & Compensation Behavior

| Failure Point                               | System Behavior                                                                                                        | Compensation / Recovery                                                                                                                                        |
| :------------------------------------------ | :--------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Advisory lock already held**              | Worker exits cleanly without touching <ResourceLink id="catalog-vector" type="container">catalog-vector</ResourceLink> | Pending rows remain intact for the next five-minute trigger                                                                                                    |
| **Incomplete AI embedding response**        | Batch aborts before any vector writes                                                                                  | Advisory lock is released; entire batch is retried on the next scheduled run                                                                                   |
| **Vector store or acknowledgement failure** | Error propagates and lock is released                                                                                  | Unacknowledged rows survive in <ResourceLink id="catalog-database" type="container">catalog-database</ResourceLink>; vector writes are idempotent by book UUID |

:::note
**Eventual consistency window:** Semantic search results lag behind catalog writes by up to five minutes (plus additional cycles if the queue exceeds 200 books). Authoritative book details and prices are always hydrated from [[container|catalog-database]] at query time.
:::

## Participating Resources

- **Domains & Systems:** [[domain|catalog]], [[domain|platform]], [[system|product-catalog-system]], [[system|scheduler-system]]
- **Services:** [[service|catalog-api]], [[service|scheduler-service]], [[service|catalog-ingestion]]
- **Messages & Channels:** [[event|ProcessVectorBatchIntegrationEvent]], [[channel|scheduler.process-vector-batch]]
- **Data Stores:** [[container|catalog-database]], [[container|catalog-vector]]
