Lab · Messaging

Kafka: partitions, replication & delivery

Producers append to a topic split into partitions. Each partition is copied to three brokers — one leader, two followers — and read by one consumer in the group. Break brokers and consumers, change acks and commit order, and see ordering, lag, data loss, duplicates, retention and replay.

Try it

Lab time 3.0 s
Producers, key = user idProducerskey = user id2/s · acks=allPartition 0, leader B1 · 3/3 in syncPartition 0leader B1 · 3/3 in synclag 0Partition 1, leader B2 · 3/3 in syncPartition 1leader B2 · 3/3 in synclag 3Partition 2, leader B3 · 3/3 in syncPartition 2leader B3 · 3/3 in synclag 1Consumer C1, reads P0, P2Consumer C1reads P0, P20 processedConsumer C2, reads P1Consumer C2reads P12 processedDead-letter queue, 0 messages parkedDead-letter queue0 messages parked
About each part:
  • Producers — key = user id — healthy — 2/s · acks=all
  • Partition 0 — leader B1 · 3/3 in sync — healthy — lag 0
  • Partition 1 — leader B2 · 3/3 in sync — healthy — lag 3
  • Partition 2 — leader B3 · 3/3 in sync — healthy — lag 1
  • Consumer C1 — reads P0, P2 — healthy — 0 processed
  • Consumer C2 — reads P1 — healthy — 2 processed
  • Dead-letter queue — 0 messages parked — healthy
  • Asynchronous
  • Event / message
  • Poison message

Producer

Acknowledged
6
of 9 sent
Produce latency
824 ms
send() → ack
Messages per request
1.0
9 requests
Retried / refused
0
Acked, then lost
0
Written twice
0

Consumers

Processed
2
Lag
4
visible, not yet processed
Received twice
0
redelivered after a crash
Applied twice
0
Transactions
—
exactly-once is off
Skipped
0
Deleted unread
0

Cluster

Under-replicated
0
partitions short of copies
Offline
0
partitions with no leader
Leader elections
0
Rebalances
0
Compacted
0
compaction is off
In
0

Numbers add up:sent 9 = waiting 0 + awaiting ack 3 + acknowledged 6 + failed 0; every partition: oldest kept ≤ committed ≤ high watermark ≤ end; one consumer per partitionThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Producers
2/s

Wait for every in-sync replica. Slower; acknowledged writes survive a leader failure.

0 ms
Topic & group
700 ms
Retention

Removes an older message once a newer one with the same key is 3 s old, so each key keeps its latest value. Needs keyed messages.

Rebalance

Every consumer stops while all partitions are handed out again.

Kafka can add partitions but never remove them.

Consumers
Commit the offset

At-least-once: a crash redelivers.

The result and the offset commit together. A crash aborts the transaction, so nothing is applied twice. Commit-before is ignored.

Failures

Click a consumer in the diagram, or a broker in the replica table, to crash or restart it.

Lag (messages visible but not yet processed)
01.32.53.851s2s3s
Show data table
TimeLag
1s0
1s1
2s3
2s2
3s2
3s4
Throughput (messages/s)
Sent by producersProcessed
02.557.5101s2s3s
Show data table
TimeSent by producersProcessed
1s40
1s20
2s00
2s22
3s42
3s60
Produce requests per second
02.557.5101s2s3s
Show data table
TimeRequests/s
1s4
1s2
2s0
2s2
3s4
3s6
Produce latency (ms, send() → ack)
025050075010001s2s3s
Show data table
Timep99
1s0
1s627
2s710
2s710
3s710
3s824

Brokers & replicas

Replicas of each partition on each broker
BrokerP0P1P2Action
B1upLeaderhas 0Followerhas 7Followerhas 1
B2upFollowerhas 0Leaderhas 7Followerhas 1
B3upFollowerhas 0Followerhas 7Leaderhas 1

“has N” is how many messages that copy holds. The in-sync replicas (ISR) are the copies that are caught up; only they can become leader.

Partition logs

