Microservices and APIs · senior
Sagas, the outbox and async checkout
Across services use a saga of local transactions with compensations, not two-phase commit. Publish events through a transactional outbox. Keep only what the user must wait for synchronous at checkout.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Two-phase commit blocks while waiting, its coordinator is a single point of failure and most brokers and services do not support it. It is rarely used across microservices.
- Saga: a series of local transactions, each with a compensating action. Create order (pending), reserve stock, authorise payment, confirm order. On failure compensate in reverse: void or refund payment, release stock, cancel order.
- Orchestration (a state machine in Temporal or AWS Step Functions, or your own table) suits many steps and a clear owner. Choreography (services reacting to events) suits short flows with few participants.
- Durable saga state after each step, idempotent steps and compensations keyed by saga id, retries with backoff, and a timeout per step. Failed compensations retry, then alert and go to a manual queue. Sagas are not isolated: others can see in-between states.
- Dual write problem: the order is saved but the event is lost if the pod dies between commit and publish. Publishing first fails the other way.
- Outbox: insert the event into an outbox table in the same transaction as the order; a relay publishes by polling or CDC (Debezium) and marks it sent. It is at-least-once, so consumers dedupe by event id. Use the order id as the key. Kafka transactions do not cover your database. Add a reconciliation job for orders with no downstream ack.
- At checkout keep synchronous only stock reservation and payment authorisation. Move notification, loyalty, analytics and invoice to an OrderPlaced event. Four services at 0.999 each give about 99.6 percent before the order service itself.
- Async costs: eventual consistency, harder debugging (correlation ids), duplicates and ordering, more infrastructure. Synchronous calls need timeouts, retries only for idempotent calls, and circuit breakers.
The red pen
Common traps: mistake → fix
Mistake: Use two-phase commit across services
Fix: it blocks and does not scale.
Mistake: Use Kafka transactions to fix the lost event
Fix: they do not include the database.
Mistake: Make payment confirmation async too
Fix: keep it synchronous.
Mistake: Publish the event before the commit
Fix: consumers see an order that may not exist.
Easy to hard
Questions interviewers ask, easy to hard
- Place an order across services. Why a saga and not 2PC?
- Orchestration or choreography?
- The order was saved but its event never went out. Why and how do you fix it?
- What stays synchronous at checkout?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.