---
id: order-database
name: Order Database
version: 1.0.0
summary: Authoritative PostgreSQL system of record owned by the Ordering bounded context for buyers, accepted orders, purchase-time line items, and order lifecycle event streams.
owners:
  - commerce-fulfillment-team
container_type: database
badges:
  - content: Database
    backgroundColor: blue
    textColor: blue
technology: postgresql
authoritative: true
access_mode: readWrite
classification: confidential
retention: indefinite
residency: eastasia
purpose: Persist accepted purchases and Buyer profiles alongside order lifecycle event streams, projections, and transactional outbox storage.
styles:
  icon: /icons/database/postgresql.svg
---

## Purpose & Ownership Boundary

Order Database (`orderingdb`) is the authoritative PostgreSQL system of record owned exclusively by [[service|order-api]] in the Ordering bounded context. PostgreSQL was chosen to combine ACID relational persistence for buyer profiles and immutable checkout snapshots with append-only order lifecycle event streams, read projections, and a transactional outbox over a single connection.

## Data Store Profile

| Property                            | Value                                                                             |
| :---------------------------------- | :-------------------------------------------------------------------------------- |
| **Bounded Context / Owner**         | Ordering — [[team\|commerce-fulfillment-team]]                                    |
| **Engine / Technology**             | PostgreSQL 18 (Azure Database for PostgreSQL Flexible Server)                     |
| **Store Type**                      | Database                                                                          |
| **Primary Writer(s)**               | [[service\|order-api]]                                                            |
| **Primary Reader(s)**               | [[service\|order-api]]                                                            |
| **Authoritative / Source of Truth** | Yes                                                                               |
| **Data Classification**             | Confidential (contains customer names and shipping addresses)                     |
| **Retention / TTL Policy**          | Indefinite (soft-delete on orders; subject to environment data-governance policy) |

## Logical Data Layout & Key Patterns

| Table / Schema         | Owned Entity           | Partition / Primary Key                           | Purpose & Concurrency                                                                                                                                                                                                                                                         |
| :--------------------- | :--------------------- | :------------------------------------------------ | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `buyers`               | [[entity\|buyer]]      | `id` (`uuidv7()`)                                 | Customer profiles including name and shipping address (`street`, `city`, `province`).                                                                                                                                                                                         |
| `orders`               | [[entity\|order]]      | `id` (`uuidv7()`)                                 | Accepted orders with status (`New`, `Cancelled`, `Completed`), order note, timestamps, and soft-delete flag; optimistic concurrency via row-version token.                                                                                                                    |
| `order_items`          | [[entity\|order-item]] | `id` (`uuidv7()`), unique (`book_id`, `order_id`) | Immutable checkout-time line item snapshots locking `book_id`, quantity, and unit price at purchase time.                                                                                                                                                                     |
| Event store (`public`) | [[entity\|order]]      | Stream ID (`order_id`)                            | Append-only order placement, completion, and cancellation events; live stream aggregation and asynchronous summary projection (`DaemonLockId = 44444`).                                                                                                                       |
| `wolverine` schema     | —                      | Envelope ID                                       | Transactional outbox and inbox tables sharing the domain write transaction; partitions Kafka events ([[event\|UserCheckedOutIntegrationEvent]], [[event\|OrderStatusChangedToCompleteIntegrationEvent]], [[event\|OrderStatusChangedToCancelIntegrationEvent]]) by `OrderId`. |

## Consistency, Isolation & Security Guardrails

- **Checkout price immutability:** `order_items` persists the unit price resolved at checkout so subsequent price edits in [[container|catalog-database]] never alter historical purchases.
- **Soft-delete audit preservation:** Administrative cleanup and failed basket-cleanup compensation mark orders as soft-deleted (filtered from normal reads) rather than physically erasing rows.
- **Eventually consistent summary views:** Asynchronous order summary projections can briefly lag transactional writes; `orders` remains the canonical source for order status.
- **Confidential PII posture:** Buyer names and street/city/province addresses are classified `confidential` and protected by encryption at rest, regional residency, and backup retention policies.

:::warning
**Database-per-Service Isolation:** Only [[service|order-api]] may read or write `orderingdb`. Active pre-checkout baskets live in [[container|basket-database]], temporary locks and HTTP idempotency markers live in [[container|order-cache]], and post-checkout saga state lives in [[service|order-orchestration]]. External services must never query `orderingdb` directly.
:::

## Operational & Resilience Notes

<AccordionGroup>
	<Accordion title="Hosting, backup retention, and environment parity">
		- **Local vs. Cloud:** Local development runs a persistent PostgreSQL container alongside
		`catalogdb` and `schedulerdb`; cloud environments run Azure Database for PostgreSQL Flexible
		Server. - **Scaling & HA:** Development uses `Standard_B1ms` (single zone, 7-day backup, 32 GB);
		Staging uses `Standard_D2ds_v5` (zone-redundant HA, PgBouncer, 14-day backup, 128 GB);
		Production uses `Standard_D4ds_v5` (zone-redundant HA, PgBouncer, 35-day backup, 256 GB). -
		**Active messaging schema:** Treat the `wolverine` schema as the sole authoritative messaging
		store; ignore any retired messaging tables from historical migrations.
	</Accordion>
</AccordionGroup>

## Architecture Visualizations

<NodeGraph />

<MermaidFileLoader file="mermaids/order-database-diagram-1.mmd" />

## Related Documentation

- [[service|order-api]] — Owning service endpoints, gRPC clients, and Kafka handlers.
- [[container|order-cache]] — Redis locks, atomic HTTP idempotency, and cached responses.
- [[service|order-orchestration]] — Saga coordinating post-checkout cleanup and notifications.
- [[adr|adr-001-postgresql-order-system-of-record]] — Decision establishing PostgreSQL as Ordering's system of record.
- [[adr|adr-002-preserve-checkout-purchase-snapshot]] — Decision locking checkout-time prices in `order_items`.
- [[doc|operational/005-backup-restore-and-dr]] — Platform backup, restore, and disaster recovery runbook.
