Quick revision
The short answer
The database commit and the Kafka send are two separate operations, so either one can succeed while the other fails: we lose the event for a saved order, or publish an event for an order that was rolled back. Because send() runs inside the method, the event can even reach consumers before the order is committed. The transactional outbox fixes this by writing the event as a row in an outbox table in the same database transaction as the order, and a relay publishes those rows to Kafka afterwards, by polling the table or with change data capture such as Debezium. Delivery becomes at-least-once, so consumers must be idempotent.
What to remember
In this answer, we saw that saving an order with JPA and calling kafkaTemplate.send() in the same @Transactional method uses two separate systems, giving phantom events, lost events and events that arrive before the data. The transactional outbox writes the event as a row in the same database transaction, and a relay publishes it afterwards by polling or CDC. Delivery is at-least-once, so consumers must be idempotent.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The database commit and the Kafka send are two separate operations, so either one can succeed while the other fails: we lose the event for a saved order, or publish an event for an order that was rolled back. Because send() runs inside the method, the event can even reach consumers before the order is committed. The transactional outbox fixes this by writing the event as a row in an outbox table in the same database transaction as the order, and a relay publishes those rows to Kafka afterwards, by polling the table or with change data capture such as Debezium. Delivery becomes at-least-once, so consumers must be idempotent.
Why this question is asked
Saving something and then announcing it is the most common thing an event-driven service does, and the most common place it goes wrong. The bug is invisible in tests, because both operations usually succeed, and shows up in production as an order with no invoice or an invoice with no order. Interviewers ask about it because the outbox pattern is the standard fix and they want to see that we understand why it works. In this answer, we will recap what @Transactional covers, walk through the ways the naive code fails, build the outbox, see how the relay publishes it, and explain why consumers must still handle duplicates.
1A Quick Recap of What @Transactional Covers
@Transactional on a Spring method starts a database transaction before the method runs and commits it when the method returns, or rolls it back if it throws. It covers the resources that the transaction manager controls, here the database connection used by JPA. Everything else the method does happens outside that transaction, including network calls such as a Kafka send.
KafkaTemplate.send() is also asynchronous: it adds the record to the producer's buffer and returns a CompletableFuture straight away, and the record is sent to the broker shortly after, whether or not our database transaction has committed.
2Three Ways the Naive Code Fails
The first failure is a phantom event. Kafka receives OrderCreated, then the commit fails, for example because a constraint is violated at flush time or the database connection drops. The order does not exist, but the billing service has already received an event about it and may charge the customer.
The second is a lost event. The commit succeeds, but the send fails: the broker is unreachable, or the application crashes before the producer flushed its buffer. The order exists, and nobody downstream will ever hear about it. Because send() is asynchronous and nobody checks the future, this failure is usually not even logged.
The third is subtler: the event arrives before the data. Even when both succeed, the record can reach a consumer before the commit. A consumer that reacts by calling back the order service to fetch the order gets a 404, because the row is not visible yet.
6 messages in order. Play them, or step through them one at a time.
The event left before we knew whether the order would exist.
Making the producer transactional and letting Spring synchronise the Kafka transaction with the database transaction narrows these windows, because Spring commits one right after the other. It does not close them: there are still two commits, and a crash between them leaves one done and the other not. Two separate systems cannot commit atomically without a distributed transaction protocol, which Kafka does not support with databases.
3The Outbox: One Transaction, Two Rows
The outbox pattern removes the second system from the transaction. Instead of sending to Kafka, the method inserts the event as a row in an outbox table in the same database, in the same transaction as the order. Now there is only one commit: either the order and its event are both saved, or neither is.
Notice that the event can no longer leave before the commit, because it is just a row that nobody outside can see until the commit. The phantom event and the early event are gone, and the lost event is turned into a delayed event, because the row waits safely until it is published.
4The Relay: Getting Rows to Kafka
A separate component, the relay, reads committed outbox rows and publishes them. The simple version polls: every few hundred milliseconds it selects unpublished rows in id order, sends each to Kafka with the aggregate id as key, waits for the broker's acknowledgement, and marks the rows as published. The alternative is change data capture (CDC): a tool such as Debezium reads the database's transaction log, sees each inserted outbox row as it commits, and publishes it to Kafka, with no polling queries at all. Both work; polling is simpler to start with, CDC has lower latency and no query load but needs Kafka Connect and Debezium to run.
- Insert order and outbox rowone database transaction
- Commitboth rows visible, or neither
- Relay reads the new outbox rowpolling or CDC
- Publish to Kafkakey = order id, wait for ack
- Mark row publishedor delete it
- Consumer processes OrderCreatedskips it if already seen
6 steps in order. Play them, or step through them one at a time.
5Why Consumers Must Be Idempotent
There is still one gap, but it is a safe one. If the relay publishes a row and crashes before marking it published, it will publish the same row again after restarting. So delivery is at-least-once: every event arrives, some of them twice. Consumers must therefore be idempotent, meaning processing the same event twice has the same effect as once. The usual way is to record each processed event id, or the outbox row id carried in a header, in the consumer's database in the same transaction as its own changes, and skip events it has already seen. A duplicate is harmless when it is detected; a lost event never is.
6Follow-up questions
Could I just publish the event in an @TransactionalEventListener after commit?
That fixes the phantom and early events, because the send happens only after the commit. It does not fix the lost event: if the application crashes or Kafka is down after the commit, the event is gone, since it only existed in memory. The outbox keeps it in the database until it is delivered.
Does the outbox table slow down the order service?
It adds one insert per event in a transaction that already writes to the database, which is usually a small cost. The relay's load is separate. The main operational concern is keeping the table from growing, by deleting or archiving published rows.
Which is better, polling or CDC?
Polling needs nothing new and is easy to reason about, and it is fine for moderate volumes. CDC gives lower latency and no polling queries, and suits high volumes, but adds Kafka Connect and Debezium to operate. Many teams start with polling and move to CDC when volume or latency demands it.
7Conclusion
In this answer, we saw that saving an order with JPA and calling kafkaTemplate.send() in the same @Transactional method uses two separate systems, giving phantom events, lost events and events that arrive before the data. The transactional outbox writes the event as a row in the same database transaction, and a relay publishes it afterwards by polling or CDC. Delivery is at-least-once, so consumers must be idempotent.
Open with
The problem is that @Transactional only covers the database. kafkaTemplate.send is outside that transaction, and it's asynchronous, so the record goes to Kafka as soon as the producer flushes, possibly before the commit.
Then, if they want more
That gives three failures. If the commit then fails, consumers have a phantom event for an order that doesn't exist. If the commit succeeds but the send fails or the app crashes before flushing, the event is lost, often without even a log line.
And even when both work, a consumer can get the event before the order is visible and fail to look it up. Kafka transactions synchronised with the database narrow the window but there are still two commits. The transactional outbox fixes this by not talking to Kafka in the business transaction.
Instead I insert the event as a row in an outbox table, in the same transaction as the order, so there's one commit and both rows exist or neither does.
A separate relay publishes committed outbox rows to Kafka with the order id as key, either by polling the table and marking rows published after the broker acks, or by change data capture with Debezium reading the transaction log.
The relay can crash after publishing but before marking, so delivery is at-least-once, and consumers must be idempotent, typically by recording processed event ids in their own transaction.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An order service saves an order with JPA and then calls...": https://scrivio-interview-library.netlify.app/spring-boot/event-driven-patterns/147c0dfb0b/