Quick revision
The short answer
The duplicates come from at-least-once delivery during rolling deploys: when a consumer stops, its partitions move to another instance, which restarts from the last committed offset, so records whose email was already sent but whose offset was never committed are delivered and emailed again. We shrink the window by committing promptly, shutting down gracefully and using cooperative rebalancing, and we close most of the rest by recording each event id in a sent-emails table with a unique constraint before calling the provider, accepting a rare duplicate for the crash window that remains.
What to remember
In this answer, we traced duplicate emails to at-least-once delivery during rolling deploys: records processed after the last committed offset are delivered again to the partition's new owner. We reduced redelivery with per-record commits, graceful shutdown and cooperative or broker-side rebalancing, made the send idempotent with a sent-emails table keyed by event id, explicitly chose to retry stuck SENDING rows because a rare duplicate shipping email beats a missing one, and added a metric to watch the duplicate rate.
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 duplicates come from at-least-once delivery during rolling deploys: when a consumer stops, its partitions move to another instance, which restarts from the last committed offset, so records whose email was already sent but whose offset was never committed are delivered and emailed again. We shrink the window by committing promptly, shutting down gracefully and using cooperative rebalancing, and we close most of the rest by recording each event id in a sent-emails table with a unique constraint before calling the provider, accepting a rare duplicate for the crash window that remains.
Why this question is asked
Duplicate emails after every deployment are one of the most common complaints about event-driven systems, and they are not a bug in Kafka. They are what at-least-once delivery looks like when the side effect cannot be undone and the provider cannot deduplicate. Interviewers ask this to see whether we can connect a deployment pattern to consumer offsets, and whether we can design a local idempotency guard that is honest about its remaining gap. In this answer, we will see where the duplicates come from, reduce redelivery during deploys, make the email send idempotent, decide what to do with the crash window, and add a metric.
1Where the Duplicates Come From
A Kafka consumer group shares a topic's partitions among its instances, and each instance commits an offset per partition to say how far it has got. Spring Kafka's listener container commits after processing, by default once per batch of records returned by poll(), which can be up to max.poll.records, 500 by default. So at any moment there may be hundreds of records that have been processed, with emails sent, but whose offsets are not yet committed.
During a rolling deploy, each old pod is stopped. Its partitions are revoked and assigned to another instance in a rebalance, and the new owner starts reading from the last committed offset. Every record processed after that commit is delivered again, and because the provider has no idempotency key, every one of them sends another email. With the classic eager rebalancing protocol, every rebalance revokes all partitions from all members, so even instances that are not being restarted pause and may hand over work. A deployment with 6 pods causes several rebalances in a row, which is why some customers get the email two or three times.
6 messages in order. Play them, or step through them one at a time.
The emails went out but the offset 150 was never committed by pod 1.
2Reduce Redelivery
The first step is to make the uncommitted window small. Committing after each record, AckMode RECORD, or with small batches means that at most a record or two is in doubt at any time, at the cost of more commit requests, which is cheap at email volumes. Next, the shutdown must be graceful: when Kubernetes sends SIGTERM, the container should stop polling, finish the record in progress and commit before the process exits. Spring Kafka commits pending offsets when partitions are revoked, but only if the pod is given enough time; the container's shutdown timeout and the pod's terminationGracePeriodSeconds must be longer than the slowest email send.
Finally, we stop unaffected partitions from moving. The cooperative sticky assignor revokes only the partitions that actually change owner, instead of all of them, and Kafka 4's new consumer group protocol (enabled with group.protocol=consumer) moves rebalancing to the broker and is incremental by design.
These changes take duplicates from hundreds per deploy to a handful, but they cannot reach zero: a pod can still be killed between sending the email and committing the offset.
3Make the Send Idempotent Locally
Since the provider cannot deduplicate, we do it ourselves. Before sending, we try to insert the event id into a table with a unique constraint and the status SENDING. If the insert fails because the id exists, we look at its status: SENT means skip. After the provider accepts the email, we update the row to SENT.
Now a redelivered record finds the row with status SENT and does nothing. Note that the event id must come from the event itself, OrderShipped's own id, not from the Kafka offset, because the same event can be produced twice upstream with different offsets.
4Decide What a Stuck SENDING Row Means
One window remains. If the pod dies after the provider accepted the email but before the update to SENT, the row stays in SENDING. When the record is redelivered, we cannot tell whether the email went out. We must choose: retry SENDING rows older than, say, five minutes, which risks a rare duplicate, or abandon them, which risks a rare missing email.
For a shipping notification, a rare duplicate is clearly better than a customer not knowing their parcel is on its way, so we retry. For something like a marketing email, abandoning might be acceptable. The point is to make this choice explicitly and write it down, because the window can be made small but never zero when the provider offers no idempotency key.
- Insert event id as SENDING succeeded?YesSend the emailNoRead the existing row
- Existing row is SENT?YesSkip and commitNoSENDING: check its age
- SENDING older than 5 min?YesRetry: rare duplicate acceptedNoAnother pod is working: skip
- Mark SENT, commit offsetduplicate window is now tiny
4 steps in order. Play them, or step through them one at a time.
5Measure It
Finally, we count how often the guard triggers. Every time a record arrives for an event id that already exists, we increment a metric such as email_duplicate_events_total, tagged with whether the row was SENT or SENDING. During a normal day it should be near zero; it will rise slightly during deploys. An alert on an unusual rate catches the next problem, such as a misconfigured shutdown, before customers report it.
6Follow-up questions
Why not use Kafka transactions to get exactly-once?
Kafka transactions make a read-process-write loop atomic within Kafka, but the email provider is outside Kafka and cannot take part in the transaction. The email could be sent and then the transaction aborted. So transactions do not help here; local idempotency does.
How long should rows stay in the sent_email table?
At least as long as the longest realistic redelivery or replay, including manual replays from a dead-letter topic. Thirty days is a common choice for notifications, with a scheduled job deleting older rows.
Could we deduplicate in memory instead of a table?
Not reliably. The duplicates happen exactly when partitions move to another pod, whose memory does not contain the ids the old pod saw. The guard has to live in shared, durable storage.
7Conclusion
In this answer, we traced duplicate emails to at-least-once delivery during rolling deploys: records processed after the last committed offset are delivered again to the partition's new owner. We reduced redelivery with per-record commits, graceful shutdown and cooperative or broker-side rebalancing, made the send idempotent with a sent-emails table keyed by event id, explicitly chose to retry stuck SENDING rows because a rare duplicate shipping email beats a missing one, and added a metric to watch the duplicate rate.
Open with
The consumer is at-least-once, so the duplicates come from redelivery. Spring Kafka commits after a batch by default, so at any time there can be many records whose emails went out but whose offsets aren't committed.
Then, if they want more
In a rolling deploy each pod is stopped, its partitions move to another instance in a rebalance, and the new owner starts from the last committed offset, so those emails are sent again; with eager rebalancing every deploy step moves all partitions, which explains two or three copies. I would attack it in two layers.
First, reduce redelivery: commit per record or in small batches, shut down gracefully so the in-flight record finishes and offsets are committed on revocation, with a termination grace period longer than the slowest send, and use cooperative sticky assignment or Kafka 4's new consumer group protocol so unaffected partitions don't move.
Second, make the side effect idempotent locally, because the provider has no idempotency key. Before sending, I insert the event id into a sent-emails table with a unique constraint and status SENDING; after the provider accepts, I mark it SENT; a redelivered event that finds SENT is skipped.
There is still a window if the pod dies after sending but before marking SENT. I would decide that explicitly; for shipping emails I'd retry old SENDING rows and accept a rare duplicate rather than a lost notification. Finally, I'd count repeated event ids as a metric and alert when the rate rises.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Explain where the duplicates come from and how you would reduce them to nearly zero.": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/6a9dd74c0e/