Try
↑ ↓ move↵ openesc close
In this lesson · topic 4 of 7
  1. 1. One line is not enough
  2. 2. Partitions, offsets and consumer groups
  3. 3. Four partitions, one hot key, one rebalance
  4. 4. When one partition cannot keep up
  5. 5. How many partitions to create
  6. 6. Where the commit goes decides the guarantee
  7. 7. Owners, order and commits: three checks

Topic 4

When one partition cannot keep up

6 min read

Every number on this page was produced by the simulator with a fixed seed and is checked by a test in the repository. Hover a measured value to see the setup.

More consumers than partitions

Run four partitions with producers sending 8 a tick. With four consumers, 4799measured messages were processed in 800 ticks. With eight consumers it was 4799measured, the same count. 4measured of the eight sat idle.

The partition count is the ceiling on a group’s parallelism. It is not easy to raise later, as the section on repartitioning below shows. That is why teams choose more partitions than they need today.

A hot key

Send half of all messages with one key. The partition that owns the key then receives 63%measured of the topic, more than half, because other keys hash there too. After 800 ticks its lag was 808measured messages. The worst of the other three partitions was 0measured.

Giving the group 12 consumers left the hot partition’s lag at 808measured. One partition has one reader.

What helps is to change the key. Add a suffix so a busy key spreads over several partitions. The price is that order now holds only within each suffix, and no longer across the whole key. Or make the slow consumer faster. More partitions alone will not help for a single key, because a single key still maps to one partition.

A rebalance stops everyone

With four consumers at 2 messages a tick against 4 produced, lag was 0measured just before a consumer left. With a 20-tick pause the group was 120measured messages behind when it ended. With a 60-tick pause it was 280measured. Lag grows at the production rate for as long as the pause lasts.

After the first pause, 40measured messages were read a second time. They had been processed since the last commit. A rebalance is a replay of the work done since then, so consumers must tolerate repeats. A group that also loses a consumer in a tight setup may never catch up. When a consumer joins instead, the new member ends up with work. With 6 partitions and 3 consumers, after one more joined, 4measured consumers held partitions.

No order across keys

In the default group, 50%measured of the messages were processed after a later one from another key. Order broke across keys all the time. Within a key it broke 0measured times. If a result needs a global order, a log with several partitions does not provide it. You can use a single partition, with its throughput ceiling, or you can reorder by timestamps downstream. The Dataflow Model paper is a good account of how to reason about out-of-order data and event time.

Changing the partition count

Keys are placed by hash modulo the partition count. Raise it from 4 to 5 and 84%measured of this lab’s keys land in a different partition. Raise it from 4 to 8 and 52%measured do. For a while, old messages for a key sit in the old partition and new ones in the new, and two consumers may handle them at once. Per-key order across the change is not guaranteed.

Replay and retention

A replay from the earliest offset read 2000measured messages a second time, in a log that held 2000measured. Consumers must be idempotent or the effects repeat.

A log does not keep data forever. With one key taking 90 percent of the traffic and slow consumers, 8107measured messages were deleted by retention before anyone read them. A consumer that falls further behind than the retention window loses data silently, unless lag is alarmed.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms