Scrivio Interview library
ScenarioIntermediate7 min read · updated 13 November 2026

An exactly-once Kafka Streams app aggregates order items and, inside a Processor, calls an email API to send an order-ready email when an order's last item arrives. After a rebalance during a deployment, some customers received the same email two or three times.

The team argues that exactly_once_v2 should have prevented this. Explain what happened and redesign it.

Exactly-once in Kafka Streams covers state, output records and offsets inside Kafka, not an HTTP call made from a processor. During the rebalance, open transactions were aborted and the new task owners reprocessed the same records, so the email call ran again each time. The fix is to have the Streams app only emit an OrderReady event, and let a separate read_committed consumer send emails with orderId as an idempotency key.

Why this question is asked

This scenario is common because exactly-once is easy to over-read: the setting is called exactly-once, so teams assume everything the app does happens once. Interviewers use it to check that we know where the guarantee ends and that we can design around it. In this answer, we will recap what exactly_once_v2 commits, replay the deployment step by step to see where the duplicates came from, look at why a dedup store inside the app does not fix it, and then redesign the flow so emails are sent once in practice, with a clear rule for the team.

1A Quick Recap of What Exactly-Once Covers

Under exactly_once_v2, each stream thread commits three things in one Kafka transaction: the output records it produced, the changes to its state stores (written to changelog topics) and the offsets of the input records it consumed. If the transaction commits, all three are visible; if it aborts, none are, and the input records are processed again from the last committed offsets.

Notice the word again. Exactly-once does not mean each record is processed once. It means the effects of processing that Kafka can see are committed once. Records are routinely processed more than once; the aborted attempts simply leave no visible trace inside Kafka. A call to an email API is not inside Kafka, so it leaves a trace every time: a sent email.

2Reproducing the Problem

Here is the kind of processor the team wrote. It counts items per order in a store and sends the email when the last item arrives.

Java
public void process(Record<String, OrderItem> r) {
int seen = store.get(r.key()) == null ? 1 : store.get(r.key()) + 1;
store.put(r.key(), seen); // goes to the changelog, inside the transaction
if (seen == r.value().totalItems()) {
emailClient.sendOrderReady(r.key()); // HTTP call, outside the transaction
}
}
// normal run: one email
// abort + reprocess: store rolled back to seen = 2, item 3 arrives again -> second email

The store.put is safe: if the transaction aborts, the changelog record is aborted and the store is rebuilt without it. The sendOrderReady call is not: the HTTP request has already reached the email provider, and no Kafka abort can unsend it.

3What Happened During the Deployment

A rolling deployment stops and starts instances, and each change of membership triggers a rebalance that moves tasks between instances. Suppose the instance owning the task for partition 3 has processed the last item of order 1001, called the email API, and has not yet committed its transaction when it is told to give up the task or is shut down. If the task migrates and the old instance's producer is fenced by the new owner, it sees TaskMigratedException, wrapping ProducerFencedException in many cases, and its open transaction is aborted.

The new owner restores the store from committed changelog records, in which order 1001 still has two of three items, and starts reading from the last committed offset. It processes the last item again, the count reaches three again, and it sends the email again. With several restarts in one deployment, the same order can go through this two or three times, which matches what customers saw.

Step throughHow one order got two emails
Old instanceEmail APIKafkaNew instance
item 3 of order 1001: count = 3
send order-ready email
202 accepted
rebalance: producer fenced, transaction aborted
restore store (count = 2), read from committed offset
item 3 again: count = 3
send order-ready email again

7 messages in order. Play them, or step through them one at a time.

The store and offsets roll back; the email does not.

4Why a Dedup Store Inside the App Does Not Help

The natural reaction is to remember sent emails in another state store and check it before sending. The problem is that this store is part of the same transaction. When the transaction aborts, the record saying the email was sent is rolled back along with everything else, while the email itself stays sent. The new owner checks the store, finds nothing, and sends again. Writing the marker before the call does not change this, because the marker is only durable once the transaction commits, and the call happens before the commit. Any state that lives inside the Streams transaction is the wrong place to record an external effect.

5Redesign: Emit an Event, Send From a Consumer

We split the job in two. The Streams app keeps doing what Kafka transactions protect: it counts items and, when an order is complete, writes an OrderReady record to an order-ready topic, keyed by orderId. Because that write is inside the transaction, each OrderReady is committed exactly once. A separate email service consumes order-ready with isolation.level=read_committed, so it never sees records from aborted transactions, and sends the email using orderId as an idempotency key.

