Prevent duplicate Kafka messages with idempotent producers
Enable Kafka idempotent producers to discard duplicate retries within a producer session, with configuration trade-offs and verification steps.
19 Dec 2025, 23:54 UTC

A retry can charge a customer twice
Your payment service sends a Kafka record, the network times out, and the application retries. The broker already wrote the event, but the producer never received the acknowledgment. The retry creates a second copy. The consumer processes both, and the downstream system debits the account twice. This is at-least-once delivery: retries prevent message loss, but they can introduce duplicates.
Kafka idempotent producers address this for a single producer session. When enabled, the broker assigns each producer an ID and tracks sequence numbers per partition. If a retry arrives with a sequence number the broker has already seen, the broker discards the duplicate and still acknowledges success to the producer. The result is exactly-once delivery per partition for that producer session.
Configuration requirements
Enable idempotence in the producer configuration, not on the broker. The following settings belong in the producer properties file or client code:
enable.idempotency=true
acks=all
retries=2147483647
max.in.flight.requests.per.connection=5
Run this configuration inside the application that produces messages. You need permission to edit the application config and restart the service. enable.idempotency=true requires acks=all, and Kafka automatically limits max.in.flight.requests.per.connection to five when idempotence is on. A retries value greater than zero allows the client to recover from transient failures.
Before deploying, confirm the broker is version 0.11.0 or newer. Idempotent producers are not supported on 0.10.x or earlier brokers or clients.
Verify deduplication before production
Use a controlled test rather than assuming the config works:
- Start a test producer with idempotence enabled against a non-production topic.
- Send a record, then block the network before the acknowledgment returns so the client retries.
- Allow the retry to complete.
- Consume from the topic and confirm the consumer receives the record once for that producer session.
- Optionally raise broker log verbosity and check for duplicate sequence number rejections.
A practical check is to count consumed records for a known producer ID. If you see two copies with the same sequence number, idempotence is not active or the producer instance was recreated between attempts.
Trade-offs and limitations
- Idempotence is per partition and per producer session. If the producer crashes and the application starts a new producer instance, the new producer ID means earlier duplicates are not recognized.
- It does not provide atomic writes across multiple partitions or topics. Use Kafka transactions for that broader guarantee.
- Broker-side deduplication state adds a small latency and throughput cost. Benchmark high-throughput workloads before enabling it everywhere.
- Exactly-once processing still depends on the consumer and downstream system. Kafka can prevent duplicate writes, but it cannot prevent a consumer from acting twice on the same message if the downstream system lacks its own dedup.
Rollback
Because this changes producer behavior, rollback means removing enable.idempotency=true, redeploying the producer, and confirming that acks=all still meets your durability requirement. Monitor duplicate rates after the revert.
Actionable closing
Turn on idempotent producers for services where a duplicate event causes financial or state errors, such as payments or inventory updates. Verify with a retry test, benchmark throughput, and keep consumer-side deduplication as a safety net for downstream systems.