In this lesson · topic 1 of 7
Topic 1
One line is not enough
4 min read
A ceiling with one reader
A single consumer can only process so many messages per second. When the producers send more, the queue behind it grows without end. The obvious fix is to add consumers.
On a plain queue that creates a second problem. Two consumers take messages from the same line, and they finish at different speeds. A message that was sent later can be processed first. For many uses that is fine. For others, such as the updates to one account or the events of one order, the order is the point. A deposit processed after the withdrawal that followed it gives a different balance.
You want two things that pull in opposite directions: many consumers working in parallel, and a fixed order for the messages that belong together.
Split the stream by key
The answer is to split the stream into several ordered lines, called partitions, and to send every message that belongs together to the same one. The thing that decides it is a key: an account, an order, a device. A message’s key is hashed, and the hash picks the partition. All messages with the same key go to the same line, in the order they were sent.
Each partition is read by exactly one consumer in a group. That consumer sees the messages of its partition in order. Other partitions are read in parallel by other consumers.
How many consumers do you need
Take four partitions, producers sending 4 messages a tick, and consumers that each handle 1.5 messages a tick.
Predict first
After 800 ticks, one consumer was 2000measured messages behind. Two consumers were 800measured behind. Four were 0measured behind.
The next topic explains how a log is built, what an offset is, and how a group shares the partitions.