Friday night, 9:47 p.m. A customer confirms their cart, the card is charged, and the screen says "Your order has been received." On Monday the customer service phone rings: "My money's gone, but there's no shipment." The support team opens the admin panel, and the order is right there. On the warehouse screen, though, the order never shows up. No invoice was issued, and no notification text was sent.
Nobody made a mistake. The order service restarted in a tiny fraction of a second: right after saving the order and right before telling the warehouse "there's a new order." This post explains why messages get lost between systems and how to prevent it for good.
The Same 100,000 Orders, Four Different Methods
In an e-commerce system, the order service does two things: it writes the order to its own database and it notifies the other systems. These notifications usually travel through a message queue; Kafka and RabbitMQ are the best-known examples. The warehouse, invoicing, and SMS services listen to this queue.
To see what happens, we set up a small experiment. We ran 100,000 orders step by step on a computer. Between any two steps, there is a 1-in-1,000 chance that the program crashes or the connection drops. When the program restarts, it resumes from wherever its own records say it left off. A single consumer, the warehouse service, reads the queue. The sample data is synthetic; anyone can rerun the experiment's code.
| Method | Orders whose message was never sent | Messages with no order | Orders processed twice | Total problems |
|---|---|---|---|---|
| Save first, then send | 118 | 0 | 87 | 205 |
| Send first, then save | 0 | 89 | 85 | 174 |
| Outbox + mail carrier | 0 | 0 | 202 | 202 |
| Outbox + consumer that recognizes repeats | 0 | 0 | 0 | 0 |
The first row is the most commonly written code. The order is saved, then the message is sent. If a crash happens in between, the order exists but the warehouse never hears about it. The customer at the top of this post is one of these 118 orders.
The second row reverses the order. This time nothing is lost, but there's a different problem: the message goes out, but the order fails to save. The warehouse starts preparing 89 orders that don't exist in the system.
In both rows, about 85 orders are processed twice. The cause is on the consumer side: if the warehouse service does the work and crashes before saying "got it," the queue delivers the same message again. That's a deliberate design choice in queues: better for a message to arrive twice than to be lost.
The third and fourth rows are the solution this post is about. Lost and phantom messages drop to zero. In the third row the duplicates seem to go up; in the fourth row those duplicates become harmless, too. We explain how below.

Why Is This So Hard?
The order service writes to two separate ledgers: its own database and the message queue. Each ledger is reliable on its own. A database works on an "all or nothing" basis; a half-finished write is rolled back. But there's no such link between the two ledgers. If you write to one and stop before writing to the other, the two ledgers start lying to each other. Engineers call this the "dual write" problem.
One in a thousand looks like a small chance. For a store that takes 5,000 orders a day, that rate means about 6 orders silently disappearing every day, or more than 2,000 problem orders a year. Even when we cut the crash probability by a factor of ten, 25 problem orders remain out of 100,000. The problem is solved by the method, not by the rate.
There is a well-known way to combine the two ledgers into a single transaction: two-phase commit. In practice it rarely works out. As the Debezium team's article points out, Kafka doesn't support taking part in this kind of distributed transaction. And in systems that do support it, it ties both sides tightly to each other.
The Solution: Write the Letter in the Same Ledger as the Order
Picture a shopkeeper. When writing a new line in the order ledger, they also jot down "To the warehouse: order 1042 has been created" in the "letters to send" section at the bottom of the same page. Both lines are written with the same stroke of the pen. Either both make it into the ledger or neither does.
The shop also has a mail carrier. Their only job is to read the ledger: if they see a letter that hasn't been sent, they post it and then write "sent" next to it. Even if the shopkeeper faints after writing the letter, the letter is still in the ledger. Sooner or later the mail carrier finds it and sends it.
In software, the ledger is the order's own database. The "letters to send" section is a separate table in the same database: the outbox. Because the order and the message are written in a single database transaction, they can never come apart. This method, described by Chris Richardson, is called the "transactional outbox." In his words, messages are sent if and only if the database transaction commits.

The second half of the animation shows the method's cost. If the mail carrier posts the letter and crashes before writing "sent," it sends the same letter again after it restarts. In our experiment, the mail carrier sent 104 extra messages as duplicates. Zero losses, but some repeats. That's not bad news; handling duplicates is much easier than handling losses.
How Does the Mail Carrier Read the Ledger?
There are two ways. The first is for the mail carrier to check the ledger every few seconds: "any new letters?" It's simple and good enough for most systems. The second is more elegant: every database writes each change it makes, in order, to its own internal log. Instead of querying the table, the mail carrier can follow this log live. This is called "change data capture."
The best-known open-source tool for this is Debezium. Its documentation includes a ready-made transformation for the outbox: it reads every row that lands in the table, routes it to the right queue based on the order type, and adds a unique ID to every message. The documentation says this ID can be used precisely to filter out duplicates.
Why "Exactly Once" Delivery Is a Myth
You sent a friend an important message and got no reply. Did the message never reach them, or did it arrive and their reply get lost on the way back? You can't know. You have two options: resend it, and maybe it arrives twice, or don't, and maybe it never arrives. There's no third option. Every exchange of messages between systems lives with this dilemma.
That's why Kafka's "exactly once" feature needs to be read carefully. According to the documentation, the idempotent delivery option available since version 0.11.0.0 keeps the producer's retries from writing duplicates to the queue. In current versions this setting is on by default. Transaction support, meanwhile, provides exactly-once processing for streams that read from Kafka and write back to Kafka. But the same documentation states plainly that for other destination systems, this generally requires cooperation from those systems. The warehouse service's database is outside Kafka; the guarantee doesn't automatically extend there.
The RabbitMQ documentation says the same thing: an unacknowledged message is requeued when the connection drops, and consumers should be written to be ready for redelivery and to recognize duplicates.
The real-world answer is this: guarantee that the message arrives at least once, and build the consumer so that it recognizes the same message when it sees it a second time.
A Consumer That Recognizes Repeats
Think of an experienced clerk in accounting. They write the number of every invoice that crosses their desk onto a list. If an invoice with the same number arrives a second time, they don't pay it; they just say "this has already been paid" and set it aside. Even if the invoice arrives ten times, the money goes out once.
The warehouse service does the same thing. It writes each message's unique ID to a "processed messages" table. The critical detail is that this ID is written in the same database transaction as the stock reservation. That way, an in-between state of "stock reserved but ID not recorded" can never arise. In engineering this is called an "idempotent consumer": no matter how many times you do the same work, the result stays as if it had been done once.

