Batch and stream: logs, partitions and consumer groups
Two ways to wait for data
Section titled “Two ways to wait for data”A batch job reads a bounded set of data that is complete when the job starts, produces its result, and ends. A stream job reads events as they arrive, from an input that has no end, and keeps its result up to date. Both compute over the same events. What differs is how long the answer waits, and how much machinery the job needs to keep running.
The wait of a batch is set by its schedule. If a job runs once an hour and events arrive evenly through the hour, an event that comes just after a run waits almost 60 minutes and one that comes just before the next run waits almost none, so the mean wait is 30 minutes, and that is before the job’s own running time. A job that runs once a day makes it 12 hours. A stream job has no schedule to wait for: the delay is the time to reach a consumer and be processed, plus whatever has queued up in front.
Neither is better in general. A batch is simpler, because its input does not change while it runs, and it is a good fit whenever a late answer is as good as an early one. A stream is the right shape when the value of the answer fades fast, such as a fraud check or a live count. This module is about the second shape, and about what it costs: a stream never ends, so it has to say what time means for an event, when a result is final, and what happens after a crash.
The log in the middle
Section titled “The log in the middle”Streams are usually built on a log. Kafka’s documentation describes a topic as split into partitions spread over several brokers, with events that share a key written to the same partition. It guarantees that any consumer of a partition reads that partition’s events in exactly the order they were written. And events are not deleted after they are read: a topic keeps them for a period set per topic, and they can be read as often as needed.
Several consumers can share a topic by forming a consumer group. Kafka’s design document says each partition is consumed by exactly one consumer within each subscribing group at any time. A group can therefore have no more busy consumers than the topic has partitions, and the others sit idle until a consumer fails or leaves. Each group keeps its own position in each partition, so two groups read the same log independently.
The distance between the end of a partition and the position its group has reached is the consumer lag. It is the number to watch: while it is growing, the consumers are taking messages slower than they arrive.
Mental model. A partition is a notebook that producers only write to at the end. Each consumer group keeps its own bookmark in it, and reading never tears out a page. The lag is the number of pages between the bookmark and the end.
Try it: sharing the work
Section titled “Try it: sharing the work”A topic is split into partitions, and a message goes to the partition its key hashes to. Add consumers to the group and watch the partitions being shared out.
What is happening
Producers send 12 a second and the 1 consumers at work can process 5. The lag grows by about 7 a second.
- Consumer lag
- 7018 on Partition 1, the furthest behind
- Messages processed
- 5050 different messages, 0 read again
- Consumers at work
- 1 of 1each partition is read by one consumer of the group
- Rebalances
- 0each pauses the whole group for 2 s
Total lag Furthest behind
| Partition number | Read by | Log end | Read up to | Committed | Lag | Share of messages |
|---|---|---|---|---|---|---|
| Partition 0 | Consumer 1 | 30 | 13 | 13 | 17 | 23% |
| Partition 1 | Consumer 1 | 38 | 20 | 20 | 18 | 27% |
| Partition 2 | Consumer 1 | 32 | 14 | 14 | 18 | 27% |
| Partition 3 | Consumer 1 | 20 | 3 | 3 | 17 | 23% |
Offsets count messages from 0: "Read up to" is the offset the consumer reads next, and "Committed" is where a new owner would start.
Last messages processed
Within a partition the offsets only go up. Between partitions there is no order at all.
- Partition 0, offset 11
- Partition 1, offset 19
- Partition 0, offset 12
- Partition 2, offset 9
- Partition 2, offset 10
- Partition 2, offset 11
- Partition 2, offset 12
- Partition 2, offset 13
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab advances one second at a time. A consumer receives up to its rate each second and finishes each message in the same second; processing time, the network and the broker itself are not modelled.
- A message goes to the partition its key hashes to: FNV-1a of the key text mod the number of partitions. There are 64 keys, and the hot key is key-0. The hash is a choice of the lab, not what any particular broker uses.
Simplifications
- Partitions are shared out in consecutive ranges, and a change of the group pauses all consumers for two seconds, as an eager rebalance does. A crashed consumer loses what it processed since the last commit; the commit interval is a setting.
- Exactly-once processing, transactions, replicated brokers, retention, message size and a consumer that is slow rather than dead are not modelled. The numbers are floors and illustrations, not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
The lab opens with 4 partitions and one consumer. Producers send 12 messages a second and the consumer can process 5, so the lag grows by 7 a second. At 10 seconds it is 70. Press Skip 30 s and Consumer lag reads 280, with 71 of them on Partition 2.
Now add consumers. For each row of the table, press Reset, press Add a consumer one time fewer than the number in the first column, press Skip 30 s, and read Consumer lag:
| Consumers | Consumer lag after 40 s | Consumers at work |
|---|---|---|
| 1 | 280 | 1 of 1 |
| 2 | 150 | 2 of 2 |
| 3 | 65 | 3 of 3 |
| 4 | 1 | 4 of 4 |
| 5 | 1 | 4 of 5 |
Four consumers are enough, and a fifth changes nothing: Consumers at work reads 4 of 5, because the 4 partitions keep only 4 consumers busy. The lab shows the consequence of the rule, and it is the same for a real group. Capacity can be added only up to the number of partitions, so the number of partitions has to be chosen with the future load in mind.
Then sort some needs by the shape that suits them.
Challenge
Batch or stream?
A batch job reads a bounded set of stored data and ends. A stream job reads events as they arrive and never ends. Put each need under the style that fits it best.
Sorting needs JavaScript.
Categories
- Batch
- A bounded input that is complete when the job starts. The answer can wait.
- Stream
- Events with no end, handled as they arrive. The answer is wanted soon.
Check your answers at least once before looking at the answers.
What happens if one key is hot?
Section titled “What happens if one key is hot?”A message goes to the partition its key hashes to, so all the messages of one key land in one partition, in order. That is what makes per-key order possible, and it is also a weakness: a key that carries most of the traffic puts most of it on one partition, and only one consumer can read it.
Most messages carry one key, so one partition gets most of the traffic. Its consumer falls behind while the others wait for work.
What is happening
Producers send 16 a second and the 4 consumers at work can process 20, so the lag does not grow overall.
Partition 1 receives 71% of the messages, 11.3 a second, but its one consumer can process 5. Its lag grows while the other consumers wait for work.
- Consumer lag
- 6666 on Partition 1, the furthest behind
- Messages processed
- 9494 different messages, 0 read again
- Consumers at work
- 4 of 4each partition is read by one consumer of the group
- Rebalances
- 0each pauses the whole group for 2 s
Total lag Furthest behind
| Partition number | Read by | Log end | Read up to | Committed | Lag | Share of messages |
|---|---|---|---|---|---|---|
| Partition 0 | Consumer 1 | 14 | 14 | 14 | 0 | 9% |
| Partition 1 | Consumer 2 | 116 | 50 | 50 | 66 | 71% |
| Partition 2 | Consumer 3 | 21 | 21 | 21 | 0 | 11% |
| Partition 3 | Consumer 4 | 9 | 9 | 9 | 0 | 9% |
Offsets count messages from 0: "Read up to" is the offset the consumer reads next, and "Committed" is where a new owner would start.
Last messages processed
Within a partition the offsets only go up. Between partitions there is no order at all.
- Partition 0, offset 13
- Partition 1, offset 45
- Partition 1, offset 46
- Partition 1, offset 47
- Partition 1, offset 48
- Partition 1, offset 49
- Partition 3, offset 7
- Partition 3, offset 8
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab advances one second at a time. A consumer receives up to its rate each second and finishes each message in the same second; processing time, the network and the broker itself are not modelled.
- A message goes to the partition its key hashes to: FNV-1a of the key text mod the number of partitions. There are 64 keys, and the hot key is key-0. The hash is a choice of the lab, not what any particular broker uses.
Simplifications
- Partitions are shared out in consecutive ranges, and a change of the group pauses all consumers for two seconds, as an eager rebalance does. A crashed consumer loses what it processed since the last commit; the commit interval is a setting.
- Exactly-once processing, transactions, replicated brokers, retention, message size and a consumer that is slow rather than dead are not modelled. The numbers are floors and illustrations, not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
Here 60% of the 16 messages a second carry one key. The lab reads that out: Partition 1 receives 71% of the messages, 11.3 a second, while its consumer can process 5. Its lag is 66 when the lab opens and 249 after Skip 30 s, while the other three consumers have no lag at all. Adding a consumer does not help: the hot partition still has one reader. Adding partitions does not either. Press Add a partition 4 times and Add a consumer 4 times, for 8 and 8, and Partition 1 still receives 66% of the messages, 10.5 a second, and after Skip 30 s the furthest behind has a lag of 220. The hot key still hashes to a single partition.
The cure is on the key side, as with a hot shard in the partitioning module: spread the key (for example by adding a number to it), cache or combine its messages before they reach the topic, or give up the order of that key. Each of these is a trade, and the first two change what a consumer can assume about the order within the key.
- A batch job reads bounded data and ends, so an event waits for the next run: 30 minutes on average for an hourly job, and 12 hours for a daily one. A stream job reads events as they arrive and has no end.
- A partitioned log keeps events in order within a partition, sends the events of a key to the same partition, and keeps them after they are read.
- In a consumer group each partition has exactly one reader, so at most as many consumers as partitions are busy: with 4 partitions the fifth consumer is idle, and the lag is the same as with 4.
- Consumer lag is the distance between the end of a partition and the group’s position. With 1 consumer for 12 messages a second it grew from 70 to 280 in 30 seconds, and with 4 consumers it stayed at 1.
- A hot key fills one partition. More consumers, or 8 partitions instead of 4, leave it at 66% of the messages and a lag of 220, so the cure is in the key.
Next: event time and windows.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.