Order, Payment and Inventory are separate services. How do you keep them consistent?
“Placing an order must create the order, charge the card and reserve stock across three services with their own databases. How do you make that consistent?”
What this tests
- Understanding what a local ACID transaction gave you and why it is gone across services
- Two-phase commit: how it works and why it is avoided in practice
- Sagas and the outbox pattern as the working alternatives
- Whether the candidate questions the service split itself
Answers by level
Read the beginner answer first and notice what is missing.
Inside one database a transaction gives you atomic all-or-nothing across the three tables. Across three services the only way to keep that is two-phase commit: a coordinator asks each participant to prepare (write and hold locks), then tells all to commit. It works, but participants hold locks while waiting on the network, and if the coordinator dies after prepare they are blocked indefinitely. Latency and availability both suffer, which is why almost nobody runs 2PC between services.
The practical alternative is a saga: a sequence of local transactions, each committed, with compensating actions for failure — reserve inventory, charge, and on payment failure release the reservation and cancel the order. The order is pending while this runs; the user sees that state. Each step publishes its outcome via an outbox so the state change and the event are written atomically in the same local transaction — see Distributed Transactions.
Green flags · Red flags
- Explains what 2PC does and specifically why it blocks
- Saga with compensation and a visible pending state
- Outbox pattern for atomic state + event
- Orders steps by cost of undo
- Questions whether the split is necessary
- "Use XA / a distributed transaction, the database handles it."
- Sequential calls with ad-hoc undo and no persisted saga state
- Believes the saga gives atomicity
- Publishes the event after commit with no outbox and cannot explain the lost-event window