Apache Kafka
medium 34 minApache Kafka is a distributed log for events.
See it run
Partitions, copies, offsets and groups, one step at a time.
Operations
One record, end to end
From send() to the committed position: batching, the leader's log, followers, the acknowledgement, the fetch and the commit.
Step 1 of 6
Producer sends
The key is hashed to a partition. The record waits briefly in a batch (linger.ms is 5 by default), and the batch may be compressed.
Step 1: Producer sends
producer.send("orders", key="user-123", value=order)# client: hash(key) -> partition, add to a batch# leader: append, give offsets# followers: fetch, append# leader: all in-sync copies have it -> acknowledgerecords = consumer.poll()process(records)consumer.commit()- Guarantee
- Survives 1 loss
- Cost
- 3 copies
Followers and consumers both pull. The leader never pushes to anyone.
In the real world
The same apache kafka doing real jobs. Pick one and step through it.
90% of the traffic carries one key.
Step 1 of 3
Everything lands in one place
- Partition 0
- 90%
- Partition 1
- 3%
- Partition 2
- 3%
- Partition 3
- 4%
One consumer does nearly everything
The hash of one key is always one partition. Partition 0 gets 90%.
Step 1: Everything lands in one place
Common mistakes
The bugs interviewers watch for, with the fix beside each.
acks=all with min.insync.replicas left at 1
criticalIf the in-sync set shrinks to the leader, one copy counts as all copies. Losing that broker then loses acknowledged data.
Wrongreplication.factor=3# min.insync.replicas default: 1Rightreplication.factor=3min.insync.replicas=2Aborting a transaction and polling on
criticalThe consumer's own position has already moved past the aborted records, so the next poll skips them.
Wrongproducer.abort_transaction()continueRightproducer.abort_transaction()rewind_to_committed(consumer)continueHanding records to a thread pool with auto-commit on
majorThe next poll may commit positions whose work is still in a queue. A crash then skips them.
Wrongfor r in consumer.poll(): pool.submit(work, r) # auto-commit onRightdone = [pool.submit(work, r) for r in consumer.poll()]wait(done); consumer.commit()Adding partitions to a keyed topic
majorThe partition is the key's hash modulo the count. A new count sends old keys elsewhere and breaks their order.
Wrongkafka-topics.sh --alter --topic orders --partitions 24Right# create orders-v2 with 24 partitions,# copy, then switch producers and consumers
Interview tips
How to explain it
- A topic is several logs, called partitions.
- Order holds inside one partition only.
- Readers keep a position; reading deletes nothing.
The safe trio
- Replication factor 3.
- `min.insync.replicas=2`.
- `acks=all` with idempotence, the client defaults.
Watch these
- Consumer lag per partition.
- Under-replicated and under-min-ISR partitions.
- Exactly one active controller.
Think again when
- You need a reply at once.
- Each job must be retried or delayed alone, and share groups do not fit.
- Nobody will run the cluster.