---
title: Order consistency and data ownership
summary: Authoritative stores, checkout price snapshotting, Redis coordination, and Ordering vs. Finance boundaries.
version: 1.0.0
type: engineering
order: 1
badges:
  - content: Architecture
    backgroundColor: green
    textColor: green
---

## Purpose & Scope

This document defines data ownership, transactional outbox guarantees, checkout snapshotting, and Redis coordination rules for [[service|order-api]] and [[service|order-orchestration]] in the [[domain|ordering]] subdomain. Use it when modifying order creation, Marten projections, Redis locks/idempotency, or cross-service checkout orchestration.

## Visual Reference

The visual below illustrates how [[service|order-api]] and [[service|order-orchestration]] isolate backing stores while coordinating via gRPC queries and Kafka messages.

<MermaidFileLoader file="mermaids/01-order-consistency-and-data-ownership-diagram-1.mmd" />

_Ordering and Finance Data Ownership and Messaging Boundaries_

## Core Invariants & Design Rules

- **Authoritative order state:** [[container|order-database]] (PostgreSQL) is the sole system of record for [[entity|buyer]], [[entity|order]], and [[entity|order-item]] aggregates, Marten audit event streams (`DaemonLockId = 44444`), and Wolverine outbox/inbox tables. Marten projections (`OrderSummary`, `OrderSummaryView`) are derived views and never replace the `Order` aggregate.
- **Deterministic checkout snapshot:** During `POST /api/v1/orders`, [[service|order-api]] reads the customer's basket via [[query|GetBasketGrpc]], resolves every distinct `BookId` via [[query|GetBooksGrpc]] (aborting with `404 Not Found` if any book is missing), locks in `PriceSale ?? Price` per [[entity|order-item]], and persists the sum as the immutable order total. Subsequent [[service|catalog-api]] or [[service|basket-api]] changes never rewrite accepted orders.
- **Strict status vs. orchestration separation:** Only explicit HTTP transitions (`POST /api/v1/orders`, `PATCH .../complete`, `PATCH .../cancel`) mutate an order's `Status` (`New` → `Completed` or `New` → `Cancelled`). [[service|order-orchestration]] maintains an independent saga state in Azure Durable Task Scheduler (`finance-taskhub`) and never calls [[service|order-api]] to cancel an order.
- **Soft delete vs. cancellation:** [[command|DeleteBasketFailedCommand]] triggers soft deletion (`IsDeleted = true`) to hide the order from standard queries without setting `Status` to `Cancelled`. [[command|DeleteBasketCompleteCommand]] is logged and leaves `Status` as `New`.
- **Bounded saga retries:** When [[service|order-orchestration]] exhausts its bounded retry budget (`MaxAttempts: 3` at `30m` timer intervals), it transitions its own orchestration state to `Cancelled` and emits [[command|CancelOrderCommand]] to [[service|notification-api]].

:::warning
**Never flush Redis broadly during incidents.** [[container|order-cache]] holds active HTTP idempotency markers (`{METHOD}:{PATH}:{x-request-id}`, `3600s` TTL) and buyer creation locks (`order-create:{userId}`, `60s` TTL) alongside FusionCache read entries. Broadly flushing Redis exposes concurrent checkouts to duplicate order creation.
:::

## Boundaries & Guarantees

