---
id: basket
name: Basket
version: 1.0.0
identifier: customerId
aggregateRoot: true
summary: Customer-scoped, mutable pre-order collection of requested books and quantities managed prior to checkout.
owners:
  - commerce-fulfillment-team
badges:
  - content: Aggregate Root
    backgroundColor: blue
    textColor: blue
properties:
  - name: customerId
    type: string
    required: true
    description: Authenticated customer identifier scoping the basket (one active basket per customer).
  - name: items
    type: array
    required: true
    description: Requested book entries, replaced atomically as a complete set on update.
    references: basket-item
    referencesIdentifier: bookId
    relationType: contains
    items:
      type: basket-item
  - name: createdAt
    type: datetime
    required: true
    description: UTC timestamp when the basket was created.
  - name: lastModifiedAt
    type: datetime
    required: false
    description: UTC timestamp of the most recent basket update.
---

## Domain Role & Boundary

**Basket** is the Aggregate Root in the [[domain|shopping]] bounded context representing an authenticated customer's mutable pre-purchase selection of books. Owned by [[service|basket-api]], it provides sub-millisecond read and write access during storefront browsing and supplies checkout snapshots to [[service|order-api]]. It explicitly does **not** own product pricing or availability (resolved dynamically from [[domain|catalog]]), inventory reservations, or accepted purchase records (owned by [[domain|ordering]]).

## Entity Profile

| Property                      | Value                                                                                 |
| :---------------------------- | :------------------------------------------------------------------------------------ |
| **Bounded Context / Domain**  | [[domain\|shopping]]                                                                  |
| **Owning Service**            | [[service\|basket-api]]                                                               |
| **DDD Role**                  | Aggregate Root                                                                        |
| **Identifier Strategy**       | Authenticated customer ID (GUID-formatted string, derived from access token)          |
| **Persistence Store**         | Shopping key-value store (`basket:{customerId}`, no SQL database)                     |
| **Deletion Semantics**        | Hard-delete (customer clear or idempotent post-checkout cleanup; no automatic TTL)    |
| **Concurrency Control**       | Last-write-wins with whole-set replacement; idempotent message handling on cleanup    |
| **PII / Data Classification** | Internal customer-scoped identifier (`customerId` only; no names or addresses stored) |

## Attributes & Constraints

<EntityPropertiesTable />

| Attribute                     | Business Rule / Constraint                                                                            | Sensitivity / Notes   |
| :---------------------------- | :---------------------------------------------------------------------------------------------------- | :-------------------- |
| `customerId`                  | Required; derived strictly from the authenticated customer token (one basket per customer).           | Customer-scoped key   |
| `items`                       | Required; must contain at least one [[entity\|basket-item]] on create and update at the API boundary. | Whole-set replacement |
| `createdAt`, `lastModifiedAt` | UTC audit timestamps serialized with the basket payload.                                              | Metadata              |

## Domain Invariants & Lifecycle

- **Authenticated ownership:** A basket always belongs to a single authenticated customer; anonymous baskets are not persisted.
- **Non-empty creation and update:** Creating or updating a basket via the API requires at least one valid [[entity|basket-item]]. To empty a basket, the customer explicitly deletes it.
- **Whole-set replacement:** Updating a basket replaces the entire `items` collection rather than merging partial line-item diffs.
- **Post-checkout cleanup:** Upon receiving [[command|PlaceOrderCommand]] from [[service|order-orchestration]], [[service|basket-api]] deletes the basket idempotently and publishes [[event|BasketDeletedCompleteIntegrationEvent]] or [[event|BasketDeletedFailedIntegrationEvent]].

:::warning
A basket represents mutable shopping intent—never a confirmed order or an inventory hold. If a basket fails to delete after checkout, [[domain|ordering]] compensates rather than allowing stale basket state to be misread as an active order.
:::

## Relationships & Associated Operations

| Category                     | Resource                                                                                                        | Role / Interaction                                            |
| :--------------------------- | :-------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------ |
| **Related Entities**         | [[entity\|basket-item]]                                                                                         | Embedded collection of requested books and quantities         |
| **Cross-Context References** | [[entity\|book]], [[entity\|order]]                                                                             | Items reference Catalog books; checkout hands off to Ordering |
| **Mutating Commands**        | [[command\|CreateBasket]], [[command\|UpdateBasket]], [[command\|DeleteBasket]], [[command\|PlaceOrderCommand]] | Create, replace, or delete the customer's basket              |
| **Read Queries**             | [[query\|GetBasket]], [[query\|GetBasketGrpc]]                                                                  | Enriched storefront read and internal checkout snapshot       |
| **Emitted Events**           | [[event\|BasketDeletedCompleteIntegrationEvent]], [[event\|BasketDeletedFailedIntegrationEvent]]                | Report post-checkout cleanup outcome to Finance               |