The last row of the table is the result. In the experiment, 195 duplicate messages reached the warehouse service, and all of them were quietly skipped. No order was lost, and none was prepared twice.
Five Things That Break It in Real Life
On paper, the method is simple. The things that cause trouble in the field are hidden in the details.
1. Messages Can Arrive Out of Order
A customer places an order and cancels it two minutes later. If the warehouse service receives "cancelled" first and "created" second, it starts preparing a cancelled order. Messages for the same order need to pass through the same line. In Kafka, the way to do this is to make the order number the message key; as the Debezium article explains, messages with the same key land in the same partition and are read in order. Preserving this rule while running multiple mail carriers in parallel takes extra care.
2. The Outbox Bloats
5,000 orders a day means 1,825,000 rows in the outbox in a year. A cleanup job that deletes sent letters at regular intervals is a must. If change data capture is in use, there's an even easier way: the row can be deleted immediately, inside the same transaction in which it was inserted. Because the database log has already recorded the insert, the mail carrier still sees the message, and the table always stays empty.
3. A Poison Message Clogs the Queue
Imagine a corrupt message: every time the consumer reads it, it throws an error and crashes. The queue redelivers the message, and the consumer crashes again. Hundreds of healthy messages behind it are left waiting. The fix is to limit how many delivery attempts a message gets and move the ones that exceed the limit to a separate "problem messages" queue. In RabbitMQ's quorum queues, this limit has defaulted to 20 attempts since version 4.0.
4. Lag Builds Up Silently
The outbox doesn't lose messages, but if the mail carrier stops, messages pile up. Nobody may notice for hours, because nothing throws an error. There's a single number to watch: how many minutes has the oldest unsent message been sitting there? When that number goes past a few minutes, an alarm should go off.
5. Replaying Past Messages
Once a bug is fixed, past days' messages can be replayed to the consumer. If the consumer recognizes repeats, this is safe, but the "processed messages" list has to be kept for at least as long as that replay window. Actions that can't be undone, such as sending an SMS or an email, can't be part of a database transaction. There you still have to choose: occasionally two texts, or occasionally none? For things like money and stock, losses are unacceptable; for a notification, most businesses prefer a second text.
Does Every Business Need This?
No. If a single application keeps orders, stock, and invoices in a single database, there aren't two ledgers; everything is written in one database transaction and the problem in this post never arises. For a business that takes a few dozen orders a day and checks every evening whether "there's any order in the panel that isn't in the warehouse," that simple comparison may well be enough.
The outbox pays for itself when more than one service or external system has to know about the same event, when money is involved, and when volume grows too large to check by hand. Even in a system like that, a reconciliation report that runs every night is valuable as a second seatbelt.
Queue systems and endpoints that are sensitive to repeated requests, such as payments, are at the heart of the work we describe on our Backend and API development page.
For developers: what do the outbox and a repeat-aware consumer look like in SQL?
The example below was written for PostgreSQL. The outbox table uses the default columns expected by Debezium's outbox transformation (id, aggregatetype, aggregateid, type, payload). The first block runs in the order service, the second in the warehouse service. processed_messages.message_id must be the primary key.
BEGIN;
INSERT INTO orders (id, customer_id, total) VALUES ('o-1042', 'c-77', 1250.00);
INSERT INTO outbox (id, aggregatetype, aggregateid, type, payload)
VALUES (gen_random_uuid(), 'order', 'o-1042', 'OrderCreated', '{"orderId":"o-1042"}');
COMMIT;
BEGIN;
INSERT INTO processed_messages (message_id)
VALUES ('5b30857f-0bfa-48b5-ac0b-5c64e28078d1') ON CONFLICT DO NOTHING;
UPDATE stock SET reserved = reserved + 1 WHERE sku = 'A-1';
COMMIT;
The warehouse service checks the result of the first statement: if it returns INSERT 0 0, the ID has already been processed; the service rolls back the transaction without touching the stock row and simply acknowledges the message. On the Debezium side, the transformation is defined with transforms.outbox.type=io.debezium.transforms.outbox.EventRouter, and with the default settings this row is routed to the outbox.event.order topic.
Sources
- Chris Richardson, "Pattern: Transactional outbox": microservices.io
- Debezium 3.7 documentation, Outbox Event Router: debezium.io
- Gunnar Morling, "Reliable Microservices Data Exchange With the Outbox Pattern," Debezium blog, 2019: debezium.io/blog
- Apache Kafka 4.4 documentation, Message Delivery Semantics and
enable.idempotence: design, producer configs - RabbitMQ 4.4 documentation: Consumer Acknowledgements and Publisher Confirms, Quorum Queues
- PostgreSQL 18 documentation, INSERT and ON CONFLICT: postgresql.org
Note: The 100,000-order experiment in this post is synthetic; the 1-in-1,000 crash probability at each step is an assumption, not a measurement of a real system. The example of 5,000 orders a day was chosen only to illustrate scale. The order numbers and message flows in the animations are illustrative; the counters and bars show the experiment's results.