System Design · Senior · Question 36 of 75
How do you atomically update the database and publish an event to a queue?
- ✓Correct answer: Outbox: the event is written to the DB in the same transaction
- 2Send the event first, then write to the database - order doesn't matter
- 3Send the event from the user's browser after a successful API response
Explanation
Without a transaction spanning the DB and the broker, inconsistencies are possible: the write succeeded but the event was lost (or vice versa). An outbox table records the event atomically with the change, and a relay publishes it with retries (at-least-once).