---
title: Feedback ownership, concurrency, and compensation
summary: Domain invariants, Cosmos DB concurrency, caching, and cross-domain compensation rules for Rating API.
version: 1.0.0
type: engineering
order: 1
badges:
  - content: Architecture
    backgroundColor: green
    textColor: green
---

## Purpose & Scope

This document defines the data ownership, optimistic concurrency, caching, and cross-domain compensation boundaries for [[service|rating-api]] and [[agent|rating-agent]] in the [[domain|reviews]] subdomain. Use it when modifying feedback mutations, Azure Cosmos DB persistence, Kafka rating events, or read-only AI summarization.

## Visual Reference

The visual below illustrates how [[service|rating-api]] persists feedback in [[container|rating-database]], caches reads in [[container|rating-cache]], exchanges rating events with [[service|catalog-api]], and serves read-only summarization.

<MermaidFileLoader file="mermaids/feedback-contracts-and-integration-diagram-1.mmd" />

_Feedback Persistence, Catalog Projection, and Summarization Boundaries_

## Core Invariants & Design Rules

- **Feedback system of record:** [[service|rating-api]] owns individual [[entity|feedback]] documents in [[container|rating-database]] (Azure Cosmos DB, partitioned by `/bookId`); it uses no relational database. [[service|catalog-api]] holds only a derived `average_rating` and `total_reviews` projection on [[entity|book]].
- **Rating & PII constraints:** Every feedback rating must be an integer between `0` and `5` inclusive (enforced at both request validation and domain construction). Reviewer names are tagged as PII for automatic log redaction.
- **Post-commit event publication:** [[event|FeedbackCreatedIntegrationEvent]] and [[event|FeedbackDeletedIntegrationEvent]] are published only after the Cosmos DB write commits, never speculatively before persistence.
- **Compensation without re-emission:** When [[service|catalog-api]] rejects a feedback event for a missing book via [[event|BookUpdatedRatingFailedIntegrationEvent]], [[service|rating-api]] reverts and deletes the orphaned feedback without emitting [[event|FeedbackDeletedIntegrationEvent]] (preventing a compensation loop).
- **Read-only AI summarization:** [[agent|rating-agent]] reads catalog context via [[service|mcp-server]] and raw feedback via [[service|rating-api]], runs behind Presidio PII middleware, and produces derived insights only—never mutating [[container|rating-database]] or [[container|catalog-database]].

:::warning
**Never emit `FeedbackDeletedIntegrationEvent` during compensation.** Deleting an orphaned feedback in response to [[event|BookUpdatedRatingFailedIntegrationEvent]] must use the revert path rather than the standard delete path; otherwise [[service|catalog-api]] would receive a spurious decrement for a book it already rejected.
:::

## Boundaries & Guarantees