| Concern                       | Strategy & Guarantee                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | Failure / Edge-Case Behavior                                                                                                                                                                                                                                                                            | Related Resources                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| :---------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Persistence**               | <ResourceLink id="order-database" type="container">order-database</ResourceLink> commits `Order` aggregates, Marten event streams, and Wolverine outbox messages in a single ACID transaction; <ResourceLink id="order-orchestration" type="service">order-orchestration</ResourceLink> persists saga state in `finance-taskhub` (no PostgreSQL).                                                                                                                                                                                                                                                                                                                                                                                 | Uncommitted writes never publish Kafka events; Marten async projections recover automatically from the authoritative event stream.                                                                                                                                                                      | <ResourceLink id="order-database" type="container">order-database</ResourceLink>, <ResourceLink id="order" type="entity">order</ResourceLink>, <ResourceLink id="order-item" type="entity">order-item</ResourceLink>, <ResourceLink id="adr-001-postgresql-order-system-of-record" type="adr">adr-001-postgresql-order-system-of-record</ResourceLink>, <ResourceLink id="adr-001-finance-persisted-order-saga" type="adr">adr-001-finance-persisted-order-saga</ResourceLink>                                                                                           |
| **Caching**                   | <ResourceLink id="order-cache" type="container">order-cache</ResourceLink> provides FusionCache L1/L2 (`2h` fail-safe) for Catalog lookups and dashboard reads.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | Safe to invalidate individual cache keys without losing order or buyer state.                                                                                                                                                                                                                           | <ResourceLink id="order-cache" type="container">order-cache</ResourceLink>, <ResourceLink id="adr-002-preserve-checkout-purchase-snapshot" type="adr">adr-002-preserve-checkout-purchase-snapshot</ResourceLink>                                                                                                                                                                                                                                                                                                                                                         |
| **Messaging**                 | <ResourceLink id="order-api" type="service">order-api</ResourceLink> emits <ResourceLink id="UserCheckedOutIntegrationEvent" type="event">UserCheckedOutIntegrationEvent</ResourceLink>, <ResourceLink id="OrderStatusChangedToCompleteIntegrationEvent" type="event">OrderStatusChangedToCompleteIntegrationEvent</ResourceLink>, and <ResourceLink id="OrderStatusChangedToCancelIntegrationEvent" type="event">OrderStatusChangedToCancelIntegrationEvent</ResourceLink> partitioned by `OrderId`; consumes <ResourceLink id="DeleteBasketCompleteCommand" type="command">DeleteBasketCompleteCommand</ResourceLink> and <ResourceLink id="DeleteBasketFailedCommand" type="command">DeleteBasketFailedCommand</ResourceLink>. | Exhausted retries route to <ResourceLink id="bookworm.{env}.ordering.dead-letter-queue" type="channel">`bookworm.{env}.ordering.dead-letter-queue`</ResourceLink>; <ResourceLink id="notification-api" type="service">notification-api</ResourceLink> delivery never proves authoritative order status. | <ResourceLink id="bookworm.{env}.ordering.user-checked-out" type="channel">`bookworm.{env}.ordering.user-checked-out`</ResourceLink>, <ResourceLink id="bookworm.{env}.finance.delete-basket-complete" type="channel">`bookworm.{env}.finance.delete-basket-complete`</ResourceLink>, <ResourceLink id="bookworm.{env}.finance.delete-basket-failed" type="channel">`bookworm.{env}.finance.delete-basket-failed`</ResourceLink>, <ResourceLink id="bookworm.{env}.ordering.dead-letter-queue" type="channel">`bookworm.{env}.ordering.dead-letter-queue`</ResourceLink> |
| **Concurrency & Idempotency** | `POST`/`PATCH` endpoints require `x-request-id` (`{METHOD}:{PATH}:{x-request-id}`, `3600s` TTL, returns `409 Conflict` on duplicate, cleared on exception or `>= 500`); `order-create:{userId}` (`60s` TTL) serializes buyer writes after gRPC lookups; inbound Kafka handlers use a `24h` Redis idempotency guard.                                                                                                                                                                                                                                                                                                                                                                                                               | Concurrent checkout attempts for the same buyer fail fast with conflict/lock error; duplicate `x-request-id` does **not** replay a cached response body.                                                                                                                                                | <ResourceLink id="CreateOrder" type="command">CreateOrder</ResourceLink>, <ResourceLink id="user-checkout" type="flow">user-checkout</ResourceLink>, <ResourceLink id="adr-003-separate-order-status-from-finance-saga" type="adr">adr-003-separate-order-status-from-finance-saga</ResourceLink>, <ResourceLink id="adr-005-finance-separate-workflow-and-order-status" type="adr">adr-005-finance-separate-workflow-and-order-status</ResourceLink>                                                                                                                    |

## Safe Change & Verification Checklist

- Keep [[entity|order-item]] purchase prices (`PriceSale ?? Price`) immutable after checkout; allow read-time Catalog enrichment for display titles only.
- Preserve `x-request-id` idempotency, `order-create:{userId}` buyer write locking, and `24h` integration-event idempotency guards.
- Keep `Order.Status` transitions (`New` → `Completed` | `Cancelled`), soft deletion (`IsDeleted`), and `finance-taskhub` orchestration states strictly decoupled.
- Verify Pact contracts (`Ordering-Finance`, `Finance-Ordering`) and run `mise run format`, `mise run build`, and `mise run test`.

## Related Resources

- [[service|order-api]] — Authoritative Ordering REST API and Marten/Wolverine host
- [[service|order-orchestration]] — Durable Task Scheduler post-checkout saga
- [[flow|order-lifecycle]], [[flow|user-checkout]] — End-to-end checkout and lifecycle flows
- [[adr|adr-001-postgresql-order-system-of-record]], [[adr|adr-002-preserve-checkout-purchase-snapshot]], [[adr|adr-003-separate-order-status-from-finance-saga]], [[adr|adr-004-finance-bounded-scheduled-retries]]
- [[doc|platform/006-api-and-contracts]] — Consolidated HTTP, gRPC, and Kafka contracts
