How Many Partitions: Calculate, Don’t Guess
21:30. Sixty-four, with room to spare
November 2, 2184, in the dispatch room at Vault-9. The feed is live, the reading room is connected, and the antenna directory is taking shape. This morning you created signal_raw with three partitions in a hurry, just to catch the carrier. Now you expand it to 64. Plenty of headroom: you can’t reduce the count later, so you might as well leave yourself extra capacity.
At 21:50, feed producers start dropping out. Six producers across 64 partitions need 384 megabytes of memory, and the comms level doesn’t have that much available. At the same time, the reading room complains: a seventh consumer has joined its group, and the others pause for almost eleven seconds while the group redistributes partitions among the consumers. This morning the same pause lasted half a second. There were only three partitions to assign; now there are 64.
The headroom you added “so we won’t have to redo this later” has broken both sides at once: the systems writing to the topic and the systems reading from it.
QUERY: Headroom is a number too. You calculate it too.

signal_raw after expansion—how many of its 64 partitions received any frames at all—and an attempt to reduce the count back to twelve. Third, it shows the only way to get twelve partitions under the same name. Look at how many frames remain in the topic afterward.Where the number comes from
The screen shows the same 384 megabytes and 10.9 seconds from the opening scene. Extra partitions are expensive on both sides. A producer uses memory for every partition in the topic. A group remains paused while it works out which consumers should receive which partitions, and the more partitions it has to assign, the longer that takes. This is a rebalance: redistributing partitions among consumers in a consumer group. So “more is safer” is not a valid answer to “how many partitions?” You have to calculate the number.
The numbers arrive at 22:40 in a request from the reading room to capture the full feed starting at midnight: a peak of 216 MB/s (480 frames per second at 450 KB each), nine consumers at peak load, and a line saying “one partition accepts 18 MB/s.” Within a group, each partition is assigned to exactly one consumer, so consumers beyond the partition count sit idle. Those figures give you the number. There are three bounds to calculate:
- throughput lower bound — required throughput divided by what one partition can handle: 216 ÷ 18 = 12. With fewer partitions, the peak won’t fit;
- consumer-count lower bound — the number of consumers working concurrently at peak load: 9. With fewer partitions, the extra consumers sit idle;
- key upper bound — the number of distinct values in the key.
source_iddetermines the partition, and it has twelve distinct values, one for each antenna in the directory. With more than twelve partitions, the frames can never occupy all of them, no matter how much traffic arrives: in the second part of the cell, only 12 of 64 partitions received frames, while the other 52 remain empty forever.
Take the higher lower bound—12—and compare it with the upper bound, which is also 12. There is no room for headroom between the bounds: fewer partitions cannot carry the peak, while the key cannot use more than twelve. One caveat: twelve is a ceiling, not a guarantee of even distribution. In the third part of the cell, the frames landed in ten of twelve partitions, while dividing 216 by 18 quietly assumes that traffic is spread evenly. Chapter 2 explains why key-based distribution can be uneven.
A number you didn’t measure
You did not measure the 18 MB/s per partition. The reading room DECLARED that number, along with the peak and the consumer count. The course Kafka gives you no way to measure it, and in real design discussions you are often given a figure like this rather than measuring it yourself. The rule is simple: record it as an assumption—what you accepted, where it came from, and what happens if it turns out to be half as large. If a partition handles 9 MB/s, the throughput lower bound becomes 24. That exceeds the upper bound, so there is no valid answer within twelve partitions. In the first lesson of this chapter, you recorded a requirement as a number; here you record an assumption the same way.
plan_partitions(peak_mb_s, per_partition_mb_s, peak_consumers, key_values)—the number of partitions to use, or None if no suitable count exists.
The request contains three numbers, and all three are DECLARED rather than measured by you: the peak is 216 MB/s, one partition accepts 18 MB/s, and there are 9 consumers at peak load. The fourth number comes from your directory: the source_id key has twelve distinct values.
- the throughput lower bound is the peak divided by what one partition can handle; you can’t have a fraction of a partition, so round up;
- the consumer-count lower bound is the number of consumers working at peak load;
- take the higher of the two lower bounds and compare it with the upper bound—the number of distinct key values. If the lower bound is above the upper bound, return
None.
None is not an error. It is the honest answer: with this key, no suitable partition count exists. Do not build growth headroom into the function. The function calculates the bounds; headroom is a separate decision made afterward.The cost of an extra partition
Every partition above the upper bound gives you nothing, but you still pay for it—and you have already calculated how much. There are three costs:
- producer memory. In the course model, a producer keeps one megabyte for every partition in the topic because it does not know in advance which keys it will receive. Six producers across 64 partitions use 384 MB; across twelve, 72 MB;
- broker files and memory. Each partition has its own set of segments and offsets, which means its own open files on the broker’s disk and its own memory overhead. Sixty-four partitions mean 64 such sets;
- rebalance time. While the group works out again which consumers should receive which partitions, the consumers are paused. In the course model, that costs 170 ms per partition: 10.9 s with 64 partitions versus 2.0 s with twelve. Chapter 3 covers the mechanics.
Migration: when you created too many partitions
You cannot reduce the partition count—the broker rejected that attempt in the second part of the cell. That leaves , with two options and a different cost for each.
With a new name. Create a second topic with twelve partitions, switch the producers to it, read from both topics for a while and write their output to the same destination, then shut down the old topic once it has been drained. The producers do not need to stop. The cost is reading two topics and losing ordering across the cutover boundary: for each key, old messages are in one topic and new messages are in another, so while a consumer reads both, their relative order is not guaranteed.
Keeping the same name. Let the consumers finish reading, shut down the old topic, and create a new one under the same name. That is what the third part of the cell shows: twelve partitions and zero frames. The producers wait while the topic is replaced. The cost is the entire history that was stored in the old topic.
The station chooses the second option today. The feed in signal_raw lives for only six hours anyway, and the reading room had drained it by 23:10, so there is almost nothing to lose. The migration is scheduled for midnight: starting at 00:00, signal_raw receives the feed on twelve partitions.
23:10. One partition and an empty board
The same request contains another line: the reading room wants four consumers to process dispatch-room responses from signal_ack. But this morning you created signal_ack with ONE partition—“because ordering matters there.”
Four consumers on one partition means one working consumer and three idle ones. There is no way to give them more work without redesigning the topic. One partition gives you total ordering across all responses, but it also sets the ceiling: one broker and one consumer. That is a valid choice if you state its cost, and you do: there are four responses per hour, their order matters more than speed, and one consumer is enough. The reading room gets the answer: “one consumer, and here’s why.”
At 23:10, you mount the lag board on the rack—a screen that shows consumer lag, or how far the consumers are behind the end of the log. For now, it is empty. You will start reading it tomorrow.
QUERY: Remember what an empty board looks like. You won’t see it like this again.
What’s real in this lesson, and what belongs to the course model
The cost figures come from a simplified course model chosen to keep the arithmetic easy to follow: 170 ms of group pause per partition and one megabyte of producer memory per topic partition. Real Kafka producer memory does not work that way. A producer has one shared memory buffer and divides it among partitions as needed. So the 384 megabytes from the scene is an upper estimate. The lesson “Producer Performance: Batches, linger.ms, Compression” in the “Advanced” track explains how that memory really works and what happens when it runs out. Rebalance pauses are not calculated as “170 ms per partition” either: they depend mostly on how quickly the consumers rejoin the group, while a larger partition count adds more assignment work.
The 18 MB/s per partition is not part of the model. It comes from the reading room’s request. The course Kafka has no disk or network, and its replicas—copies of the same partition on other brokers—live in memory without transferring data, so there is no meaningful way to measure partition throughput there. Migration in the sandbox is instantaneous: delete_topic and create_topic take no time, and producers do not wait. The cell also does not show message ordering across the boundary because it does not model two live topics or producers switching between them on the fly.
The real parts are the method—two lower bounds, one upper bound, and the cost of every extra partition—and the rule that the course Kafka enforces just like real Kafka: the partition count can only increase, and an attempt to reduce it is rejected. The key upper bound is not an assumption either. It is a fact from the directory: source_id has as many values as there are antennas.
Interview question
How this comes up in interviews
“How many partitions would you use for this topic?” is an almost inevitable question, and a candidate who immediately gives a number has already answered the wrong question. Interviewers expect a method: two lower bounds, one upper bound, and one explicit assumption. The throughput floor is required throughput divided by what one partition can handle. The consumer parallelism floor is the number of consumers working at peak load. The key cap is the number of distinct key values: more partitions will not create more parallelism, and the extras will remain empty. Take the higher lower bound and compare it with the upper bound. If there is room between them, add growth headroom and round up to a multiple of the broker count so the partitions distribute evenly. If they do not give you peak throughput or the consumer count, ask for those numbers; asking is part of the correct answer.
They will also care about how you handle the figure for “how much one partition can sustain.” A strong answer does not quote a number from memory or copy one from an article. It states where the figure came from—your own measurement or someone else’s requirement—and explains what changes if the real value is half as large.
The second question is about cost: “Then why not create a thousand?” They expect at least three costs: producer memory that grows with the partition count, open files and broker memory, and consumer-group rebalance time while the group works out again which consumers get which partitions.
The third question is “What if you got it wrong?” If you chose too few, you can add partitions, and you should mention that the key-to-partition mapping will shift afterward. If you chose too many, you cannot reduce the count: you either live with it or do a topic cutover. A strong candidate describes both straight away: using a new topic name and dual reading when downtime is unacceptable, or keeping the same name when you can afford to lose the history.
source_id key. A colleague says, “The traffic is 1.5 times higher, so use 1.5 times as many partitions: set it to 18.” What do you say?Key takeaways
| what | number | source |
|---|---|---|
| throughput lower bound | 216 ÷ 18 = 12 | request; 18 MB/s is an assumption, not a measurement |
| consumer lower bound | 9 | request |
| key upper bound | 12 | directory: twelve distinct source_id values |
| answer | 12 | lower and upper bounds meet |
| cost of 64 partitions | 384 MB of memory, 10.9 s pause | course model |
signal_ack | 1 partition | total ordering; one consumer—the cost is explicit |
You do not choose a partition count; you calculate it. Take the higher of the two lower bounds and compare it with the upper bound. Any number you did not measure should be recorded as an assumption together with its source. An extra partition costs memory, files, and time without adding useful capacity. You cannot reduce the count, so the only way down is : with a new name, you pay with ordering at the boundary and two topics being read at once; with the same name, you pay with history.
QUERY: This morning, three by guesswork. This evening, sixty-four by guesswork. Twelve is the first partition count today that you actually calculated.