Producers and Consumers
11. What does the producer acks setting control?
acks sets how many replicas must have a write before the producer counts it as sent. The default is all.
acks=0: the producer does not wait. Fastest, and writes can be lost silently.
acks=1: the leader alone confirms. A write is lost if the leader dies before followers copy it.
acks=all (or -1): every ISR member confirms. Combined with min.insync.replicas, this is the durable choice.
The default moved from 1 to all in Kafka 3.0, per the 3.0 upgrade notes. A candidate who says the default is acks=1 learned from an old source.
A known trap on old clients. The same upgrade notes record that a bug in 3.0.0 and 3.1.0 (KAFKA-13598) stopped the idempotence default from applying unless it was set to true by hand. Services still on those client versions should set it explicitly.
12. How does the idempotent producer prevent duplicates?
The broker gives each producer a producer ID. The producer numbers every batch per partition. If a retry arrives with a sequence number the broker has already written, the broker drops it.
So a network timeout followed by a retry does not create a duplicate.
Idempotence is on by default. It needs acks=all, retries above 0 and max.in.flight.requests.per.connection of 5 or less, and order is kept within those limits.
If you set a conflicting value without enabling idempotence yourself, it is quietly turned off. If you enable it and set a conflicting value, the producer throws a ConfigException. Idempotence covers one producer session only.
It does not stop your code from sending the same event twice.
13. How does the producer choose a partition?
If a record has a key, the default partitioner hashes the key and takes it modulo the partition count, so one key always lands on one partition. That is what gives per-key ordering.
If there is no key, the producer uses a "sticky" partition. It fills a batch for one partition, then moves on once about batch.size bytes have gone there. This builds bigger batches than plain round robin.
A custom partitioner is rarely needed. When a candidate wants one, ask whether a better key would do the same job.
14. How do batching and compression work together?
The producer groups records for the same partition into batches, and compression applies to a whole batch. Bigger batches therefore compress better.
Two settings shape the batch. batch.size (16,384 bytes by default) is the size target. linger.ms is how long to wait for more records. Its default changed from 0 to 5 in Kafka 4.0.
The docs note that larger batches often give similar or lower latency despite the wait. compression.type defaults to none, and lz4 and zstd are common choices. For high throughput, raise both batch settings and turn on compression.
For the lowest latency, keep linger.ms small. Do not lower acks to save time unless losing data is acceptable.
15. How does a classic consumer group rebalance work?
When a member joins, leaves or misses its session timeout, the group coordinator starts a rebalance. With the classic protocol, one consumer runs the assignor and the coordinator hands out the result.
With an eager assignor, every member gives up all partitions first, so the whole group stops. The cooperative sticky assignor only moves the partitions that must change, over two short rounds.
The default list is [RangeAssignor, CooperativeStickyAssignor], which allows a move to cooperative with one rolling restart. Static membership (group.instance.id) stops a quick restart from causing a rebalance.
Kafka 4.0 made a new server-side protocol generally available; question 26 covers it.
16. How should a consumer commit offsets?
Commit after the records are processed, not before. That gives at-least-once delivery. A crash then means some records are processed again, so processing should be idempotent.
Auto-commit is on by default (enable.auto.commit=true, every 5 seconds). It commits the offsets from earlier polls during later poll() calls. That is safe for simple loops that finish each batch before polling again.
Code that hands records to other threads should disable it and call commitSync() or commitAsync() itself. A common pattern is commitAsync() in the loop and one commitSync() on shutdown or when partitions are revoked.
17. What happens when you add partitions to a topic?
You can add partitions with kafka-topics.sh --alter --partitions, but you cannot remove them. Keyed data also moves: the key-to-partition mapping uses the partition count, so many keys now land on a different partition.
Old records for a key stay in the old partition and new ones go to the new partition. Ordering per key is broken across that boundary. Stateful consumers may also see a key's state split.
That is why teams size partitions up front for expected peak load, and treat an increase as a migration.