Core Concepts
Transactional Outbox
Save the instruction to send a message in the same transaction as the business change.
A transactional outbox saves a message in the same database transaction as the change it reports. A separate publisher sends the message after the transaction commits. A pending message stays available if the application or publisher restarts.
Save the instruction with the turn
The ChatGPT walkthrough saves each accepted prompt as a turn, the work for one reply. Workers can claim pending turns directly from the database. In the queue-based extension here, the gateway also has to make the turn discoverable to workers that read the queue.
If the gateway commits the pending turn and crashes before sending the message, the reply job is still pending after restart. A worker reading only the queue has no message for it.
If the gateway sends the queue message first, a worker may receive the turn ID before the database commits. If the transaction rolls back, the message points to a job that was never saved.
A transactional outbox is a table of messages the application has committed to sending. Save the pending turn and its outbox event in the same database transaction. The event has its own ID, E19, and carries the type TurnQueued and the turn ID. The commit makes both rows available. A rollback leaves neither. After the commit, the gateway can send 202 Accepted while delivery is still pending.
The rows have to share the transaction. An outbox in another database would create another pair of writes that can succeed at different times. Put the fields the event needs in the saved row, or refer to the exact version it describes. A relay that reads the latest version later might publish a different state. AWS transactional outbox pattern
Publish the event from a separate process
A relay finds committed events and publishes them. For a small table it can poll in batches. Several relays can claim different rows for a limited time. After a crash, those claims can expire. The relay sends the event to a broker, a service that accepts messages for later delivery. When the broker confirms acceptance with the required durability, record the event as sent. A timeout keeps it eligible for retry even if the broker already accepted it.
Suppose the broker accepts E19 and the relay dies just before committing the sent marker. Another relay sees E19 as unsent and publishes it again. Keeping the same ID gives the consumer a way to recognize the repeat. Moving the sent marker earlier would risk leaving an event marked sent that the broker never received.
If the consumer writes to its own database, it can claim E19 under a unique constraint and create the work row in one transaction. Only the transaction that claims the ID applies the effect. Acknowledge the delivery after that transaction commits. A crash before acknowledgment can cause another delivery, which can be acknowledged once the consumer finds the committed ID. An external notification or payment needs a stable operation ID that the remote system honors, or a way to resolve the earlier outcome before another unsafe attempt. Idempotency
Keep each key in the order consumers need
Suppose a turn has two events, sequence 1 and 2, and a worker needs to apply both in that order. If two relays publish for that turn at once, the later event can reach the broker first. A keyed partition can preserve arrival order and still deliver the reverse of the intended sequence.
A relay can wait for the broker to confirm sequence 1 before it sends sequence 2 for that key. A slow earlier event then holds back later events for the same turn. A subscriber can also track the next expected sequence and fetch or wait for missing events. A consumer that only needs the latest complete state can use a version check. A consumer with one required effect per event needs the missing record.
Notice when delivery falls behind
Track the age of the oldest unsent event. The gateway can still accept turns while the relay is unable to reach the broker, so accepted work can wait longer even when database writes look healthy. Track the backlog and repeated failures. Give retries a delay, surface events that keep failing, and retain the data needed for your recovery window.
A relay can read the database's change stream instead of polling. PostgreSQL logical decoding reads the write-ahead log and can expose committed outbox changes. A position in that stream gives the relay a place to resume. A crash can repeat recent changes, and a stalled reader can cause the server to retain log data. The outbox row still defines the event the application intended to send. PostgreSQL logical decoding · Backpressure and Load Shedding
See also
- ACID Transactions explains what one database commit can protect.
- Messaging and Work Queues compares pending rows, work queues, and retained logs.
- Idempotency shows why a retry must protect the actual effect.
- Change Data Capture follows a snapshot and a stream of committed changes.
Used in
- ChatGPT introduces a saved dispatch record when a separate queue is added to the design.
- Object Storage (S3) saves a verified upload's finalization instruction alongside its pending database row.
Done reading? Mark it so it sticks in your dashboard.