Database Concurrency & Scaling interview question · Most asked
An order is saved but the 'OrderPlaced' event sometimes never reaches Kafka. How do you fix it?
Summary
Use the transactional outbox: insert the event into an outbox table in the same transaction as the order, and publish it via a relay or CDC (Debezium). Consumers dedupe because delivery is at least once.
Longer Answer
Writing to the database and broker separately can't be atomic: commit-then-publish loses events on crashes; publish-then-commit emits events for rolled-back orders. The outbox makes the event part of the database transaction. Monitor the outbox backlog and, for CDC, replication slot lag.
Key Points
- Dual writes are not atomic
- Outbox in the same transaction
- Relay or CDC publishes
- Idempotent consumers
Deep dive: Outbox and CDC
More most-asked Database Concurrency & Scaling questions
- How do you prevent overselling in a flash sale with 50,000 buyers in the first minute?
- After a deploy, logs show hundreds of 'deadlock detected' errors per hour on checkout. Walk through how you'd investigate.
- Two customers buy the last item at the same moment and both orders are confirmed. What happened, and how do you prevent it?
- Your service throws 'connection pool exhausted' errors but database CPU is only 20%. What's going on?
- Why shouldn't you call a payment API inside a database transaction?
- What's the difference between Read Committed and Repeatable Read in practice?
- Database CPU is at 85%. How do you decide what to optimise before buying a bigger server?
- A user saves their profile and the next page shows the old data. Reads go to replicas. How do you fix it?