dine. a kitchen run by sagas Book a table
Log in with Google (coming soon)

Articles

Tiếng Việt · English

Transactional outbox: writing to the database and sending to Kafka without drifting apart

October 13, 2026 · about 10 min read

At first I thought sending a message from order to Kafka was the simplest part of the whole system: save the order, then send it off, that's it. It turned out not to be simple at all. This article is about why, and how dine solves it with the transactional outbox.

When you press Book a table, order has two jobs to do

  1. Write to MySQL. The orders table stores the order. A few other tables come with it: order_items (the dishes), sagas and saga_steps (tracking which step the order is at with each service). Taken together, this is the MySQL job.
  2. Send a message to Kafka, to the order-events topic, so the table, kitchen and payment services can start working.
order has two jobs: job 1 writes the orders, order_items, sagas and saga_steps tables in MySQL; job 2 sends a message to the order-events topic on Kafka. order guest books a table 1 2 MySQL orders · order_items sagas · saga_steps Kafka order-events save the order notify 3 services
Two jobs, two different systems. No transaction can span both MySQL and Kafka.

The approach everyone thinks of first is to do them one after the other: write to MySQL, then send to Kafka. In testing it works every time. The problem only shows up when one of the two jobs fails halfway, and in practice that happens sooner or later: the process is killed during a deploy, runs out of memory, Kafka is restarting, the network is flaky.

Two ways it breaks

Case 1: MySQL is written, but sending to Kafka fails

The order is in the database, but the message never reaches Kafka. The consequences:

Case 2: the message reaches Kafka, but writing to MySQL fails

This happens when the code sends to Kafka first and writes to MySQL afterwards, or sends to Kafka inside the transaction and then the COMMIT fails. The message has gone out, but the order is not in the database. The consequences are even worse than case 1:

Case 1: MySQL is written, Kafka fails, the order sits there unknown to anyone. Case 2: MySQL fails, Kafka is sent, three services work for an order that does not exist. Case 1 MySQL ✓ Kafka ✕ The order is in the database, but no service knows about it. It just sits there, no error anywhere. Case 2 MySQL ✕ Kafka ✓ Table held, stock taken, money charged for an order that does not exist. Nobody steps in to undo it.
The two systems drift apart in both directions. This is known as the dual write problem: writing to two places and wanting both to be right.

Both cases have the same root: MySQL and Kafka are two separate systems, and no transaction can span both (the article Why saga instead of 2PC explains why dine does not use two-phase commit here). Swapping the order of the two jobs only swaps one case for the other; it does not make the problem go away.

The fix: an outbox

The idea of the outbox is very simple: don't send the message right away, write it down first.

In one single MySQL transaction, order writes to the orders, order_items, sagas and saga_steps tables, and also writes the message into one more table called outboxes. Everything lives in one database, so the database can guarantee it: either everything is written, or nothing is.

The two original jobs then become:

Outbox: order writes orders, order_items, sagas, saga_steps and outboxes in one transaction; a background job reads the outboxes table every 100ms, sends the message to Kafka and, once sent, marks it as sent. order API MySQL, 1 transaction orders order_items sagas · saga_steps outboxes message: waiting to send read background job scans every 100ms Kafka once sent: marked "sent"
Job 1 is done the moment the transaction commits. Job 2 is now a row of data, sent later by the background job.

Why the outbox fixes both cases

The "write to two places" problem becomes "write to one place". Writing to one place correctly is something databases have done well for a long time.

On the diagram of the order detail page, the Kafka box sometimes shows "command still in the outbox, not sent": that is exactly the window where the message has been written but the job has not reached it yet.

Once you see it, you see outboxes everywhere

The problem above is not unique to creating orders. It shows up anywhere a single action has to write to two different storage systems that do not share a transaction: a database and a message queue, a database and a search index, a database and an external service.

In dine, the way out is only half the story. When the table service, the kitchen or payment finishes its work, it has to do exactly the same two jobs as order: write the result into its own MySQL, then send its reply to saga-step-responses. If the kitchen takes the stock but its reply never arrives, order waits out the 5 seconds and cancels an order the kitchen had already completed. So all three services have outboxes of their own. Undo commands go through the outbox too. The whole system has four outboxes, and no message leaves a service without passing through that service's outbox.

The four outboxes in dine: order has an outbox to send commands to order-events; reservation, kitchen and payment each have their own outbox to send replies to saga-step-responses. order outbox Kafka order-events reservation outbox kitchen outbox payment outbox Kafka saga-step-responses
Four services, four outboxes. Going out or coming back, every message is written in the same transaction as the work it reports, and only then sent.

Outside dine, you will meet exactly this shape in very familiar places:

The tell-tale sign is simple: whenever you see a line of code that says "save, then send" or "send, then save", ask "what if it dies right between these two lines?". If the answer is "the data drifts apart", that spot needs an outbox.

The remaining risk: a message can be sent twice

