ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/coordinator/tasks/task-19-3-5-shopping-cart
TASK

Implementation

Implement a complete e-commerce checkout saga with inventory, payment, and shipping services. This demonstrates the saga pattern in a realistic scenario.

E-commerce checkout saga:

Step 1: ReserveInventory
  - Service: InventoryService
  - Action: Reserve SKU quantities
  - Compensator: ReleaseReservation

Step 2: ChargePayment
  - Service: PaymentService
  - Action: Charge credit card
  - Compensator: RefundPayment

Step 3: CreateShipment
  - Service: ShippingService
  - Action: Create shipping label
  - Compensator: CancelShipment

Happy path example:

Request:  {"type": "checkout", "msg_id": 1, "saga_id": "checkout42", "user_id": "u42", "items": [{"sku": "abc123", "quantity": 2}, {"sku": "xyz789", "quantity": 1}], "shipping_address": "123 Main St"}

// Step 1: ReserveInventory
{"type": "ReserveInventory", "saga_id": "checkout42", "step": 1, "items": [{"sku": "abc123", "quantity": 2}, {"sku": "xyz789", "quantity": 1}]}
{"type": "ReserveInventory_ok", "saga_id": "checkout42", "step": 1, "reservations": [{"sku": "abc123", "reservation_id": "r1"}, {"sku": "xyz789", "reservation_id": "r2"}]}

// Step 2: ChargePayment
{"type": "ChargePayment", "saga_id": "checkout42", "step": 2, "user_id": "u42", "amount": 249.99}
{"type": "ChargePayment_ok", "saga_id": "checkout42", "step": 2, "payment_id": "p1", "transaction_id": "txn_12345"}

// Step 3: CreateShipment
{"type": "CreateShipment", "saga_id": "checkout42", "step": 3, "items": [...], "address": "123 Main St"}
{"type": "CreateShipment_ok", "saga_id": "checkout42", "step": 3, "shipment_id": "s1", "tracking_number": "1Z999AA1"}

// Saga complete:
{"type": "checkout_complete", "saga_id": "checkout42", "status": "COMPLETED", "order_id": "o123"}

Failure path example:

// Step 1: ReserveInventory (succeeds)
{"type": "ReserveInventory_ok", "saga_id": "checkout43", "step": 1, "reservations": [...]}

// Step 2: ChargePayment (fails - insufficient funds)
{"type": "ChargePayment_failed", "saga_id": "checkout43", "step": 2, "error": "insufficient_funds", "decline_code": "DECLINED"}

// Compensating: ReleaseReservation
{"type": "ReleaseReservation", "saga_id": "checkout43", "step": 1, "compensating": true, "reservations": [...]}
{"type": "ReleaseReservation_ok", "saga_id": "checkout43", "step": 1}

// Saga aborted:
{"type": "checkout_failed", "saga_id": "checkout43", "status": "ABORTED", "reason": "Payment declined: DECLINED"}

Sample Test Cases

Successful checkoutTimeout: 5000ms
Input
{"src":"c0","dest":"checkout_orchestrator","body":{"type":"init","msg_id":1}}
{"src":"c1","dest":"checkout_orchestrator","body":{"type":"checkout","msg_id":2,"saga_id":"checkout42","user_id":"u42","items":[{"sku":"abc123","quantity":2}],"shipping_address":"123 Main St"}}
Expected Output
{"src": "checkout_orchestrator", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Payment failure triggers rollbackTimeout: 5000ms
Input
{"src":"c0","dest":"checkout_orchestrator","body":{"type":"init","msg_id":1}}
{"src":"c1","dest":"checkout_orchestrator","body":{"type":"checkout","msg_id":2,"saga_id":"checkout43","user_id":"u999","items":[{"sku":"abc123","quantity":2}],"shipping_address":"123 Main St"}}
Expected Output
{"src": "checkout_orchestrator", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
Model the complete e-commerce checkout flow with 3 steps
Hint 2
T1: ReserveInventory - reserve items in stock
Hint 3
T2: ChargePayment - charge user's credit card
Hint 4
T3: CreateShipment - create shipping label
Hint 5
Compensators: ReleaseInventory, RefundPayment, CancelShipment
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

e-commerce sagainventory reservationpayment processingshipment creationreal-world sagacompensating transactions
main.py
python
Implement E-Commerce Checkout Saga - The Coordinator | Build Distributed Systems