---
title: Rating summary mismatch
summary: Investigate when a book's average rating or review count disagrees with Reviews.
version: 1.0.0
type: runbooks
order: 4
badges:
  - content: Runbook
    backgroundColor: orange
    textColor: orange
---

## Trigger / Symptom & Blast Radius

A book's `average_rating` or `total_reviews` in [[service|catalog-api]] disagrees with [[service|rating-api]], or [[event|BookUpdatedRatingFailedIntegrationEvent]] appears on <ResourceLink id="bookworm.{env}.catalog.book-update-rating-failed" type="channel">`bookworm.{env}.catalog.book-update-rating-failed`</ResourceLink>. [[service|rating-api]] owns authoritative [[entity|feedback]] documents in [[container|rating-database]], while [[service|catalog-api]] maintains only a derived summary in [[container|catalog-database]].

<Admonition type="danger">
	Never invent or hand-edit `average_rating` or `total_reviews` in [[container|catalog-database]]
	without deriving the exact aggregate from [[container|rating-database]]. Replaying already-applied
	rating events after the 24-hour idempotency window expires will double-count ratings.
</Admonition>

## Pre-Flight Check

Compare Catalog's derived summary in [[container|catalog-database]] against the authoritative aggregate in [[container|rating-database]] (`ratingdb` / `feedbacks`):

```sql
SELECT id, name, average_rating, total_reviews, is_deleted FROM books WHERE id = '<BOOK_ID>';
```

```sql
SELECT COUNT(1) AS total_reviews, AVG(c.rating) AS average_rating FROM c WHERE c.bookId = '<BOOK_ID>';
```

## Quick Recovery Procedure

<Steps>
<Step title="Check for missing-book compensation events">

Inspect <ResourceLink id="bookworm.{env}.catalog.book-update-rating-failed" type="channel">`bookworm.{env}.catalog.book-update-rating-failed`</ResourceLink> for [[event|BookUpdatedRatingFailedIntegrationEvent]]. If present, [[service|catalog-api]] could not find `<BOOK_ID>` and emitted the compensation event so [[service|rating-api]] deletes the orphaned feedback document.

</Step>
<Step title="Inspect Kafka consumer lag and dead-letter queues">

Check consumer lag and [[service|catalog-api]] logs across <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>, and <ResourceLink id="bookworm.{env}.catalog.dead-letter-queue" type="channel">`bookworm.{env}.catalog.dead-letter-queue`</ResourceLink>:

```bash
az containerapp logs show --name <SERVICE_NAME> --resource-group <RESOURCE_GROUP> --tail 150
```

</Step>
<Step title="Recover without double-counting">

**Unprocessed DLQ messages:** Fix the underlying database or handler fault first, then replay only unapplied messages from <ResourceLink id="bookworm.{env}.catalog.dead-letter-queue" type="channel">`bookworm.{env}.catalog.dead-letter-queue`</ResourceLink>.

**Confirmed multi-event delta drift:** Execute an approved reconciliation setting `average_rating` and `total_reviews` directly from the authoritative [[container|rating-database]] aggregate, then evict `book:<BOOK_ID>` from [[container|catalog-cache]].

</Step>
</Steps>

## Verification

Query [[service|catalog-api]] and confirm `averageRating` and `totalReviews` match [[container|rating-database]]:

```bash
curl -sS "https://<GATEWAY_HOST>/catalog/api/v1/books/<BOOK_ID>" | jq '{id, averageRating, totalReviews}'
```

Expected output:

```json
{
	"id": "<BOOK_ID>",
	"averageRating": 4.5,
	"totalReviews": 12
}
```

## Rollback / Escalation

- **Rollback:** Re-run the authoritative reconciliation query from [[container|rating-database]] and evict `book:<BOOK_ID>` in [[container|catalog-cache]] if a manual correction used wrong values.
- **Escalation:** Escalate persistent projection or compensation failures to [[team|catalog-experience-team]] with `<BOOK_ID>` and event IDs.

<AccordionGroup>
<Accordion title="Troubleshooting edge cases">

| Symptom                                                                    | Cause                                                     | Fix                                                                                 |
| :------------------------------------------------------------------------- | :-------------------------------------------------------- | :---------------------------------------------------------------------------------- |
| [[container\|catalog-database]] matches Cosmos DB, API returns old numbers | Stale entry in [[container\|catalog-cache]]               | Evict `book:<BOOK_ID>` using the stale cache runbook                                |
| `total_reviews` higher than Cosmos DB count                                | Duplicate event replayed after 24-hour idempotency expiry | Reconcile directly from [[container\|rating-database]] and evict cache              |
| Messages accumulating in Catalog DLQ                                       | Transient DB or handler error exhausted retries           | Fix dependency in [[service\|catalog-api]] logs, then replay unapplied DLQ messages |

</Accordion>
</AccordionGroup>

## Related documentation

- [[flow|feedback-publication]] — End-to-end review projection and compensation flow.
- [[adr|adr-006-reviews-rating-projection-on-book]] — Derived rating summary boundary.