There is still one gap that cannot be avoided. The job sends the message to Kafka successfully, but dies before it can mark it "sent" in MySQL (killed during a deploy, or the database connection drops at exactly that moment).

The next time the job runs, it sees the message is still not marked, so it starts over: it sends that message to Kafka again. (At dine, a message whose job died mid-send is put back to "waiting to send" after about 30 seconds, so the job picks it up again.)

The job takes a message, sends it to Kafka successfully and dies before marking it sent; when it runs again the message is still unmarked, so it is sent again; Kafka ends up with two copies of the same message. 1. Take message waiting to send 2. Send to Kafka succeeds 3. Job dies before marking 4. Runs again still waiting 5. Sent again Kafka has 2 copies A message is never lost, but it may arrive twice. That is the price of splitting "send" and "mark" into two steps: there is no way to make both happen at once.
Dying at step 3 is the main source of duplicate messages. Dying at any other step only makes the message slower, never lost.

The outbox guarantees a message is never lost, but it may arrive twice (at-least-once). There is no way to write to the database and send to Kafka "exactly once" with an outbox alone. So the receiving side must tolerate duplicates: when it gets "hold a table for order X" a second time, it recognizes it has already done that and does not hold another table. The article Receiving a command twice without doing it twice explains how dine does it.

Another price: about 100 ms slower

The message does not go out immediately; it waits for the job's next scan. The order detail page can measure this, because all four services record what they do. This is the kitchen leg of a real order, from the moment order writes the message into the outbox to the moment it has received the kitchen's reply:

SegmentTime
Waiting in order's outbox52 ms
Kafka delivers to the kitchen0 ms
Kitchen processing (take stock, write the reply into the kitchen's outbox)11 ms
Waiting in the kitchen's outbox44 ms
Kafka delivers back, order updates the saga6 ms

96 out of 113 ms is outbox wait. The job scans every 100 ms, so a freshly written message waits about half an interval on average, around 50 ms, and a round trip passes through two outboxes.

There is nothing special about 100 ms. It is simply the pause I chose between two MySQL scans: short enough that a person booking a table cannot notice it, sparse enough that the database is not asked constantly when there are no orders at all. Another system might choose 10 ms, 1 second, or several seconds, depending on how fast its work needs to be.

If you need it close to instant, there are a few ways:

For dine, being about 100 ms slower in exchange for a simple table-scanning job is a very good deal.

Can you avoid the outbox?

You can avoid the outboxes table, but not the problem. Each way around it just moves the difficulty somewhere else:

ApproachIdeaThe price
Write to only one place Drop Kafka and call the services directly over HTTP. The problem is still there: the other side holds the table and this side dies before recording it, and things drift apart anyway. You also lose the ability to resend while the other side is down.
Kafka as the only write order only sends to Kafka, then reads Kafka back itself to write MySQL. Only one write remains. The API cannot say "order created" right away. Checks that need the database (one pending order per guest) become much harder.
Read the binlog instead of an outbox table Debezium reads changes to the orders table directly. Receivers depend on order's internal table structure: change one column and the other side breaks. People usually keep the outbox table and let Debezium read that.
A shared transaction (2PC) One transaction spanning both MySQL and Kafka. Kafka cannot take part in this kind of transaction. The article Why saga instead of 2PC explains why dine would not want it anyway.
Accept the drift, reconcile later Just write and send, and periodically run a job that finds drifted orders and fixes them. Only suits work that is not urgent and where drifting is cheap. And that reconciliation job, if you think about it, is a slow outbox.

Kafka does have transactions, but they only cover things inside Kafka (read from this topic, write to that one), not MySQL. So the short answer is: as long as you write to two systems, you need a way to keep them from drifting apart, and the outbox is the simplest way, needing nothing but the database you already have.

The outbox and idempotency always come as a pair

dine uses Kafka so its services talk to each other asynchronously, through messages, instead of calling each other directly. (Strictly speaking, most of order's messages are commands, like "hold a table for order X", rather than pure "event-driven" style, where services only react to things that have already happened. But the problem here is exactly the same.)

Once services with their own databases talk through messages, you almost certainly need both an outbox and idempotency:

Without the outbox, work gets lost. Without idempotency, work gets done twice. With both, a message may travel several times, but the result is as if each piece of work happened exactly once. Idempotency is needed even without an outbox, because Kafka itself can redeliver a message to the receiver. The next article covers how dine's receiving side does it: Receiving a command twice without doing it twice.

See it for yourself

Open a sample order and press Show technical chart below the diagram. You will see the API answer as soon as it has written the order, while sending to Kafka is done afterwards by a different process (cmd/saga). Any recent order on the home page also has a timing bar split into segments like the table above, in the section "How order reacts to each reply".

Spotted a mistake, or have a question? Write to [email protected].

dine is a personal project about distributed systems, written in Go: orchestrated sagas, transactional outbox, Kafka. More articles.