Menu

Dual write: the bug hiding between your database and your message broker
📰

Dual write: the bug hiding between your database and your message broker

DEV Community: backend·Dev May Cry·2 days ago
#UjGU247d
#devtobackend#devto#dev#order#database
Reading 0:00
15s threshold

You save an order to the database and then publish an order.created event. Two lines of code. What could go wrong?

DB :: transaction ( function () use ( $order ) { $order -> save (); });

Kafka :: publish ( 'order.created' , $order ); // what if this fails?

Enter fullscreen mode

Exit fullscreen mode

This is a dual write : one operation writing to two different systems (database + broker, database + cache, database + search index) with no transaction covering both.

Why it breaks:

Partial failure: the order is committed, but the publish fails (network blip, broker down, process killed mid-request). The order exists, and no downstream service ever hears about it.

Swapping the order doesn't help: publish first and you can end up with an event for an order that was never saved.

Race conditions: two concurrent requests can write in a different order to each system, leaving them out of sync.

It even hides in familiar patterns.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.