Lesson 4 of 9 · 27m
Transactional Outboxes and Idempotent Producers
Guarantee event publication with an outbox, preserve logical event identity across retries, and keep database state and webhook delivery from diverging.
The fastest way to lose webhooks is to publish them after committing a database transaction. The second fastest is to publish them before committing. In one direction, the process crashes after the database write but before enqueueing; the event never exists. In the other, the webhook goes out and the transaction rolls back; consumers see a payment that never happened. The fix is not hope. The fix is a transactional outbox.
The outbox pattern
Write business state and event intent in the same database transaction:
BEGIN;
UPDATE invoices
SET status = 'paid', paid_at = now()
WHERE id = 'in_9fb311';
INSERT INTO webhook_events
(id, type, aggregate_id, payload, created_at, published_at)
VALUES
('evt_01J9RB5T', 'invoice.paid', 'in_9fb311', $1, now(), NULL);
COMMIT;A publisher process then scans unpublished rows, writes durable delivery attempts, marks events published only after enqueueing or dispatching them, and retries forever within policy. The important invariant is that a committed state change always leaves behind an event record. Crashes can delay delivery but cannot erase intent.
Event identity must survive attempts
Do not create a new event ID every time delivery is retried. The event ID is the identity of the business transition. Delivery attempts are separate:
webhook_events
id evt_01J9RB5T
type invoice.paid
payload canonical JSON snapshot
webhook_deliveries
id del_01J9RJD4
event_id evt_01J9RB5T
endpoint_id endp_42
status failed
attempt 3
next_retry_at 2026-11-30 18:37:41ZConsumers use evt_... for idempotent processing. Operators use del_... to inspect transport history. Collapsing those concepts makes incident analysis miserable.
Idempotency is not only a consumer concern
Producers need idempotent event creation too. If two workers process the same invoice transition, uniqueness constraints should prevent duplicate logical events:
CREATE UNIQUE INDEX webhook_events_business_key
ON webhook_events (type, aggregate_id, transition_key);The transition key might be paid, cancelled-at-2026-11-30T18:22Z, or a hash of meaningful state. Exact design depends on whether repeated transitions are meaningful. Do not blindly use mutable rows as event payloads without defining what transition they represent.
CDC can work, but it is not magic
Change data capture tools such as Debezium can stream outbox inserts from PostgreSQL WAL into Kafka. That moves complexity rather than eliminating it. You still need schema discipline, dead letters, replay controls, and a way to reconstruct events when the stream consumer crashes. For smaller systems, polling WHERE published_at IS NULL every few seconds is perfectly respectable.
Payload snapshots versus fetch-back
An outbox snapshot preserves what the producer knew at transition time. A thin event asks consumers to fetch current state. Snapshots can go stale; fetch-back can race future changes. Many mature systems send a compact snapshot plus object version and expose a fetch API. Document which one is authoritative.
Field notes
- Marking an event published before successfully enqueueing it recreates the original loss window one layer down.
- Beware
UPDATEin consumers triggering more events in producers; event cascades can become distributed feedback loops. - Keep payload size bounded. Put large artifacts behind authenticated fetch-back URLs rather than stuffing megabytes into the outbox.
- Archive old events, but retain enough history for consumer reconciliation windows and dispute resolution.
- Test crash recovery by killing the worker after database commit and before publication. If the event still eventually leaves, the pattern is working.
Was this lesson helpful?