Try
↑ ↓ move↵ openesc close
In this lesson · topic 5 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 5

How many partitions to create

7 min read

The count is hard to change

The earlier topics showed two facts. The partition count caps the parallelism of a consumer group. And a key maps to a partition by hash modulo the count, so changing the count moves most keys. Put together, you want to pick the number once, and pick it with room to grow.

A sizing rule

Start from the throughput you need and the throughput one partition can carry on each side. Let t be the target in messages a second, p the most one partition can accept from producers, and c the most one consumer can process. You need at least the larger of t / p and t / c partitions. This is the rule of thumb in Jun Rao’s guide to choosing a partition count.

Work an example. The target is 50,000 messages a second. Brokers can take about 10,000 a second into one partition, so producers need 5. Each consumer does expensive work and finishes 2,000 a second, so consumers need 25. The answer is 25. The slow side sets it, and usually that is the consumer.

Now add headroom. Traffic doubles in a year, so you want 50. The consumer group can then grow to 50 members. Today you may run only 25, with two partitions per consumer. That is fine, and it leaves room to scale out without touching the topic.

Predict first

A topic needs 40,000 messages a second. A consumer finishes 2,500 a second, and a partition accepts 20,000 a second from producers. What is the minimum number of partitions?

Why not 10,000 partitions

Extra partitions are not free.

More open files and memory on the brokers. Each partition is stored as log segment files, and brokers hold file handles and buffers for them. Large counts need tuning of limits.

Slower recovery. When a broker fails, each partition it led needs a new leader elected. With thousands of partitions per broker, that takes longer, during which those partitions are unavailable. Newer Kafka versions with KRaft metadata handle large counts much better than ZooKeeper-based ones, but the cost does not vanish.

Longer rebalances and more state. A group coordinating 5,000 partitions has more assignments to compute, commit and recover.

Worse batching. A producer batches messages per partition. Spread 1,000 messages a second over 10 partitions and each batch carries 100. Spread over 1,000 partitions and each partition sees one message a second, so batches are tiny, and throughput and latency both suffer.

End-to-end latency. A message is visible to consumers only once it is replicated. More partitions means more replication work per broker, which can add delay.

A decent guideline is to size for roughly twice your projected need, not a hundred times.

When you must grow it anyway

If the count has to rise from 25 to 50, existing keys remap. About half stay in the same partition and half move, as the earlier topic measured for 4 to 8. Two ways to cope:

  • Create a new topic with the new count and migrate. Producers write to both for a while, consumers drain the old topic, then switch. Order per key is kept because each topic is internally consistent.
  • Add partitions in place and accept a brief window where a key’s history is split across two partitions. This is fine for data where per-key order does not matter, and it is risky for state-machine events.

Either way, a consumer that keys state by partition number breaks. Key state by the message key, not by where it happened to land.

Sources

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