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.…