The dual-write problem
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
explain why writing to a database and a broker separately can lose or invent events.
Planned topics
- A dual write updates two systems (database, broker) with no shared transaction.
- Commit then publish: a crash between the two loses the event. The order exists, nobody hears about it.
- Publish then commit: a rollback or crash leaves an event for an order that does not exist.
- Publishing inside the DB transaction does not fix it: the broker call is not part of that transaction.
- Retrying makes it worse without idempotency: you may publish twice.
Draft outline details
Demonstration
@Transactional
void place(Order o) {
repo.save(o);
kafka.send("order-placed", o.id(), event); // not atomic with the commit
}Check your understanding
The service crashes after the commit but before send. What is lost?
Show answer
The OrderPlaced event; the order stays in the database.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.