Skip to content
Kafka Is Fun

MediumData Quality

Message Duplication on Producer Retry

Symptoms

  • The same business event appears more than once downstream
  • Database unique-key violations
  • Counts in analytics are higher than the source

Detection

Compare record counts with the source. Check whether the producer sets enable.idempotence=false. NETWORK_EXCEPTION (the server disconnected before a response was received) is a retriable error, so the producer sends the record again after it.

Challenge mode: diagnose the root cause from the symptoms before reading the answer.

Root cause

After a network error the producer cannot tell whether the broker wrote the record before the connection failed, so it sends it again. Without idempotence a retry can write the record twice. enable.idempotence defaults to true, but it only covers retries within a single producer session; a restarted producer, or an application that resends, is not covered. Consumers that read a record twice (at-least-once) produce the same symptom.

Immediate actions

  1. Check whether the downstream system can ignore a repeat (an upsert instead of an insert)
  2. Check whether each record carries a unique ID the consumer can use to spot a repeat
  3. Make sure enable.idempotence is not turned off

Permanent fix

Keep enable.idempotence=true (the default). For atomic writes across partitions and recovery across producer restarts, use transactions (transactional.id). Make consumers tolerate redelivery.

Prevention

Design consumers to be idempotent, because Kafka guarantees at-least-once delivery by default. Test duplicate handling explicitly.

Reproduce it in the simulator

Watch the producer pipeline: when a send hits a stale leader it retries against the new one.

Open the simulator to try it.

The interview angle

How do you prevent duplicates? enable.idempotence prevents duplicates from producer retries within one session; transactions cover recovery across sessions; idempotent processing covers the consumer side.

Sources

Checked against Kafka 4.2 documentation on 11 October 2026. Draft: not yet reviewed by a Kafka specialist, and releases move on, so confirm against the current documentation before you act on it.