---
id: basket-database
name: Basket Database
version: 1.0.0
summary: Authoritative Redis key-value store owned by the Shopping bounded context for customer-scoped pre-checkout shopping baskets.
owners:
  - commerce-fulfillment-team
container_type: database
badges:
  - content: Database
    backgroundColor: blue
    textColor: blue
technology: redis
authoritative: true
access_mode: readWrite
classification: internal
retention: transient
residency: eastasia
purpose: Authoritative store for active customer-scoped shopping baskets prior to checkout.
styles:
  icon: /icons/database/redis.svg
---

## Purpose & Ownership Boundary

Basket Database is the authoritative Redis key-value store owned exclusively by [[service|basket-api]] in the Shopping bounded context. Redis was chosen for low-latency, customer-scoped key-value persistence of ephemeral pre-checkout [[entity|basket]] state without requiring a relational SQL database, while delegating product metadata to [[service|catalog-api]] and accepted orders to [[service|order-api]].

## Data Store Profile

| Property                            | Value                                                                               |
| :---------------------------------- | :---------------------------------------------------------------------------------- |
| **Bounded Context / Owner**         | Shopping — [[team\|commerce-fulfillment-team]]                                      |
| **Engine / Technology**             | Redis (Azure Managed Redis in cloud; Redis container locally)                       |
| **Store Type**                      | Database (Key-Value)                                                                |
| **Primary Writer(s)**               | [[service\|basket-api]]                                                             |
| **Primary Reader(s)**               | [[service\|basket-api]]                                                             |
| **Authoritative / Source of Truth** | Yes (for active pre-checkout baskets)                                               |
| **Data Classification**             | Internal                                                                            |
| **Retention / TTL Policy**          | Transient (persisted until replaced, explicitly deleted, or cleared after checkout) |

## Logical Data Layout & Key Patterns

| Key Pattern                  | Stored Data                                                                                                                    | TTL / Eviction                                         | Invalidation / Update Trigger                                                                                                                            |
| :--------------------------- | :----------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `basket:{userId}`            | UTF-8 JSON [[entity\|basket]] containing customer `id` (`uuid`) and non-empty [[entity\|basket-item]] array (`id`, `quantity`) | No explicit TTL set; persists until mutated or deleted | Full-document atomic replacement (`SET`) on basket updates; deleted (`DEL`) by customer request or post-checkout [[command\|PlaceOrderCommand]] cleanup. |
| Integration idempotency keys | 24-hour Kafka message deduplication markers                                                                                    | `24 hours`                                             | Automatic TTL expiration after consumer processing.                                                                                                      |

Note: Neither the Aspire Redis configuration nor the generated Azure Redis templates declare an explicit eviction policy; verify any externally configured memory-pressure eviction policy before assuming indefinite idle retention.

## Consistency, Isolation & Security Guardrails

- **No duplicated catalog metadata:** Titles, descriptions, and prices are never stored in Redis; [[service|basket-api]] enriches items from [[service|catalog-api]] over gRPC at read time.
- **Full-set replacement & invariants:** Basket updates atomically overwrite the complete item list and enforce a non-empty basket invariant (empty baskets are deleted rather than stored).
- **Not a record of accepted orders:** Once checkout completes, [[service|order-api]] persists the immutable purchase in [[container|order-database]], and post-checkout cleanup publishes [[event|BasketDeletedCompleteIntegrationEvent]] or [[event|BasketDeletedFailedIntegrationEvent]].

:::warning
**Shared Redis Key-Prefix & Flush Isolation:** Basket Database shares the `redis` cluster with [[container|catalog-cache]], [[container|order-cache]], [[container|rating-cache]], and Notification idempotency markers. Because `basket:{userId}` is **authoritative** for active pre-checkout carts, flushing the shared Redis instance permanently drops all active customer baskets alongside cached responses and distributed locks. Only [[service|basket-api]] may read or write `basket:*` keys.
:::

## Operational & Resilience Notes

<AccordionGroup>
	<Accordion title="Hosting, scaling, and environment parity">
		- **Local vs. Cloud:** Local development runs a persistent single-container Redis instance with
		RedisInsight; cloud environments run Azure Managed Redis (`BalancedB0` in Dev, `BalancedB1` with
		HA in Staging, `BalancedB5` with HA in Production). - **Failure impact:** If a post-checkout
		delete fails, [[service|basket-api]] emits [[event|BasketDeletedFailedIntegrationEvent]] so
		[[service|order-orchestration]] can trigger compensation.
	</Accordion>
</AccordionGroup>

## Architecture Visualizations

<NodeGraph />

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

## Related Documentation

- [[service|basket-api]] — Owning service REST/gRPC endpoints, authorization, and Kafka handlers.
- [[container|order-database]] — Authoritative PostgreSQL store for confirmed purchases after checkout.
- [[container|order-cache]] — Sibling logical workload on the shared Redis resource.
- [[adr|adr-001-redis-authoritative-basket-store]] — Decision adopting Redis as the authoritative basket store.
- [[adr|adr-004-catalog-enrichment-at-read-time]] — Decision to enrich basket items at read time instead of caching prices.
- [[doc|operational/005-backup-restore-and-dr]] — Platform backup, restore, and disaster recovery runbook.
