Full answer
Sample schema and data:
CREATE TABLE orders (id int PRIMARY KEY, status text);
CREATE TABLE outbox (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
topic text NOT NULL,
payload jsonb NOT NULL,
sent_at timestamptz
);
CREATE INDEX outbox_unsent ON outbox (id) WHERE sent_at IS NULL;BEGIN; INSERT INTO orders VALUES (1, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 1}'); COMMIT;Output:
COMMITBEGIN; INSERT INTO orders VALUES (2, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 2}'); ROLLBACK; SELECT (SELECT count(*) FROM orders) AS orders, (SELECT count(*) FROM outbox) AS events;PostgreSQL returns:
orders | events
--------+--------
1 | 1
(1 row)BEGIN; SELECT id, topic, payload FROM outbox WHERE sent_at IS NULL ORDER BY id LIMIT 100 FOR UPDATE SKIP LOCKED;Result on PostgreSQL 17:
id | topic | payload
----+------------+-----------------
1 | order.paid | {"order_id": 1}
(1 row)UPDATE outbox SET sent_at = now() WHERE id = 1; COMMIT; SELECT count(*) FILTER (WHERE sent_at IS NULL) AS unsent FROM outbox;Result:
unsent
--------
0
(1 row)The first transaction stores the order and its event together; the second rolls back and leaves neither, so the database and the event stream cannot disagree. The relay picks unsent events with SKIP LOCKED, so several relay instances can run, publishes them (outside this demo) and marks them sent. If the relay crashes after publishing but before committing, the event is published again, which is why consumers must be idempotent. Further points: keep outbox rows small, delete or partition sent rows, preserve order per aggregate if consumers need it, and consider logical decoding tools such as Debezium when polling latency matters.
CREATE TABLE orders (id int PRIMARY KEY, status text);
CREATE TABLE outbox (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
topic text NOT NULL,
payload jsonb NOT NULL,
sent_at timestamptz
);
CREATE INDEX outbox_unsent ON outbox (id) WHERE sent_at IS NULL;
BEGIN; INSERT INTO orders VALUES (1, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 1}'); COMMIT;
BEGIN; INSERT INTO orders VALUES (2, 'paid'); INSERT INTO outbox (topic, payload) VALUES ('order.paid', '{"order_id": 2}'); ROLLBACK; SELECT (SELECT count(*) FROM orders) AS orders, (SELECT count(*) FROM outbox) AS events;
BEGIN; SELECT id, topic, payload FROM outbox WHERE sent_at IS NULL ORDER BY id LIMIT 100 FOR UPDATE SKIP LOCKED;
UPDATE outbox SET sent_at = now() WHERE id = 1; COMMIT; SELECT count(*) FILTER (WHERE sent_at IS NULL) AS unsent FROM outbox;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- Write the change and the event in one transaction
- A relay publishes unsent rows
- SKIP LOCKED lets relays scale
- Delivery is at least once; consumers deduplicate
- Clean up sent rows
Common mistakes
- Publishing to the broker inside the database transaction
- Assuming exactly-once delivery
Follow-ups
- How would you keep per-order event ordering?
- When would logical decoding replace polling?
Tips
- Show the rollback case