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
Create an outbox table: id, type, payload, created_at, sent_at.
In the business transaction, write both the business tables and the outbox table.
A relay process periodically reads unsent rows, publishes them to a message queue, and marks sent_at on success.
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.
🔀 Outbox: the message lands with the order first
Business service
Database
Relay process
Message queue
1Same transaction: write order + write outbox
2Read unsent rows
3Publish message
4Mark sent_at
5Crash after publish, before mark → resend
Component comparison
No component data for this language yet. On the home page choose "Decide later" and let AI recommend one.
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
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