The transactional outbox pattern makes a business change and its event record part of the same database transaction. A separate relay publishes committed outbox rows and retries failures. This closes the gap created when an application commits a database write and publishes a message as two independent operations.
This guide implements the pattern with PostgreSQL 18, Node.js 26 LTS, and the pg\ driver . The relay sends events to a small HTTP consumer so the example can run without assuming a specific broker's delivery guarantees. The same outbox can feed a broker through a publisher adapter or a change-data-capture (CDC) pipeline.
The dual-write problem
Imagine an order service that saves an order and then publishes order.created :
await db . query ( " INSERT INTO orders ... " ); await broker . publish ( " order.created " , event );
Enter fullscreen mode
Exit fullscreen mode
There are two independent systems involved: the database and the event transport.…