Apache Kafka Streaming (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What are Idempotent Producers (`enable.idempotence=true`) and how do they eliminate duplicates?
senior LevelSystem Design
In Simple Words: When a producer sends a batch of records, the broker may successfully write them to disk, but the network ACK packet drops before reaching the producer. The producer assumes a failure and retries, creating duplicate records in the partition log.
Idempotence Mechanics (Enabled by default in modern Kafka):
- The broker assigns each producer a unique 64-bit Producer ID (PID) via
InitProducerId. - Each batch of records produced by that client includes a monotonically increasing Sequence Number per partition.
- When the broker receives a batch:
- If
incoming_seq == expected_seq: Commits data and increments expected sequence. - If
incoming_seq <= expected_seq - 1: Broker detects a duplicate retry! It acknowledges the write to the producer but drops the duplicate records from disk.
- If