Menu

1 / 3

The Transactional Outbox Pattern in Postgres and NestJS: Ending Dual Writes Between Your Database and Your Queue — Field Notes

DEV Community: nestjs·Ahmed Mahmoud·5 days ago
#atbqatDW
#devtonestjs#devto#dev#outbox#order
Long article (2284 words) — no AI summary yet.
Reading 0:00
15s threshold

Headline: Saving a row in Postgres and then publishing a message to a queue is a dual write, and it loses or invents events whenever the process dies between the two calls. The transactional outbox pattern fixes it by inserting the event into an outbox table in the same Postgres transaction as the business change, then letting a relay publish pending rows with SELECT … FOR UPDATE SKIP LOCKED . Delivery becomes at-least-once, so consumers deduplicate by event ID.

For a long time my NestJS services created an order and then pushed an order.created job to a queue on the next line. It passed every test, because tests never kill the process between two awaits. Production does: SIGTERM during deploys, OOM kills, broker timeouts.…

Continue reading — create a free account

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