System design lab · Episode 11

A partitioned log, Kafka style

A message queue hands each message to one worker and then forgets it. A log keeps every message in order and lets each reader keep a bookmark. Split the log into partitions, and many readers can work at once. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
1234123412341234
A bookmark that fell behind the visible part of the railTiles: messages, with the first letter of the userRails: partitions 0, 1 and 2, newest message at the right

Controls

Consumers in the group
A second group, reading from the start
Time

Want the details?

A queue or a log?

In the message queue lab, a worker takes a message, does the job, and the message is gone. That suits jobs: send this email, resize this photo.

A log works differently. Messages are written to the end, in order, and stay there (for days, or forever). A reader does not take messages away; it keeps an offset, a number that says how far it has read, like a bookmark. Many readers can read the same log, each at its own place, and a new reader can start from the beginning. This suits events that many parts of a system care about: an order was placed, a user signed up.

One log on one machine is not enough for a busy system, so the log is split into partitions. Each message goes to one partition, chosen by its key. All messages with the same key go to the same partition, so their order is kept.

How to read the bench
Part on the benchWhat it means in a real system
Box at the rightThe producer, writing events.
Three railsThree partitions of one topic. The newest message is at the right.
Tile with a letterOne message; the letter is its key, the user.
Tall bookmarkThe committed offset of the first consumer group. Tiles to its left are read (pale).
Thin postThe offset of the second group.
Plate at the left of a railWhich consumer of the first group owns that partition.
Boxes at the frontThe consumers of the first group. A dark lamp: no partition to read.
The numbers
  • The producer writes one message every 0.5 s: 2 a second, for six users in turn.
  • The partition is hash(user) mod 3. With these six names, each partition gets two users, so 2/3 of a message a second.
  • A consumer reads 1.5 messages a second.
  • 1 consumer: the lag grows by 2 − 1.5 = 0.5 a second, about 20 after 40 s.
  • 2 consumers: the one with two partitions needs 4/3 a second and can read 1.5, so it keeps up; the lag goes back to nearly 0.
  • The second group has one consumer per partition, 4.5 messages a second, so it catches up with the whole history.

The test of the model also checks that every user's messages are read in the order they were written, through hundreds of random changes.

In real systems
  • Apache Kafka is the best known log: topics, partitions, consumer groups and committed offsets, as on this bench. Redpanda speaks the same protocol. Amazon Kinesis calls partitions shards.
  • Choose the key with care. All messages of one key go to one partition, so one very busy key (a celebrity account) makes one partition hot.
  • Rebalance. When consumers join or leave, the partitions are shared out again, and reading pauses briefly.
  • Lag is the number to watch. Growing lag means the consumers are too slow: add consumers (up to the number of partitions) or make each one faster.
  • Replay is a superpower: fix a bug in a consumer, reset its offset, and process the past again.
Words to know
Topic
A named log, for one kind of event.
Partition
One part of a topic; a log of its own, in order.
Offset
The position of a message in a partition, and the bookmark of a reader.
Consumer group
Consumers that share the work of reading a topic; each partition is read by one of them.
Lag
How many messages are written but not yet read by a group.