Transactional outbox writes
You can commit an outbox row with its order change, giving the relay a durable record to publish without a dual-write gap.
Learning outcomes
- Explain why separate MySQL and Kafka writes create a dual-write failure window.
- Describe how the transactional outbox records an order change and its event in one database transaction.
- Trace how a relay claims unsent outbox rows, publishes them and marks them sent.
- Explain how FOR UPDATE SKIP LOCKED coordinates multiple relays sharing an outbox table.
- Identify the duplicate-delivery window and why consumers need idempotent processing.
When an order moves from PLACED to ACCEPTED, MySQL must record the new status and an order event must reach Redpanda. The API has to update MySQL and publish to Kafka, but these two systems do not share a database transaction. A crash between those operations can leave the order updated without notifying the rest of the system.
The solution is to record the event in MySQL alongside the order change. A separate relay publishes that durable record to Kafka.
The dual-write failure window
The dual-write problem occurs when one application operation must update two independent systems. Here, the API must commit a MySQL transaction and publish a Kafka record.
Neither ordering of those operations is safe.
Commit MySQL, then publish to Kafka.
Suppose the order transaction commits successfully. The order now has status ACCEPTED, but the API process crashes before publishing the event. MySQL contains the correct state, but the notifier never receives the transition.
Publish to Kafka, then commit MySQL.
Suppose Redpanda accepts the event, but the MySQL transaction subsequently fails. Kafka now contains an event for a transition that never committed.
You cannot fix either failure by putting a retry around the network call. After a crash, the application might not know which operation succeeded.
The transactional outbox
The transactional outbox moves the responsibility for recording the event into MySQL. Instead of publishing directly from the order handler, the handler inserts an outbox row in the same database transaction as the order change.
For an order transition, the transaction contains three related writes:
- Update the
ordersrow. - Insert the corresponding
order_eventsrow. - Insert an
outboxrow containing the message to publish.
A Transaction gives these writes one commit boundary. Either all three succeed, or none of them becomes committed database state.
The Go database/sql transaction follows this pattern:
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
// Perform the related database writes using tx.
if err := tx.Commit(); err != nil {
return err
}Every related write must use tx, not db. Calling db.ExecContext for one of the writes would execute it outside this transaction.
Notice that the deferred rollback is safe after a successful commit. Once committed, the transaction is finished and there is nothing left to roll back.
The outbox row
The outbox table stores the information the relay needs to publish a record without reconstructing the event from the order's current state.
The relevant columns are:
| Column | Purpose |
|---|---|
id | Identifies the outbox row and determines its publication order. |
topic | Kafka topic that receives the record, such as order-events. |
key_ | Kafka record key, the decimal order ID for order events. |
payload | JSON value containing the event data. |
sent_at | Time the row was marked as successfully published, or NULL while unsent. |
For example, an outbox payload for an order transition can contain:
{
"order_id": 42,
"event_id": "<event-id>",
"from": "PLACED",
"to": "ACCEPTED"
}The event_id comes from the corresponding order_events row. It is not the HTTP idempotency key used when a client creates an order.
The order change and its outbox row commit together. If the transaction rolls back, neither becomes committed. If it commits, the relay has a durable record of the event that still needs publication.
A committed outbox row does not mean Kafka has received the record. It means the system has recorded its obligation to publish it.
The relay lifecycle
The relay runs in the background and periodically checks MySQL for unsent rows. In the reference solution, it polls every 500 ms.
Each publishing cycle has five stages:
- Begin a MySQL transaction.
- Select a bounded batch of unsent rows and lock them.
- Publish the records to their stored Kafka topics.
- Set
sent_aton the successfully published rows. - Commit the transaction.
The relay uses the stored topic, key and payload. It does not rebuild the event by looking at the order's current status. An order may have moved through several transitions since an earlier event was created.
The claim query orders rows by their ID:
SELECT id, topic, key_, payload
FROM outbox
WHERE sent_at IS NULL
ORDER BY id
LIMIT ?
FOR UPDATE SKIP LOCKED;ORDER BY id preserves the order of the selected rows. LIMIT bounds the batch, while sent_at IS NULL excludes rows already marked as sent.
The relay publishes each record using its stored topic, key and payload. For order events, the decimal order ID as the key routes those records to the same Kafka partition, where their order is preserved.


Row locking and concurrent relays
MySQL row locks prevent two relay instances from claiming the same locked outbox row at the same time.
The important part is SKIP LOCKED. Without it, a relay selecting a row locked by another transaction could wait for that transaction to finish. With it, the query skips the locked row and can return later unlocked rows.
Consider two unsent rows for the same topic:
- Row A has payload sequence
0. - Row B has payload sequence
1.
If another transaction holds a lock on row A, the relay can publish row B without waiting. Once the transaction releases A, a later relay cycle can claim and publish it.
This is the behaviour the W3B6 lock test checks.
The required exercise runs one relay per API process. The optional multi-relay-outbox exercise (W3O2) starts two API instances whose relays share the same outbox table. FOR UPDATE SKIP LOCKED lets those relays claim different available rows without waiting for rows locked by the other relay.
There is an important distinction between row ownership and event ordering. SKIP LOCKED coordinates which relay can claim a row. It does not, by itself, guarantee that two different relays publish different events for the same order in their original order. Strict ordering across multiple concurrent publishers requires an additional ordering or ownership strategy.
Publication and the at-least-once window
The relay publishes before setting sent_at. This ordering prevents an event from being permanently lost merely because the process crashes.
However, it creates a possible duplicate-delivery window.
Suppose Redpanda accepts an event, but the API process crashes before the MySQL transaction commits. The outbox row remains unsent because its sent_at update was not committed. When a relay processes the row again, it publishes the event again.
This is At-least-once delivery publication. Every committed event remains available for publication, but some events can be published more than once.
The opposite ordering would risk losing events:
- Set
sent_atand commit MySQL. - Crash before publishing to Kafka.
- The relay no longer selects that row because it is marked sent.
The event would never reach Kafka.
Consumers must therefore handle duplicates. The notifier uses the stable event_id to recognise an event it has already processed. This is why the outbox guarantees durable publication intent, not exactly-once delivery.
Housekeeping and alternatives
Sent outbox rows are no longer pending work, but they can accumulate over time. A housekeeping job can delete rows whose sent_at is older than a few days, processing them in small batches to avoid holding long database locks.
Never remove unsent rows as part of this cleanup.
Polling is the approach used in this course. A more advanced alternative is change data capture (CDC). Tools such as Debezium can read committed outbox inserts from the MySQL binlog and publish them to Kafka, removing the need for periodic polling.
CDC changes how the relay discovers new rows. It does not remove the need to handle duplicate delivery.
Connecting the outbox to the relay
The exercise Outbox relay to order-events connects the durable MySQL record to the Kafka stream. W3B4 and W3B5 already create outbox rows with their order changes. The relay must publish those rows to their stored topics, using the decimal order ID as the key for order events.
The tests check the order transitions from PLACED through ACCEPTED to PREPARING, the sent_at updates, the ascending outbox ID order and the behaviour when another transaction holds a row lock.
The distinction to keep clear is that committing an outbox row records the obligation to publish. The relay fulfils that obligation, while the notifier handles the possibility of receiving an event more than once.
Check your understanding
The API commits an order transition in MySQL and crashes before publishing directly to Kafka. What is the result without an outbox?
References
Everything above is explained on this page; these are where it comes from (MySQL 8.4, franz-go v1.22, Redpanda).