---
id: feedback-publication
name: Customer Feedback Publication and Projection
version: 1.0.0
summary: >-
  Rating API persists customer feedback in Cosmos DB and publishes an
  integration event; Catalog API updates the derived book rating summary or
  emits a compensating failure event to delete orphaned feedback.
owners:
  - catalog-experience-team
badges:
  - content: Event-Driven
    backgroundColor: purple
    textColor: purple
steps:
  - id: customer
    title: Customer submits feedback for a book
    actor:
      name: Customer
    next_step:
      id: create-feedback
      label: Submit rating and optional comment
  - id: create-feedback
    title: CreateFeedback command
    message:
      id: CreateFeedback
      version: 1.0.0
    next_step:
      id: rating-api
      label: Validate and persist feedback
  - id: rating-api
    title: Rating API persists new feedback
    summary: >-
      Validates the book ID, score, reviewer name, and comment, then commits the
      authoritative feedback record to Cosmos DB.
    service:
      id: rating-api
      version: 1.0.0
    next_step:
      id: rating-db
      label: Save feedback document
  - id: rating-db
    title: Rating Database stores committed feedback
    summary: >-
      Stores the authoritative Feedback document partitioned by book ID and
      atomically queues the integration event.
    container:
      id: rating-database
      version: 1.0.0
    next_step:
      id: feedback-created
      label: Publish feedback-created event
  - id: feedback-created
    title: FeedbackCreatedIntegrationEvent
    message:
      id: FeedbackCreatedIntegrationEvent
      version: 1.0.0
    next_step:
      id: feedback-create-channel
      label: Publish to rating.feedback-create
  - id: feedback-create-channel
    title: Feedback created channel
    channel:
      id: bookworm.{env}.rating.feedback-create
      version: 1.0.0
    next_step:
      id: catalog-api
      label: Deliver to Catalog API
  - id: catalog-api
    title: Catalog API updates derived rating summary
    summary: >-
      Applies a 24-hour Redis idempotency guard, looks up the book in Catalog
      Database, and updates the derived average rating and review count.
    service:
      id: catalog-api
      version: 1.0.0
    next_steps:
      - id: catalog-db
        label: Active book found; update summary
      - id: feedback-compensation
        label: Missing book; trigger compensation
  - id: catalog-db
    title: Catalog Database stores updated rating summary
    summary: >-
      Persists the recalculated average rating and total review count on the
      book record for storefront discovery.
    container:
      id: catalog-database
      version: 1.0.0
  - id: feedback-compensation
    title: BookUpdatedRatingFailedIntegrationEvent
    message:
      id: BookUpdatedRatingFailedIntegrationEvent
      version: 1.0.0
    next_step:
      id: compensation-channel
      label: Publish to catalog.book-update-rating-failed
  - id: compensation-channel
    title: Rating update failed channel
    channel:
      id: bookworm.{env}.catalog.book-update-rating-failed
      version: 1.0.0
    next_step:
      id: rating-api-cleanup
      label: Deliver compensation to Rating API
  - id: rating-api-cleanup
    title: Rating API deletes orphaned feedback
    summary: >-
      Deletes the invalid feedback document from Cosmos DB so Reviews and
      Catalog remain consistent without leaving orphaned data.
    service:
      id: rating-api
      version: 1.0.0
---

## Trigger & Business Outcome

This flow starts when a customer submits [[command|CreateFeedback]] for a book through [[service|rating-api]]. Success is confirmed when [[service|rating-api]] persists the authoritative [[entity|feedback]] document in [[container|rating-database]] and [[service|catalog-api]] asynchronously updates the book's derived average rating and review count in [[container|catalog-database]]—or, if the book does not exist, compensates by deleting the orphaned feedback.

## Interactive Flow Visualization

<NodeGraph />

## Step-by-Step Walkthrough

