Kafka
Kafka from the service's side: keys and partitions, producer acks and idempotence, offset commits and rebalancing, replication, transactions and share groups.
11 questions
JuniorTheoryVery commonWhat do acks=0, acks=1 and acks=all change for a producer in the message broker Kafka?
What do acks=0, acks=1 and acks=all change for a producer in the message broker Kafka?
acks sets when the producer counts a record as sent. acks=0 waits for nothing, so a lost record goes unnoticed. acks=1 waits for the leader to append it to the partition log, and it is lost if the leader dies before followers copy it. acks=all waits for every replica in the ISR.
Common mistakes
- ✗Believing
acks=1is safe: the leader confirms after its own write, so a record it loses before followers copy it is gone though the producer saw success - ✗Confusing
ackswith the number of copies: the replication factor sets that, andacksonly says how many replicas the producer waits for
Follow-up questions
- →Why is
acks=allnot enough when the leader is the only in-sync replica left? - →Why does Kafka's idempotent producer require
acks=all?
JuniorTheoryVery commonWhat is a Kafka partition log, and what does an offset point to?
What is a Kafka partition log, and what does an offset point to?
A partition is an append-only log: each record is added at the end and never changed, and reading deletes nothing. An offset is the record's number in that one partition, so each reader keeps its own position in the log, and old records go only by retention.
Common mistakes
- ✗Treating an offset as a topic-wide number: it is counted inside one partition, so records in two partitions can share an offset
- ✗Believing reading removes a record: records stay until retention deletes old segments, and any reader can go back
Follow-up questions
- →What happens to the offsets of records that retention has deleted?
- →Why do two consumer groups reading one partition not disturb each other?
MiddleTheoryCommonWhy can enable.auto.commit=true make a Kafka consumer lose or duplicate records?
Why can enable.auto.commit=true make a Kafka consumer lose or duplicate records?
Auto-commit saves the position every auto.commit.interval.ms (5 s by default) for records poll returned, not for records your code finished. If work lags behind poll, say in another goroutine, a crash after a commit loses records; a crash before the next commit replays all work since the last one.
Common mistakes
- ✗Believing auto-commit waits for processing: it commits what
pollreturned on a timer, so records still in flight are lost on a crash - ✗Expecting no duplicates with auto-commit: records processed after the last commit are read again after a restart or a rebalance
Follow-up questions
- →How does committing by hand after processing turn this into at-least-once?
- →Why does handing records to a worker goroutine make auto-commit lose data?
MiddleTheoryCommonWhat does Kafka's idempotent producer prevent, and what does it not?
What does Kafka's idempotent producer prevent, and what does it not?
The producer gets an id and numbers its batches per partition; the broker drops a resent batch it already wrote and refuses a gap, so retries add no duplicates and no reordering. It does not cover a producer restarted without a transactional.id, or a consumer processing a record twice.
Common mistakes
- ✗Believing it gives end-to-end exactly-once: it drops only the producer's own resends, so a consumer can still process a record twice
- ✗Expecting it to survive a producer restart: a restart starts a new session, so a resent record is new to the broker; only a transaction, read with
read_committed, discards the unfinished work
Follow-up questions
- →Why does idempotence cap
max.in.flight.requests.per.connectionat 5? - →How does a
transactional.idfence off a zombie producer after a restart?
MiddleTheoryCommonWhat do the in-sync replica set ISR and min.insync.replicas guarantee for an acks=all write in Kafka?
What do the in-sync replica set ISR and min.insync.replicas guarantee for an acks=all write in Kafka?
The ISR is the leader plus followers that caught up within replica.lag.time.max.ms. acks=all waits for the current ISR, which may shrink to the leader alone; min.insync.replicas refuses the write with NOT_ENOUGH_REPLICAS below that size, so an acked record is on at least that many replicas.
Common mistakes
- ✗Believing
acks=allwaits for every replica: it waits for the current ISR, which can shrink to the leader alone, somin.insync.replicassets the floor - ✗Expecting
min.insync.replicasto protectacks=1writes: onlyacks=allchecks it
Follow-up questions
- →Why is
replication.factor=3withmin.insync.replicas=2the common choice? - →What does unclean leader election risk when the ISR is empty?
MiddleTheoryCommonHow does a Kafka producer choose a partition for a keyed record, and what breaks that mapping?
How does a Kafka producer choose a partition for a keyed record, and what breaks that mapping?
The default partitioner takes murmur2 of the key bytes modulo the partition count, so one key always lands in one partition and keeps its order. Adding partitions changes the modulo and moves keys; a client whose default hash differs, such as librdkafka's CRC32, sends the same key elsewhere.
Common mistakes
- ✗Expecting new partitions to keep each key in place: the partition is
hash(key) % n, so a newnmoves many keys and breaks their order across the change - ✗Assuming every client hashes keys alike: a producer with another default hash puts the same key in another partition
Follow-up questions
- →How would you add partitions to a keyed topic without breaking per-key order?
- →Where does Kafka's default sticky partitioner send a record without a key?
MiddleTheoryCommonWhat does a Kafka transaction make atomic, and what stays outside it?
What does a Kafka transaction make atomic, and what stays outside it?
A transactional producer's writes to several partitions and topics, plus the consumer offsets sent through the transaction, commit or abort together: exactly-once read-process-write inside Kafka. A database write, an HTTP call or any other side effect stays outside and may repeat.
Common mistakes
- ✗Believing a Kafka transaction covers a database write: it rolls back only Kafka records and offsets, so external effects need idempotency
- ✗Committing consumer offsets outside the transaction: a crash between the two then duplicates the output or loses the input
Follow-up questions
- →Why does a transactional pipeline need
read_committedconsumers downstream? - →How do you make a database side effect safe when the transaction is retried?
MiddleTheoryOccasionalWhat is the high watermark in a Kafka partition, and why can't a consumer read past it?
What is the high watermark in a Kafka partition, and why can't a consumer read past it?
The high watermark is the offset up to which every in-sync replica has copied the log. Records above it are not yet on every in-sync replica, so a new leader may lack them, and after a failover they would vanish. Consumers get only records below it, which survive a leader change.
Common mistakes
- ✗Confusing the high watermark with the log end offset: records between the two are on the leader but not yet on every in-sync replica, so consumers do not see them
- ✗Thinking the limit protects against slow disks: it protects against a leader change, after which records only the old leader had are gone
Follow-up questions
- →How does an
acks=allwrite relate to the high watermark moving forward? - →What does the last stable offset add for a
read_committedconsumer?
MiddleTheoryOccasionalWhat does isolation.level=read_committed hide from a Kafka consumer?
What does isolation.level=read_committed hide from a Kafka consumer?
It hides records of aborted transactions and stops at the last stable offset, the start of the oldest open transaction, so later records wait until it ends, non-transactional ones too. Commit and abort markers never reach the app in either mode.
Common mistakes
- ✗Expecting aborted records to be deleted: they stay in the log, and a
read_uncommittedconsumer, the default, still reads them - ✗Thinking only transactional records wait: the consumer stops at the oldest open transaction, so later non-transactional records wait too
Follow-up questions
- →How can one long-running transaction raise the lag of every
read_committedconsumer? - →Why must a read-process-write pipeline read its input with
read_committed?
MiddleTheoryOccasionalHow does the new consumer group protocol KIP-848 change Kafka rebalancing versus the classic one?
How does the new consumer group protocol KIP-848 change Kafka rebalancing versus the classic one?
In the classic protocol every member rejoins and a leader consumer computes the assignment; the eager variant first revokes all partitions. With KIP-848 (group.protocol=consumer) the broker's coordinator assigns and moves only the changed partitions while others keep consuming; it is opt-in in Kafka 4.
Common mistakes
- ✗Thinking KIP-848 is just a faster classic rebalance: assignment moves to the broker and becomes incremental, with no group-wide pause
- ✗Assuming Kafka 4 consumers use KIP-848 by default: it is opt-in through
group.protocol=consumer
Follow-up questions
- →How does the cooperative-sticky assignor reduce the pause within the classic protocol?
- →What does static membership with
group.instance.idsave during a rolling restart?