System Design Atlas

Grocery orders with store inventory and substitutions

04 / 10Worked example

Interfaces

How do we distinguish lost response and new intention?

The load profile meets specific writing contracts. For B-17, E-17 now denotes the permanently stored payment instruction for the frozen amount. A command is a change order. Its idempotency key recognizes the same intention after a connection is terminated; the expected version, on the other hand, protects a now changed order status. Both of these elements solve different problems. All items and payment amounts are positive whole cent values; the proof of payment binds order, payment instruction, frozen amount and currency EUR.

Starting point The load profile from step 3 contains competing changes and technical repetitions.

Assumptions for the example case

Own example design for pick-up from a store and packaged piece goods with strictly positive integer cent prices. Free and fully discounted baskets are outside the case. Replacement items are specifically allowed in advance; the final positive amount arises from goods actually provided and is collected on collection. Figures and target values are illustrative design assumptions.

Answer can be lost

Command contract · request, commit, response and retry

Customer / Branch deviceSubmits the instruction and retries after disconnection
APIChecks identity, content and expected version
Storagecommits change and result together
  1. 01Customer / Branch device → APICommand + Key + Expected version
  2. 02API → StorageCompare content, save change and result together
  3. 03Storage → APICommit
  4. 04API → Customer / Branch deviceResult; response may be lost
  5. 05Customer / Branch device → APIRetry: same key, same content
  6. 06API → Customer / Branch deviceStored result
  7. 07API → Customer / Branch deviceSame key, different content → conflict

One commit stores the domain change and command result together. An identical retry returns this result; changed content produces a conflict.

Domain change and command result are stored together. An identical repetition gets the same result; a changed content creates a conflict.

Proposed API contracts

  1. POST/orders

    Create an order for exactly one store.

    Input
    Customer account, store, order lines, positive whole cent prices and expressly permitted alternatives.
    Result
    Order with all accepted allocations or complete conflict without partial order.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.
  2. POST/orders/{id}/positions/{position}/pick

    Prove original or permitted replacement permanently as picked.

    Input
    Store team of the store, expected version, scanned article, quantity and positive whole cent price; additionally confirmed original shortfall in the replacement.
    Result
    Pick proof and new version; incorrect version, unauthorized item, price zero or exceeded price limit reject completely.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.
  3. POST/orders/{id}/positions/{position}/omit

    omit a order line in a comprehensible manner.

    Input
    Store team, expected version, reason and, if applicable, confirmed empty original compartment.
    Result
    Omitted order line with zero amount and cancelled allocation.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.
  4. POST/orders/{id}/ready

    Complete picking and freeze the final basket.

    Input
    Store team and expected version; all order lines must be completed.
    Result
    Positive frozen amount and state ready; an empty basket leads to the cancellation review without a payment instruction.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.
  5. POST/orders/{id}/collect-payment

    Create the charge instruction for the frozen basket.

    Input
    Confirmed pickup and expected version; the server reads the positive amount and EUR from the frozen basket.
    Result
    202 with the existing payment instruction E-17 and a status URL; the response confirms local acceptance, not an external charge.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.
  6. GET/orders/{id}

    Read the current order and investigation status.

    Input
    Authorized identity; no command key and no expected version.
    Result
    Current status, final basket, payment instruction and clearing ground according to role.
    Retry behaviour
    Repeats do not change the state.
  7. POST/orders/{id}/cancel

    Begin goods resolution before a payment instruction exists or after definitive payment rejection.

    Input
    Authorized role, expected version and stable command key.
    Result
    Release open allocations; picked goods remain assigned until physical evidence resolves them. An active payment instruction or unknown outcome blocks cancellation.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.
  8. POST/orders/{id}/return-picked

    Record physically confirmed restocking or disposal of picked goods.

    Input
    Store team, expected version, pick record and saleable quantity or documented disposal.
    Result
    The actually picked article increases P at most once or remains unchanged when rejected.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.
  9. POST/orders/{id}/handover

    Hand over the paid order once.

    Input
    Store team, expected version and stable command key.
    Result
    Handover only after confirmed success for order B-17, payment instruction E-17, the frozen amount and EUR.
    Retry behaviour
    A stable command key is bound to the actor, operation and order. The same key and canonical content return the stored result; changed content produces a conflict.

Case decision and verification approach

Permanent command results

Save command result and domain change in the same transaction.

A scanner loses its connection after a successful pick.

Another click must not double the quantity or amount. An identical retry returns the same pick; changed content causes a conflict.

Alternative considered
A key only in memory loses its effect on restart.
Trade-off
Permanent command data requires retention and access protection.
Reassess
The measured scope can justify archiving; the domain uniqueness is maintained.

Terms in this step

Idempotency key
A stable identity for the same change intent after a connection is terminated.
Expected version
The last read order status; a deviation protects against a now changed order.

Try it yourself

Assess a repeating original pick with the same content, the same key for the replacement, and a new command with an outdated version.

A hint for your attempt

First check identity and content, then the changeable state.

Compare with a possible solution
Identical repetition
The same content gives the existing result without a second stock decrease.
Modified content
The same key for the replacement contradicts the stored content and is rejected.
New intention
A new command with an old version gets a version conflict and has to re-read the current status.

Apply to your project

Which inputs define the same intention in your project?

Step 5 specifies which states the commands change together.