<Steps title="Optimistic Feedback Persistence and Asynchronous Rating Projection">
	<Step title="Persist authoritative feedback in Reviews">
		[[service|rating-api]] validates the rating score, reviewer details, and comment, commits the
		[[entity|feedback]] document to [[container|rating-database]], and publishes
		[[event|FeedbackCreatedIntegrationEvent]] to{" "}
		<ResourceLink id="bookworm.{env}.rating.feedback-create" type="channel">
			`bookworm.{env}.rating.feedback-create`
		</ResourceLink>
		.
	</Step>
	<Step title="Update derived rating summary in Catalog">
		[[service|catalog-api]] consumes [[event|FeedbackCreatedIntegrationEvent]] behind a 24-hour
		Redis idempotency guard, loads the target [[entity|book]] from [[container|catalog-database]],
		and updates its derived average rating and total review count without storing review text or
		reviewer PII.
	</Step>
	<Step title="Compensate on missing book reference">
		If [[service|catalog-api]] finds no active book for the referenced ID, it emits
		[[event|BookUpdatedRatingFailedIntegrationEvent]] on{" "}
		<ResourceLink id="bookworm.{env}.catalog.book-update-rating-failed" type="channel">
			`bookworm.{env}.catalog.book-update-rating-failed`
		</ResourceLink>
		. [[service|rating-api]] consumes this compensating event and deletes the orphaned
		[[entity|feedback]] record from [[container|rating-database]].
	</Step>
</Steps>

## Failure, Retry & Compensation Behavior

| Failure Point                        | System Behavior                                                                                                                                                                                               | Compensation / Recovery                                                                                                                                                                                                                                      |
| :----------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Duplicate event delivery**         | Redis idempotency guard deduplicates by event ID for 24 hours                                                                                                                                                 | Prevents double-counting ratings during Kafka redeliveries or transient retries                                                                                                                                                                              |
| **Referenced book does not exist**   | <ResourceLink id="catalog-api" type="service">catalog-api</ResourceLink> emits <ResourceLink id="BookUpdatedRatingFailedIntegrationEvent" type="event">BookUpdatedRatingFailedIntegrationEvent</ResourceLink> | <ResourceLink id="rating-api" type="service">rating-api</ResourceLink> deletes the orphaned <ResourceLink id="feedback" type="entity">feedback</ResourceLink> record from <ResourceLink id="rating-database" type="container">rating-database</ResourceLink> |
| **Transient Catalog database error** | Worker retries with exponential backoff                                                                                                                                                                       | Exhausted failures route to <ResourceLink id="bookworm.{env}.catalog.dead-letter-queue" type="channel">`bookworm.{env}.catalog.dead-letter-queue`</ResourceLink> for replay                                                                                  |

:::note
**Feedback deletion follows a separate one-way contract:** When a review is deleted, [[service|rating-api]] publishes [[event|FeedbackDeletedIntegrationEvent]] on <ResourceLink id="bookworm.{env}.rating.feedback-delete" type="channel">`bookworm.{env}.rating.feedback-delete`</ResourceLink>, and [[service|catalog-api]] decrements the derived rating summary (treating a missing book as a safe no-op).
:::

## Participating Resources

- **Domains & Systems:** [[domain|reviews]], [[domain|catalog]], [[system|rating-system]], [[system|product-catalog-system]]
- **Services:** [[service|rating-api]], [[service|catalog-api]]
- **Messages:** [[command|CreateFeedback]], [[event|FeedbackCreatedIntegrationEvent]], [[event|BookUpdatedRatingFailedIntegrationEvent]], [[event|FeedbackDeletedIntegrationEvent]]
- **Channels & Stores:** <ResourceLink id="bookworm.{env}.rating.feedback-create" type="channel">`bookworm.{env}.rating.feedback-create`</ResourceLink>, <ResourceLink id="bookworm.{env}.catalog.book-update-rating-failed" type="channel">`bookworm.{env}.catalog.book-update-rating-failed`</ResourceLink>, <ResourceLink id="bookworm.{env}.rating.feedback-delete" type="channel">`bookworm.{env}.rating.feedback-delete`</ResourceLink>, <ResourceLink id="bookworm.{env}.catalog.dead-letter-queue" type="channel">`bookworm.{env}.catalog.dead-letter-queue`</ResourceLink>, [[container|rating-database]], [[container|catalog-database]]
- **Related Views:** [[diagram|rating-summarizer-workflow]]
