AlgoViz

Apache Kafka

medium 34 min

Apache 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

producefetchProducerbatchingBroker 1leader, 0..6Broker 2copy, 0..6Broker 3copy, 0..6Consumerposition 7

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

Producers90% one keyPartition 0Partition 1Partition 2Partition 3
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

    critical

    If the in-sync set shrinks to the leader, one copy counts as all copies. Losing that broker then loses acknowledged data.

    Wrong
    replication.factor=3# min.insync.replicas default: 1
    Right
    replication.factor=3min.insync.replicas=2
  • Aborting a transaction and polling on

    critical

    The consumer's own position has already moved past the aborted records, so the next poll skips them.

    Wrong
    producer.abort_transaction()continue
    Right
    producer.abort_transaction()rewind_to_committed(consumer)continue
  • Handing records to a thread pool with auto-commit on

    major

    The next poll may commit positions whose work is still in a queue. A crash then skips them.

    Wrong
    for r in consumer.poll():    pool.submit(work, r)      # auto-commit on
    Right
    done = [pool.submit(work, r) for r in consumer.poll()]wait(done); consumer.commit()
  • Adding partitions to a keyed topic

    major

    The partition is the key's hash modulo the count. A new count sends old keys elsewhere and breaks their order.

    Wrong
    kafka-topics.sh --alter --topic orders --partitions 24
    Right
    # 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.