| Concern                       | Strategy & Guarantee                                                                                                                                                                                                                                                                                                                                                                                                     | Failure / Edge-Case Behavior                                                                                                                                                                                                                                  | Related Resources                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| :---------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Persistence**               | <ResourceLink id="rating-database" type="container">rating-database</ResourceLink> stores <ResourceLink id="feedback" type="entity">feedback</ResourceLink> documents partitioned by `/bookId` with Wolverine Cosmos DB persistence for durable messaging.                                                                                                                                                               | Missing feedback on `DELETE` returns `404 Not Found` (never masked as `204`); compensation delete for an already-absent record completes as a no-op.                                                                                                          | <ResourceLink id="rating-database" type="container">rating-database</ResourceLink>, <ResourceLink id="feedback" type="entity">feedback</ResourceLink>, <ResourceLink id="adr-001-postgresql-feedback-system-of-record" type="adr">adr-001-postgresql-feedback-system-of-record</ResourceLink>                                                                                                                                                                                                                                                            |
| **Caching**                   | <ResourceLink id="rating-cache" type="container">rating-cache</ResourceLink> caches paged feedback reads (`ListFeedbacks`) via FusionCache with a `10m` TTL tagged by `BookId`.                                                                                                                                                                                                                                          | Feedback creation, deletion, or compensation invalidates the affected `BookId` tag so subsequent reads reflect the updated list.                                                                                                                              | <ResourceLink id="rating-cache" type="container">rating-cache</ResourceLink>, <ResourceLink id="ListFeedbacks" type="query">ListFeedbacks</ResourceLink>                                                                                                                                                                                                                                                                                                                                                                                                 |
| **Messaging**                 | Emits <ResourceLink id="FeedbackCreatedIntegrationEvent" type="event">FeedbackCreatedIntegrationEvent</ResourceLink> and <ResourceLink id="FeedbackDeletedIntegrationEvent" type="event">FeedbackDeletedIntegrationEvent</ResourceLink> partitioned by `BookId` / `FeedbackId`; consumes <ResourceLink id="BookUpdatedRatingFailedIntegrationEvent" type="event">BookUpdatedRatingFailedIntegrationEvent</ResourceLink>. | Exhausted retries route to <ResourceLink id="bookworm.{env}.rating.dead-letter-queue" type="channel">`bookworm.{env}.rating.dead-letter-queue`</ResourceLink>; missing-book compensation deletes the orphaned feedback cleanly.                               | <ResourceLink id="bookworm.{env}.rating.feedback-create" type="channel">`bookworm.{env}.rating.feedback-create`</ResourceLink>, <ResourceLink id="bookworm.{env}.rating.feedback-delete" type="channel">`bookworm.{env}.rating.feedback-delete`</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.dead-letter-queue" type="channel">`bookworm.{env}.rating.dead-letter-queue`</ResourceLink> |
| **Concurrency & Idempotency** | Cosmos DB ETag optimistic concurrency prevents lost updates on <ResourceLink id="feedback" type="entity">feedback</ResourceLink> documents; inbound compensation events use a `24h` Redis integration-event idempotency guard; HTTP routes enforce `PerUserRateLimit`.                                                                                                                                                   | Concurrent ETag conflicts fail fast rather than overwriting newer state; duplicate <ResourceLink id="BookUpdatedRatingFailedIntegrationEvent" type="event">BookUpdatedRatingFailedIntegrationEvent</ResourceLink> deliveries within `24h` are safely ignored. | <ResourceLink id="rating-api" type="service">rating-api</ResourceLink>, <ResourceLink id="feedback-publication" type="flow">feedback-publication</ResourceLink>, <ResourceLink id="adr-003-compensate-invalid-feedback-after-catalog-rejection" type="adr">adr-003-compensate-invalid-feedback-after-catalog-rejection</ResourceLink>                                                                                                                                                                                                                    |

<AccordionGroup>
	<Accordion title="Authorization & A2A gRPC boundaries">
		`POST /api/v1/feedbacks` and `GET /api/v1/feedbacks` are rate-limited (`PerUserRateLimit`).
		`DELETE /api/v1/feedbacks/{id}` requires the `admin` policy (`admin` role +
		`rating_read`/`rating_write` scopes). `GET /api/v1/feedbacks/{id}/summarize` requires an
		authenticated caller, while Agent-to-Agent (A2A) gRPC calls from [[service|chat-api]] require
		the `read` policy (`rating_read` scope) via Entra Agent Identity token exchange.
	</Accordion>
	<Accordion title="Why summarization is non-authoritative">
		[[query|SummarizeFeedback]] synthesizes themes, sentiment, and confidence from current reviews
		at read time. Its output is never persisted back to [[container|rating-database]] and never
		overrides the deterministic `average_rating` maintained by [[service|catalog-api]].
	</Accordion>
</AccordionGroup>

## Safe Change & Verification Checklist

- Preserve the `/bookId` partition key and ETag optimistic concurrency configuration on [[container|rating-database]] (`feedbacks` container).
- Verify [[event|FeedbackCreatedIntegrationEvent]] and [[event|FeedbackDeletedIntegrationEvent]] emit only post-commit, and that compensation revert never emits [[event|FeedbackDeletedIntegrationEvent]].
- Confirm `BookId` cache-tag invalidation in [[container|rating-cache]] on create, delete, and compensation paths.
- Keep `0`–`5` rating validation, PII log redaction, and `admin` / `read` authorization policies intact.
- Run `mise run format`, `mise run build`, and `mise run test` to verify Rating unit tests and Pact contracts (`Rating-Catalog`, `Catalog-Rating`).

## Related Resources

- [[service|rating-api]] — Authoritative Reviews API and compensation consumer
- [[agent|rating-agent]] — Read-only review summarization agent
- [[command|CreateFeedback]], [[command|DeleteFeedback]], [[query|ListFeedbacks]], [[query|SummarizeFeedback]] — Feedback operations
- [[flow|feedback-publication]] — End-to-end feedback publication and compensation flow
- [[adr|adr-002-publish-feedback-events-for-catalog-projection]], [[adr|adr-003-compensate-invalid-feedback-after-catalog-rejection]], [[adr|adr-004-read-only-governed-feedback-summarization]]
- [[doc|platform/006-api-and-contracts]] — Consolidated HTTP, gRPC, and Kafka contracts
