The outbox row goes in the same transaction
Publishing to the broker after the commit is two writes with nothing holding them together. The process dies in between and the order exists with nobody downstream told, 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, which is at least once, so give the consumer something to deduplicate on.
queuesdistributed-systems
Longer version: the post this came from.