Arezgitfield notes / engineering
Engineering practiceUPDATED OCT 9, 2026

Use a Transactional Outbox for Reliable Event Delivery

Coordinate database changes and event delivery with an outbox, bounded relay ownership, idempotent consumers, and explicit crash recovery tests.

Arezgit Editorial5 min read
AREZGIT / FIELD NOTEENGINEERING PRACTICE
A database update and a message publication can disagree even when both operations usually succeed. If the process crashes after committing an order but before publishing its event, downstre
READ / VERIFY / APPLYTECHNICALLY REVIEWED

A database update and a message publication can disagree even when both operations usually succeed. If the process crashes after committing an order but before publishing its event, downstream work may never start. Publishing first creates the opposite risk: a consumer receives an event for a change that did not commit.

A transactional outbox records the business change and the intent to publish in one local transaction. This guide focuses on that boundary, relay recovery, and consumer behavior. It does not claim that the database and an external broker become one atomic system.

Commit the business change and delivery intent together

In a synthetic order workflow, the transaction writes the order and an outbox row describing its creation. Either both records commit or neither does. The relay later publishes committed outbox rows independently of the request that created them.

AWS's transactional outbox guidance describes this approach to the dual-write problem and warns that duplicate delivery remains possible. The outbox closes the local recording gap; it does not remove ambiguity when publishing to another system.

Give every event an immutable identifier and an explicit schema version. Include the aggregate identifier and ordering information when consumers need them. Keep the payload limited to the downstream task rather than copying a complete database row with confidential fields.

Define the API's success promise accurately. A committed request can promise that the local change and publication intent are durable under the database's guarantees. It should not promise that every downstream consumer has already completed its work. If the product needs that later status, expose it separately.

Give relay ownership a bounded lifetime

Several relay workers need a way to claim work without publishing the same row concurrently by accident. Choose a documented strategy: short database locks, durable leases, or another coordination mechanism suitable for the database and expected workload.

If using leases, record the owner, expiry, and claim generation. Complete the row only when the worker still owns that generation. Otherwise a delayed worker could overwrite the state established by a replacement worker after the original lease expired.

For PostgreSQL, the SELECT documentation describes SKIP LOCKED as useful for queue-like tables while warning that it produces an inconsistent view. That can support concurrent claiming, but it does not define the complete ownership and recovery protocol.

Avoid holding a long database transaction open while waiting indefinitely for the broker. Bound publication time, keep claim batches small, and decide what happens when a worker loses ownership during an outbound operation. A lease expiry cannot cancel a message already accepted by the broker.

Make duplicate handling part of the consumer transaction

Consider a synthetic crash after the broker accepts an event but before the relay marks it delivered. On recovery, the relay cannot safely infer that publication failed. Sending again preserves delivery opportunity and may produce a duplicate.

At a database-backed consumer, combine the processed-event record with the business side effect in one transaction. A uniqueness constraint on the event identity can coordinate concurrent deliveries. An application-only check followed by an unrelated write leaves its own race.

If the consumer calls another remote service, its local deduplication table alone cannot make that external side effect atomic. Use the provider's supported idempotency mechanism or a separately designed downstream workflow. Marking an event complete before the remote action risks loss; marking it afterward retains retry ambiguity.

The guide to idempotent write operations explains this distinction. For each consumer, write down what repeated processing is allowed to repeat and what must occur once from the business perspective. Do not substitute the phrase "exactly once" for that analysis.

Separate ordering failures from poison events

If updates to one order must be applied in sequence, define an aggregate version and the consumer's gap policy. Global timestamps are rarely enough to establish a total business order across concurrent transactions.

In a synthetic example, version three arrives before version two. The consumer might defer it, request current authoritative state, or reject it into a recovery queue. Choose based on whether the event represents a historical transition or a replaceable state notification. Those two meanings require different recovery behavior.

A poison event fails repeatedly because its payload, schema, or referenced state cannot be handled. Bound attempts and retain enough sanitized metadata to investigate. A recovery queue needs an owner and a replay procedure; moving an event there is not completion of the business task.

When replaying, preserve the original event identity if the intent is to retry the same event. Creating a new identity can bypass consumer deduplication and repeat a side effect. If a corrected event is genuinely new, document its relationship to the original and the expected consumer transition.

Verify the crash boundaries explicitly

Use a synthetic failure matrix to make the durability promise reviewable:

  • Before local commit, neither the business change nor the outbox intent should survive.
  • After commit but before a claim, a relay should still be able to claim the durable event.
  • During broker publication, retry should remain possible under bounded ownership.
  • After broker acceptance but before completion, the consumer should handle duplicate delivery.
  • During consumer processing, deduplication records and local effects should agree after recovery.

Exercise these cases with process termination and controlled dependency failures in an isolated environment. Include concurrent relays and duplicate consumers, not only one worker delivering one event successfully.

Track oldest pending-event age, backlog size, claim expiry, delivery attempts, and recovery-queue growth. Use bounded labels rather than event identifiers in metric dimensions. Keep payloads and credentials out of ordinary logs, following the application logging guide.

Set retention according to replay and audit needs. Premature deletion can remove the evidence needed to recover a failed consumer, while indefinite retention expands storage and privacy exposure. The design is ready when operators can distinguish an uncommitted change, a durable unpublished intent, a retryable delivery, and completed consumer work.

READER SIGNAL

Was this guide useful?

Your rating helps us prioritize clearer, more practical technical content.

AREZGIT / DESKTOP WORKSPACEFROM FIELD NOTE TO RELEASE

Review diffs, run checks, and prepare the release in Arezgit.

Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.

Explore Arezgit