Skip to content

Transactional outbox and eventual consistency

Introduction

Persist the outbound message in the same transaction as the business data, then deliver it through a dedicated process so a successful write never silently skips the message.

Why use it

The database and the message queue are two independent systems and cannot share one transaction (see Transactions). That leads to two bad outcomes:

  • Write the DB first, then publish: the write succeeds, the process crashes before publish → the message is lost.
  • Publish first, then write the DB: the message goes out, the write fails and rolls back → downstream receives an order that never existed.

The outbox idea: persist the message as a row in the same database transaction as the business data, so they either both succeed or both fail.

How to do it

  1. Create an outbox table: id, type, payload, created_at, sent_at.
  2. In the business transaction, write both the business tables and the outbox table.
  3. A relay process periodically reads unsent rows, publishes them to a message queue, and marks sent_at on success.
  4. Because a crash after publish but before the mark causes a resend, delivery is at least once, so consumers must be idempotent — see Idempotency.

This is eventual consistency: not every instant is consistent, but after a short delay the systems converge.

Component comparison

Hand-written outbox table + polling

Hand-rolledOpen sourceOpsLow

Create an outbox table yourself, write to it within the business transaction, then have a background program read and send from it on a schedule.

Best forProjects with low message volume that want to validate the idea cheaply first; Python projects usually do it this way too.

Avoid whenMessage volume is very high, or you do not want to maintain retry and cleanup logic.

Pros

The logic is transparent and has no extra dependency, so failures are easy to trace

It is fully under your control, and any language and any queue can connect

There are few concepts, so getting one send path working is cheap

Cons

Polling adds delay, and polling too often adds pressure on the database

Retries, cleaning old rows, and claiming the same row are all yours to write

A message is delivered at least once, so the consumer must be idempotent on its own

MassTransit Outbox

Self-hosted open sourceOpen sourceOpsLow

The outbox feature built into the C# messaging framework MassTransit; once configured, it automatically puts messages and business data in the same transaction.

Best forC# projects that already use or plan to use MassTransit.

Avoid whenThe project is Python, or you do not want to introduce a full messaging framework.

Pros

It integrates with the EF Core transaction and works once it is configured

Retry, cleanup, and delivery are details the framework handles

Consumer-side deduplication, the inbox, can be used along with it

Cons

You accept its whole messaging model, which binds fairly deeply into the business

It applies only to C#, so projects in other languages cannot use it

The license policy has changed in recent years, so check the terms when you choose it

Official docs ↗

Wolverine

Self-hosted open sourceOpen sourceOpsLow

A C# messaging and command handling framework with a built-in transactional outbox, usable with EF Core or Marten.

Best forC# projects willing to try a newer framework and wanting to write less boilerplate.

Avoid whenYou need plenty of ready-made tutorials and community answers, or the project is Python.

Pros

The outbox is one of the core features, with concise configuration

Less boilerplate code

Also provides inbox deduplication

Cons

Smaller ecosystem and community than MassTransit

C# only

The framework has its own conventions that the team needs to learn

Official docs ↗

Debezium CDC (reading database logs)

Self-hosted open sourceOpen sourceOpsHigh

CDC means "change data capture": instead of polling, it reads the database's change log directly and forwards new rows in the outbox table to the message queue.

Best forTeams with high message volume that already have Kafka and dedicated ops.

Avoid whenSmall teams without Kafka infrastructure.

Pros

Nearly real-time, and adds no polling query load on the database

Independent of the application language, and Debezium officially provides Outbox-related support

Scales well at high data volume

Cons

You must deploy and maintain a set of infrastructure such as Kafka Connect, a high barrier

The database must enable change-log-related settings, with high permission requirements

Troubleshooting requires understanding database logs and connectors, a heavy burden for small and medium teams

Official docs ↗

SQLx outbox table

Open-source libraryOpen sourceOpsLow

Write events to an outbox table in the business transaction, then have a background task poll and send them, with retries on failure.

Best forProjects already on SQLx that want reliable event delivery.

Avoid whenEvents do not need to share a database transaction, or delivery must be built differently.

Pros

Events are written in the same transaction as the business data

Failed events can be retried

Depends only on SQLx, with no extra framework

Cons

Poll interval and row locking have to be written correctly

Redelivery requires consumers to be idempotent

Multiple instances need FOR UPDATE SKIP LOCKED so they do not claim the same row

Official docs ↗

Outbox table + worker

Open-source libraryOpen sourceOpsLow

Write events to an outbox table in the business transaction, then have a separate worker poll and publish to a queue or HTTP, with retries on failure.

Best forNode projects already on Prisma/Drizzle that need reliable domain events.

Avoid whenEvents do not need to share a database transaction.

Pros

Events are written in the same transaction as the business data

Can deliver with BullMQ or a simple poller

Not tied to a specific messaging framework

Cons

Poll interval, row locking, and idempotency are yours to design

Multiple instances need SKIP LOCKED or leases

Redelivery requires consumers to be idempotent

Official docs ↗

Outbox table + worker

Open-source libraryOpen sourceOpsLow

Write to an outbox table in the business transaction; a background goroutine or process claims rows with FOR UPDATE SKIP LOCKED and delivers them.

Best forGo services that need reliable event delivery.

Avoid whenEvents do not need a shared database transaction, or a CDC pipeline already exists.

Pros

Same transaction as business data

Doable with the standard library and few dependencies

Retries fit order and similar flows

Cons

Polling and locking must be written correctly

Redelivery requires idempotent consumers

More boilerplate without a framework

Official docs ↗

Checklist

  • Business data and the outbox row are written in the same transaction
  • The relay can retry on failure, and consumers are idempotent
  • Sent outbox rows are cleaned up or archived on a schedule
  • “Unsent backlog” and “age of the oldest unsent message” are monitored
  • Messages for the same business object stay ordered (partition by object ID when needed)