How does Kafka's idempotent producer prevent a retry from creating a duplicate record, mechanically?
When enable.idempotence=true (the default since Kafka 3.0), the producer gets a producer id (PID) from the broker at startup, and every batch it sends for a given partition carries that PID plus a sequence number that increments by one per batch. If a broker appends a batch but the acknowledgement is lost on the way back, the producer retries the same batch with the same sequence number — the broker recognises it's already seen that (PID, partition, sequence) combination and discards the duplicate instead of appending it again, while still reporting success to the producer. It only covers the client library's own retries within one producer session; if the application itself calls send() twice for the same logical event, or the producer process restarts, those look like genuinely new records to the broker.