Java
// Streams side: no external calls
if (seen == r.value().totalItems()) {
ctx.forward(r.withValue(new OrderReady(r.key()))); // committed exactly once
}
// Email service, read_committed consumer
int inserted = jdbc.update(
"INSERT INTO sent_email(order_id, status) VALUES (?, 'SENDING') ON CONFLICT (order_id) DO NOTHING",
orderId); // 1 the first time, 0 on any repeat
if (inserted == 1) {
provider.send(orderReadyEmail(orderId), idempotencyKey(orderId));
jdbc.update("UPDATE sent_email SET status = 'SENT' WHERE order_id = ?", orderId);
}
Step throughThe redesigned flow
  1. Kafka Streams appcounts items, emits OrderReady inside its transaction
  2. order-ready topicone committed record per completed order
  3. Email service (read_committed)inserts sent_email row, unique on order_id
  4. Email providerreceives an idempotency key per order

4 layers, from the top down. Step through them one at a time.

The consumer itself is at-least-once: it may see the same OrderReady again after its own restart. That is why it records the send in a table with a unique constraint on order_id before calling the provider; the second attempt inserts nothing and skips the call. Many email providers also accept an idempotency key and discard a repeated request with the same key, which covers the case where we crashed after the call but before updating our row.

There is still one honest gap: if we crash after the provider received the request but before we know the result, our row says SENDING. For an email that must go at most once, we do not blindly resend; we ask the provider for the message status by our key and only resend if it says nothing was sent. For an email where a rare duplicate is acceptable, at-least-once with the key is enough.

6Making It a Team Rule

One fix is not enough if the next processor repeats the pattern. We write a design rule: any external call inside a stream processor is at-least-once at best, so it must either be idempotent on the other side or move behind a topic. We add it to code review, and where possible a test that kills an instance mid-transaction under load and counts the external calls. That test would have shown the duplicate emails before customers did.

7Follow-up questions

Could a dedup store inside the Streams app solve this? What breaks?

No. The dedup store's writes are part of the same Kafka transaction as the offsets, so when the transaction aborts, the marker that says the email was sent is rolled back while the email stays sent. The new task owner finds no marker and sends again. A dedup record only protects an external effect if it is durable before or with that effect, which a Streams store under exactly-once is not.

Why must the email consumer use read_committed?

Because the OrderReady records from aborted transactions are still in the topic. A read_uncommitted consumer would see an OrderReady that the Streams app rolled back, and then the reprocessed one, and would rely entirely on its own dedup to avoid sending twice.

Is it acceptable to keep the HTTP call in the processor if the provider supports idempotency keys?

It removes duplicate emails, but the call still blocks the stream thread, ties the app's throughput and rebalances to the provider's latency, and repeats on every reprocessing. Moving it behind a topic keeps the stream app fast and isolates failures, so it remains the better design.

8Conclusion

In this answer, we saw that exactly_once_v2 commits Kafka-internal effects once while records themselves may be processed several times. A rebalance during the deployment aborted open transactions, the new owners reprocessed from committed offsets, and the email call inside the processor ran again. A dedup store inside the app is rolled back with the transaction, so it cannot help. The redesign emits an OrderReady event, sends from a read_committed consumer that records each send under a unique orderId and passes an idempotency key, and makes external calls in processors a reviewed exception.

What to say in the interviewAbout 90 seconds aloud

Open with

Exactly-once in Kafka Streams is about effects inside Kafka: the state store changes, the output records and the consumed offsets commit together once. It does not mean each record is processed once.

Then, if they want more

When a transaction aborts, everything inside Kafka rolls back and the records are processed again, so a side effect made during processing, like the HTTP call to the email API, simply happens again.

During the deployment, rebalances moved tasks and fenced the old producers, so open transactions aborted with errors like TaskMigratedException, and the new owners restored the store from the changelog and reprocessed the last item from the committed offset, sending the email two or three times.

A dedup store inside the app would not help, because it is rolled back with the same transaction. My redesign is to have the Streams app only emit an OrderReady event to a topic, which is committed exactly once, and a separate consumer with read_committed sends the email.

That consumer inserts a row keyed by orderId with a unique constraint before calling the provider and passes orderId as an idempotency key, and if the outcome of a call is unknown it checks the status rather than resending.

And I would make it a team rule: any external call inside a stream processor is at-least-once at best, so it has to be idempotent or go behind a topic.

About this answer

Last updated
13 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
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 "The team argues that exactly_once_v2 should have prevented this. Explain what happened...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/0ca78e4d01/