The Myth of Exactly-Once in Kafka: Chasing Duplicate Order Records

On the night of the big end-of-January sale, at 22:15, a message dropped in from the finance team: "1,847 orders show up as duplicates in reconciliation." Customers had placed a single order and made a single payment; but in the order records table and the accounting stream behind it, some orders were two rows. Some of the orders sent to restaurants had been prepared twice in the kitchen, and two couriers had been dispatched to the same address. In the war room, someone wrote on the whiteboard: "Let's check whether Kafka exactly-once was turned on." That sentence is the reason for this post. Because at the end of three weeks of digging, we would see that the duplicate records had nothing whatsoever to do with Kafka's "exactly-once" flag, and that the problem was in our own code at the edges.

The Narrow Corridor the Flag Protects

First, let me clarify the distinction everyone skips. Kafka's idempotent producer feature guarantees this: if the producer sends the same batch to the broker twice because of a network error, the broker looks at the sequence number and silently drops the copy. The transactional API, meanwhile, ties "writing to multiple partitions + committing offsets" into a single atomic package; this provides end-to-end uniqueness for read-process-write chains that stay inside Kafka (Kafka Streams-style). That's the whole corridor: Kafka to Kafka. If there's a database, a payment provider or an HTTP call at the end of the chain — and in real life, there always is — Kafka's guarantee ends at that boundary, and your engineering begins. Our duplicate orders were being born exactly at that boundary.

We started the dig with traces. The distribution of the duplicates was interesting: it wasn't random; it clustered by the minute. When we overlaid the cluster moments with the consumer group logs, the pattern emerged: every cluster coincided with a rebalance. When the sale-night traffic doubled, the HPA added new pods to the order processing service; with every pod added, the consumer group rebalanced, and with every rebalance, a batch of messages fell into the "processed but offset not committed" limbo. The service's flow was: receive the message, write the order to PostgreSQL, then commit the offset. If a rebalance landed right between the two, the new pod received the same message again and wrote the order again. Classic at-least-once behavior; it's in the textbook, it's on the exam, and in production it finds you at 22:15 at night.

Scaling triggered a rebalance; a message that had been written but whose offset hadn't been committed went to a new pod, and the order was recorded twice.
Classic at-least-once behavior surfaced in production at 22:15 at night.

The Wrong Cure: Turning Settings Into Heroes

In the first week, we wasted time on two wrong cures. The first was the "let's set processing.guarantee=exactly_once everywhere" campaign; but half of our flow wasn't Kafka Streams, and in the part that was, the problem wasn't there anyway. The second was sneakier: "let's commit the offset before processing the message, and the duplicates will stop." True, they stop — and data loss takes their place. If the rebalance lands in the same spot again, this time the order never gets written, and the customer doesn't get the food they paid for. Oscillating a setting between at-least-once and at-most-once doesn't solve the problem; it only picks which night you turn into a nightmare. While having this discussion with the team, the sentence I wrote on the whiteboard was: a delivery guarantee isn't a Kafka setting, it's an end-to-end property of the system, and it's only as strong as the weakest link in the chain.

The permanent fix had three legs. The first leg was making processing idempotent. We gave every order event an idempotency key at the point where it's produced — the customer's "confirm order" tap — and put a unique constraint on that key in the orders table. The consumer now writes using the INSERT ... ON CONFLICT DO NOTHING pattern; even if the same message arrives five times, only one row is created in the database. The second leg was tying the fate of the offset commit to the database write: we wrote the offset not to Kafka but next to the processed data, inside the same PostgreSQL transaction; on startup, the pod reads where it left off from the database and seeks to it. That way, the "written but not committed" limbo disappeared by definition. The third leg was turning on enable.idempotence and acks=all on the producer side without exception — this eliminated broker-level copies during network hiccups; in our measurements, these were only 6 percent of all duplicates, but getting them to zero was free.

Three legs: an idempotency key on the order event, storing the offset in the same transaction as the data, and producer idempotence settings.
A delivery guarantee isn't a Kafka setting; it's an end-to-end property of the system.

Deduplication Has Its Own Problems

It would be unfair to describe idempotency as "we added a unique constraint, done"; we fell into two traps. The first was the window problem: the key table for order events can't grow forever. Because we embedded the keys in the order record itself, we got off lightly here, but events without a table of their own, like payment notifications, needed a separate dedup table and a 7-day cleanup job. Why did we choose a 7-day window? We measured: the latest duplicate delivery we had seen arrived 41 hours apart (a DLQ reprocessing case), and 7 days left more than four times that as margin. The second trap is subtler: idempotent writes don't cover side effects. The order row stayed single, but the creation of the row triggered a "notify the restaurant" HTTP call, and the code sent the notification even in the ON CONFLICT case. That was the last remnant of the double preparations in the kitchen. We set the rule like this: every side effect is tied to the knowledge that "I actually created this row"; if the number of rows affected by the INSERT is zero, the flow quietly ends there.

We also tamed the rebalance frequency separately: with static group membership (group.instance.id) and a more tolerant session timeout, most pod restarts no longer trigger a rebalance; the cooperative sticky assignor smoothed out the transitions as well. Sale-night scaling still produces rebalances — that's unavoidable — but a rebalance is no longer a dangerous moment for us, just an ordinary event.

Don't assume we ruled out the transactional producer entirely: in the two places on the platform where there are genuine Kafka-internal chains — a Kafka Streams job that aggregates campaign points on the stream and the event enrichment pipeline — exactly_once_v2 is on, and there it really does its job. The extra cost we measured is an 8-12 percent drop in throughput depending on the commit interval, and a floor on end-to-end latency equal to the commit interval (100 ms in our case). That price is cheaper than building dedup infrastructure for those pipelines. So the rule is this: wherever your guarantee boundary ends, draw that line honestly; inside the boundary, use Kafka's tools, and outside it, use your own idempotency.

In Kafka-to-Kafka chains the transactional guarantee works; at external boundaries like the database and HTTP, idempotency is your job.
Inside the boundary, Kafka's tools; outside it, your own idempotency.

The Reconciliation Dashboard: The Most Honest Referee

The finish line for all this work wasn't set by code but by a Grafana panel. We turned the reconciliation finance did by hand into a continuously running comparison: the number of transactions coming from the payment provider and the number of rows in the orders table, side by side in 5-minute windows. If the difference exceeds 0.1 percent, an alarm fires. In the four weeks before the changes, this panel showed an average of 120-400 duplicate records a day, and thousands on campaign nights. In the first big campaign after the changes — with traffic up 180 percent — the number of duplicates: 0. Over the next six months we saw a total of 3 cases, all three caused by the external payment provider sending its webhook twice, and the dedup layer swallowed them all; they didn't even leave a trace on the panel, and we found them in the DLQ logs.

The lesson I take from this story is this: the answer to "Does Kafka have exactly-once?" is most often "yes, but not in your system." The flag works honestly within Kafka's own boundaries; outside them — your database, your HTTP calls, the second pide (Turkish flatbread) in the kitchen — it's your idempotency design that protects you. If you're wrestling with duplicate records, lost messages or consumers that "get weird when a rebalance happens," write to me through my contact page; I'll gladly share the query behind the reconciliation dashboard and our dedup schema. That panel has ended more debates than any line of code we've written.

The bill for the 1,847 duplicate orders on sale night, including refunds and operational costs, came to roughly 900,000 TL. The cost of the unique constraint: one index. In engineering, heroism is sometimes knowing when the most boring tool will do.