Durability & Ordering
acks, min.insync.replicas, retries, idempotence, and what 'written' means
When is a record "written"? The answer is a setting, and it's the most important decision a producer makes. The book's Reliable Data Delivery chapter is the reference; here we test each guarantee against the cluster.
acks
acks | The leader replies after... | A record can be lost if... |
|---|---|---|
0 | Nothing: the producer doesn't wait | Anything goes wrong at all |
1 | The leader wrote it to its log | The leader dies before followers copy it |
all (-1) | All in-sync replicas have it | All in-sync replicas are lost at once |
acks=all is the default in Kafka 3+ clients and in librdkafka. Note what
it really means: all replicas currently in the ISR, which might be only
the leader if the others fell behind. That's why it needs a partner.
min.insync.replicas
min.insync.replicas (2 on this cluster) is the minimum ISR size for an
acks=all write to be accepted. With 3 replicas and min ISR 2, every
acknowledged record is on at least two brokers, so any single broker can
fail without losing it.
Tested on the lab cluster, with topic demo (RF 3, min ISR 2):
# one broker stopped: ISR shrinks to 2, writes continue
docker stop kafka-3
Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2
echo x | kafka-console-producer.sh ... --request-required-acks all # OK
# two brokers stopped: ISR is 1, below min.insync.replicas
docker stop kafka-2
Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1 Elr: 2
echo y | kafka-console-producer.sh ... --request-required-acks all
WARN ... Error: NOT_ENOUGH_REPLICAS # refused
echo z | kafka-console-producer.sh ... --request-required-acks 1 # OK: acks=1 ignores min ISRThat's the trade in one experiment: acks=all + min.insync.replicas=2
chooses consistency over availability. It refuses writes rather than
accept ones that live on a single disk. acks=1 keeps accepting, and a
second failure would lose those writes.
Setting min.insync.replicas equal to the replication factor (3 and 3)
means any single broker restart blocks all acks=all writes. RF 3 with
min ISR 2 is the standard choice: tolerate one failure without
blocking, and never acknowledge a write held by only one broker.
Retries and ordering
Transient errors (a leader election, a slow broker) are retried
automatically. Retries are effectively unlimited by default; the real
bound is delivery.timeout.ms (2 minutes), the total time a record may
spend being sent before its callback reports failure.
Retries create a classic problem: with several requests in flight to a partition, batch 1 can fail and be retried after batch 2 succeeded, reversing their order. And a request that succeeded but whose response was lost gets retried, writing a duplicate.
The idempotent producer
enable.idempotence=true (default in modern clients when acks=all)
fixes both. The producer gets a producer id, and every batch carries
a per-partition sequence number. You saw these in kafka-dump-log.sh:
baseOffset: 0 lastOffset: 4 count: 5 baseSequence: 0 lastSequence: 4 producerId: 2001 producerEpoch: 0The leader remembers the last sequences per producer and partition. A retried batch with an already-seen sequence is acknowledged without being written again; one that arrives out of order is rejected and resent in order. Result: no duplicates and no reordering from retries, with up to 5 requests in flight.
Idempotence protects against duplicates within one producer
session. If your application crashes and restarts, the new producer
gets a new id, and records it re-sends (because the app didn't know the
first attempt succeeded) are duplicates. Surviving restarts needs a
transactional.id, covered in
Transactions, or
idempotent consumers.
The full durability checklist
Durability needs every link in the chain:
- Topic: replication factor 3,
min.insync.replicas=2,unclean.leader.election.enable=false. - Producer:
acks=all, idempotence on, handle delivery errors (log, alert, retry at the app level, or fail the request). - Producer lifecycle:
flush()before shutdown. - Consumer: commit offsets only after processing (next chapter).
Skip any one and "durable" stops being true somewhere.
Lab
- Reproduce the experiment above with
demo: stop one broker, then two, producing withacks=allandacks=1each time. - While two brokers are down, describe the topic and note the
Elrcolumn. Then start them again and watch the ISR return to 3. - Run
kafka-producer-perf-test.shwithacks=1andacks=alland compare latency percentiles.