user-1user-2user-3user-4user-5user-6
P0read by C1committed 0 · HW 0 · end 0lag 0
    P1read by C2committed 2 · HW 5 · end 7lag 3
    1. 0u3
    2. 1u3
    3. 2u6
    4. 3u3
    5. 4u3
    6. 5u3
    7. 6u6
    P2read by C1committed 0 · HW 1 · end 1lag 1
    1. 0u5

    Faded cells are committed. The outlined cell is next for the consumer. Dashed cells are above the high watermark: written to the leader but not yet on every in-sync replica, so consumers cannot see them. Cells marked "gone" were removed by compaction, because a newer message has the same key.

    What just happened

    Nothing yet. Start traffic or run an experiment.

    Compare two acks settings through the same leader crash

    Both sides produce the same messages, with the same keys, at the same moments. From 10 s, the first time partition 0's leader holds messages its followers have not copied, that leader's broker dies on both sides at once. Only the acks setting differs, so any gap comes from acks. Both sides start with 20 seconds already run.

    Side 1

    The producer waits for the partition leader only. A write the followers have not copied dies with the leader.

    Side 2

    The producer waits until every in-sync replica has the write. Slower, and a confirmed write survives the leader dying.

    4/s
    Lab time 20.0 s
    Side 1 acks=1
    Producers, 4/s · acks=1Producers4/s · acks=1Partition 0, leader B2, DEGRADEDPartition 0leader B22/3 in sync · lag 4DEGRADEDPartition 1, leader B2, DEGRADEDPartition 1leader B22/3 in sync · lag 22DEGRADEDPartition 2, leader B3, DEGRADEDPartition 2leader B32/3 in sync · lag 14DEGRADEDConsumer C1, reads P0, P2Consumer C1reads P0, P221 processedConsumer C2, reads P1Consumer C2reads P122 processed
    • Producers — 4/s · acks=1 — healthy
    • Partition 0 — leader B2 — DEGRADED — 2/3 in sync · lag 4
    • Partition 1 — leader B2 — DEGRADED — 2/3 in sync · lag 22
    • Partition 2 — leader B3 — DEGRADED — 2/3 in sync · lag 14
    • Consumer C1 — reads P0, P2 — healthy — 21 processed
    • Consumer C2 — reads P1 — healthy — 22 processed
    Side 2 acks=all
    Producers, 4/s · acks=allProducers4/s · acks=allPartition 0, leader B2, DEGRADEDPartition 0leader B22/3 in sync · lag 5DEGRADEDPartition 1, leader B2, DEGRADEDPartition 1leader B22/3 in sync · lag 22DEGRADEDPartition 2, leader B3, DEGRADEDPartition 2leader B32/3 in sync · lag 14DEGRADEDConsumer C1, reads P0, P2Consumer C1reads P0, P221 processedConsumer C2, reads P1Consumer C2reads P122 processed
    • Producers — 4/s · acks=all — healthy
    • Partition 0 — leader B2 — DEGRADED — 2/3 in sync · lag 5
    • Partition 1 — leader B2 — DEGRADED — 2/3 in sync · lag 22
    • Partition 2 — leader B3 — DEGRADED — 2/3 in sync · lag 14
    • Consumer C1 — reads P0, P2 — healthy — 21 processed
    • Consumer C2 — reads P1 — healthy — 22 processed
    • Asynchronous
    • A message, named after its key

    Each side draws only a few messages at a time, so the boxes stay easy to follow. The table counts every message. The leader crashed at 10.1 s on both sides.

    Compare two acks settings through the same leader crash: the numbers for each side and the gap between them
    MeasureSide 1 · acks=1Side 2 · acks=allGap
    Messages produced8686Same messages
    Confirmed to the producer8484Same
    Producer wait for a confirm, p50337 ms735 msacks=1 lower by 399 ms
    Confirmed, then lost10acks=all lower by 1

    Changing a setting starts both sides again, so the numbers always describe one setup from its start. 3 brokers hold 3 copies of each partition. A crashed broker stays down, so a second crash leaves fewer copies. "Confirmed, then lost" counts messages the producer was told were saved that are no longer in any log.

    What this model simplifies
    • Three brokers, replication factor 3, and min.insync.replicas = 2 (the broker default is 1; 2 with 3 replicas is a common production choice). A controller notices a dead broker after 1.5 s.
    • Followers are dropped from the in-sync set only when their broker dies. Real Kafka also drops a follower that falls too far behind (replica.lag.time.max.ms).
    • Eager rebalancing hands partitions out round-robin and pauses the whole group. Cooperative rebalancing here moves only what must move in one step; real Kafka takes two rounds, and newer versions have a new group protocol.
    • Offsets are committed after every message. Real consumers commit in batches, which widens the window for duplicates after a crash.
    • Exactly-once transactions here cover one result and its offset. Real Kafka transactions write to output topics, which readers see only with read_committed. For effects outside Kafka, such as an email, you still need an idempotent consumer.
    • Failed messages are retried in place, blocking the partition. Many teams publish them to a retry topic with a delay instead.
    • Retention is by time only and deletes single messages. Real Kafka deletes whole log segments by time or size. Compaction here marks single entries removed once they are 3 s old; real Kafka cleans closed segments in the background, and deleting a key with an empty message (a tombstone) is not shown.