Messaging patterns · senior
The outbox pattern and sagas
The outbox writes the event in the same database transaction as the change, and a relay publishes it later. A saga coordinates several services with steps and compensations.
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
- Outbox: write the event to an outbox table in the same transaction as the business change. A relay publishes it afterwards. This removes the dual write.
- Relay loop: begin, SELECT ... FROM outbox WHERE published_at IS NULL ORDER BY id LIMIT 100 FOR UPDATE SKIP LOCKED, publish each with the aggregate id as key and wait for the broker's acknowledgement, set published_at, commit.
- Never mark rows published before the broker confirms. A crash between publish and update republishes the batch, so delivery is at-least-once and every message needs a unique event id.
- SKIP LOCKED lets several relays share work, but then one order's events can go out of order. Use one active relay, relays that own hash ranges of the aggregate id, or consumers that check a per-aggregate version.
- On a publish failure, stop for that aggregate, back off, and retry. Keep batches small so transactions and locks stay short.
- Use a partial index on unpublished rows, and delete published rows in batches or partition by day. Debezium (change data capture) reads the database log instead of polling.
- Saga: a long business flow made of local transactions, with compensation steps (for example a refund) when a later step fails. Orchestration keeps the state in one place, so you can see where a saga is stuck.
- Give every saga step a deadline stored in the saga table, and alert on sagas stuck in one state. Replies must be idempotent by saga id and step. Workflow engines (Temporal, Step Functions) add timers, retries and history.
The red pen
Common traps: mistake → fix
Mistake: Publish to the broker inside the transaction
Fix: a rollback or crash still leaves a published event.
Mistake: Mark rows published, then send
Fix: a crash loses the event.
Mistake: Retry the whole checkout when a saga is stuck
Fix: find the step, resend the step command, or compensate.
Mistake: Change a topic name without telling the other team
Fix: contract tests and ownership prevent it.
Easy to hard
Questions interviewers ask, easy to hard
- What problem does the outbox pattern solve?
- Write the relay that publishes outbox rows.
- How do you keep order with several relays?
- 900 paid orders are stuck in a saga. What do you do?
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.