KatInterview

Atomic Updates Across Orders, Payments and Inventory Services

System DesignModel answer
HardModel answerSystem Designdistributed-transactionstwo-phase-commitsaga

Last updated

Design a protocol that keeps writes consistent across several services and databases, comparing 2PC, 3PC and Sagas, and covering failures, retries, timeouts and delivery guarantees.

A business operation, such as placing an order, must update several independent systems: an orders database, a payments service and an inventory service. Some are databases you control. Others are third-party services you do not.

Design a protocol that coordinates these updates so that the system always ends in a consistent state: either every update takes effect, or the operation is cleanly undone.

Assumptions you may use or adjust

  • Peak load of about 10,000 transactions per second, with 2 to 5 participants each.
  • Short cross-shard transactions need p99 latency under 50 ms within a region. Long workflows may run from seconds to days.
  • Committed work must never be lost. For money movement, correctness matters more than availability.
  • Nodes can crash and recover. Messages can be lost, duplicated or reordered. Network partitions happen.

What to cover

  1. Requirements and the API the client and participants would see.
  2. The data you persist on the coordinator and on each participant.
  3. A comparison of two-phase commit, three-phase commit and the Saga pattern: message flows, failure handling and when each is appropriate.
  4. How you achieve idempotency, which timeouts you set and where, and what delivery guarantees you can honestly offer (at-least-once versus exactly-once).
  5. Trade-offs, and which approach you would pick for which scenario.

Hints

Hint 1

Start by separating two problems: a short, strongly consistent cross-shard commit versus a long-running business workflow. They call for different protocols.

Hint 2

For each protocol, ask what happens if the coordinator dies at the worst possible moment, and what a participant that has already voted yes is allowed to do.

Hint 3

Exactly-once delivery is not achievable over an unreliable network. Think about at-least-once delivery combined with deduplication written in the same local transaction as the business effect.