Skip to content

.tip

· one of the small things

The outbox row goes in the same transaction

Publishing to the broker after the commit means two writes with nothing tying them together. If the process dies in between, the order exists and nobody downstream hears about it, or the message goes out and the commit rolls back.

BEGIN;
INSERT INTO orders (id, total) VALUES ($1, $2);
INSERT INTO outbox (topic, payload) VALUES ('order.created', $3);
COMMIT;

Both rows land or neither does. A worker reads the outbox table afterwards and delivers at least once, so give the consumer something to deduplicate on.

queuesdistributed-systems

Longer version: the post this came from.