0% completed
How Many Partitions
On This Page
What the Count Limits
Sizing by Data Rate
The Cost of Too Many
Adding Partitions Later
Choosing the Number
Using This in an Interview
Key Takeaways
Practice Questions
A cold-chain logistics company runs 6,000 refrigerated trucks. Every 2 seconds, each truck sends a telemetry reading of about 1.5 KB with the cargo temperature, the door state, the location, and engine data. That is 3,000 readings a second, or about 4.5 MB a second, written to a topic named truck-telemetry.
Three services read the topic: an alert checker that warns when cargo warms up, a live map, and an archive. The company expects the fleet to triple within two years. The partition count must be chosen when the topic is created. Kafka lets the team add partitions later, but adding them has side effects, and removing them is not possible.
This page turns those figures into a partition count, states every assumption behind it, and shows what a later change of count does to the data.
What the Count Limits
The partition count is the number of logs a topic is split into, and it caps how many consumers in one group can read the topic at the same time. Kafka's design documentation states that each partition is consumed by "exactly one consumer within each subscribing consumer group at any given time." On a topic with 6 partitions, a group can therefore keep at most 6 consumers busy; a seventh would receive nothing. The operations guide says it directly: "the partition count impacts the maximum parallelism of your consumers."
The count also limits how far the topic spreads across brokers. The same guide notes that "each partition must fit entirely on a single server," so a topic's write load can be spread over at most as many brokers as it has partitions. The partitions themselves are the topic's logs, as described in Topics and the Log.
The count therefore has to satisfy two needs: enough partitions to accept the writes, and enough partitions for the slowest reader to run enough consumers.
Sizing by Data Rate
Throughput per partition and per consumer depends on the hardware, the record size, and what the consumer does with each record, so the figures below are assumptions for the arithmetic. Measure your own before you rely on any of them.
- Writes. Suppose one partition accepts about 10 MB a second of writes on these brokers.
- The slowest reader. The alert checker writes every reading to a database, and suppose one instance handles about 500 readings a second. At 1.5 KB a reading, that is 0.75 MB a second.
- The other readers. Suppose one live map instance handles about 3 MB a second, and the archive writes large batches and keeps up with one instance.
The count is the larger of two divisions: the write rate over what one partition accepts, and the write rate over what one consumer of the slowest group handles. Every group reads the whole topic, so each group must keep up with the full 4.5 MB a second.
| Step | Arithmetic | Partitions |
|---|---|---|
| Writes today | 4.5 MB/s / 10 MB/s | 1 |
| Alert checker today | 4.5 MB/s / 0.75 MB/s | 6 |
| Writes in two years | 13.5 MB/s / 10 MB/s | 2 |
| Alert checker in two years | 13.5 MB/s / 0.75 MB/s | 18 |
| Alert checker at 75% load | 18 / 0.75 | 24 |
The reads decide this topic, not the writes. At 10 MB a second per partition, the writes need only 2 partitions even after the fleet triples, while the alert checker needs 18 consumers to keep up.
The last row adds room to catch up. A consumer group running at full capacity can keep up with new readings, yet it can never shrink a backlog. If the alert checker is down for 10 minutes at 13.5 MB a second, about 8.1 GB waits for it. With 24 consumers each running at 75 percent of capacity, the group has 4.5 MB a second spare, so it clears that backlog in about 30 minutes. With exactly 18, it never would.
The Cost of Too Many
More partitions are not free. Each partition is a log in its own folder on a broker, and with the documented durable setup of three copies, 24 partitions are 72 logs to store, replicate, and move when a broker fails.
The producer is affected too. It groups records into batches, and a request to a broker carries "one batch for each partition with data available to be sent." If the team instead created 48 partitions, twice the 24 it needs, the same 4.5 MB a second would give each partition's batch about half the records, so the producer would send smaller batches for the same data.
Readers rarely benefit beyond their need. The live map needs 2 consumers today and the archive needs 1, so most of the 24 partitions give them nothing extra. Size the topic for its slowest reader at its planned load, not for a guess at many times that load.
Adding Partitions Later
A topic that turns out too small can be grown. Kafka's operations guide describes the cost, because the default partitioner places each key by "hash(key) % number_of_partitions." When the count changes, the result of that division changes too, so "messages with the same key may be routed to different partitions after the expansion," and Kafka "will not attempt to automatically redistribute existing data."
The program below shows both effects on a real broker. It writes one reading for each of 1,000 trucks to a topic with 6 partitions, grows the topic to 18, and writes the same 1,000 trucks again. Finally it asks Kafka to go back to 6.
// Growing a topic: allowed. Existing records stay where they are. admin.createPartitions(Map.of(TOPIC, NewPartitions.increaseTo(18))).all().get(); // ... long moved = before.keySet().stream().filter(k -> !before.get(k).equals(after.get(k))).count(); // ... // Shrinking a topic: refused. admin.createPartitions(Map.of(TOPIC, NewPartitions.increaseTo(6))).all().get();
Running it against a single Kafka 4.4 broker printed the following.
Output:
first round: 1000 trucks written to 6 partitions partitions grown from 6 to 18 records still in partitions 0 to 5: 1000 second round: 649 of 1000 trucks now write to a different partition reduce to 6: InvalidPartitionsException: The topic truck-telemetry currently has 18 partition(s); 6 would not be an increase.
Three consequences follow for the telemetry topic:
- Order breaks for moved keys. Truck T-0001's older readings sit in partition 4 and its new ones in partition 10. Two consumers read those partitions at their own pace, so the alert checker can see a new reading before an older one.
- New partitions can be skipped. Kafka warns that consumers using
auto.offset.reset=latest"might miss messages produced to the new partitions" before they discover them. - There is no way back. The documentation states that "Kafka does not currently support reducing the number of partitions for a topic," and the broker refused the request in the run. The only way to reach a smaller count is a new topic, with every producer and consumer moved to it.
Growing is cheap for a topic whose readers do not depend on per-key order. The archive stores every reading regardless of order, so for an archive-only topic, starting small and adding partitions later is a reasonable plan.
Choosing the Number
| Size for today (6) | Size for planned growth (24) | |
|---|---|---|
| Logs to store and replicate | 18 with three copies | 72 with three copies |
| Producer batches | Larger, fewer requests | Smaller for the same data |
| When the fleet triples | Add partitions; about 2 in 3 keys move and order breaks for them | No change needed |
| Catching up after an outage | Never: 6 consumers already run at full capacity | About 30 minutes for a 10-minute outage |
For the telemetry topic, 24 is the better choice. The alert checker depends on per-truck order, the growth is planned rather than imagined, and the extra logs cost less than a change of key placement on a live topic.
Using This in an Interview
State the arithmetic and the assumptions together: "The topic takes 4.5 MB a second today and 13.5 in two years. Writes would fit in 2 partitions, but the alert checker handles 0.75 MB a second per instance, so it needs 18 consumers, and I would create 24 so that it can catch up after an outage."
Two follow-up questions are common:
- "Why not start with 6 and add partitions later?" Adding partitions changes where each key goes, so a truck's readings would split across two partitions and could be read out of order. Partitions can also never be removed.
- "Why not 200, to be safe?" Each partition is a replicated log to store and move, and producer batches shrink as the same records are spread over more partitions. No reader needs more than 18 consumers.
Key Takeaways
- The partition count caps how many consumers in one group can read the topic at once, and how many brokers share its writes.
- Size by data rate: divide the write rate by what one partition accepts and by what one consumer of the slowest group handles, and take the larger result.
- State each throughput figure as an assumption and measure your own; in this example the reads, not the writes, decided the count.
- Add room to catch up: a group at full capacity never shrinks a backlog.
- Partitions can be added, but keys then move and existing records stay where they are; in the run, 649 of 1,000 trucks changed partition.
- Partitions can never be removed, so a smaller count means a new topic.
The arithmetic above assumed that readings spread evenly across partitions, which is true when there are many trucks that each send about the same amount of data. The next lesson, Choosing a Partition Key, shows what happens when one key carries a large share of the traffic.
Practice Questions
Try each question first, then open the answer.
1. A topic receives 9 MB a second. Suppose one partition accepts 10 MB a second, and one consumer of the slowest group handles 1.5 MB a second. How many partitions does the topic need, ignoring any room to catch up after an outage?
<details> <summary>Show answer</summary>6 partitions. Writes need 9 / 10 = 0.9, or 1 partition. The slowest group needs 9 / 1.5 = 6 consumers, and a group cannot keep more consumers busy than there are partitions, so the reads decide the count.
</details>2. A consumer group runs at exactly its full capacity, with as many consumers as partitions. It is down for 15 minutes. What happens to the backlog when it returns, and how do you fix that in the design?
<details> <summary>Show answer</summary>The backlog never shrinks. The group processes new data exactly as fast as it arrives, so the 15 minutes of waiting data stays waiting. Sizing for a target load below 100 percent, for example 75 percent, leaves spare capacity that clears the backlog over time.
</details>3. A topic keyed by customer ID has 8 partitions, and a team grows it to 16. What happens to the customers' existing events, and to the order of each customer's events?
<details> <summary>Show answer</summary>Existing events stay where they are, and order can break for customers whose key moves. Kafka does not redistribute old data. With 8 to 16, a key stays only if its hash gives the same result for both counts, which is true for about half the keys. The other half write new events to a different partition, so a reader can see a new event before an older one.
</details>4. A team created a topic with 64 partitions and now wants 16. What are its options?
<details> <summary>Show answer</summary>Create a new topic with 16 partitions and move producers and consumers to it. Kafka does not support reducing the partition count of a topic. The old events must be copied to the new topic if readers still need them, and the copy changes which partition each key is in.
</details>5. An archive consumer reads a click-stream topic and stores events in files, and it does not care about order. The topic gets 2 MB a second today. Does it make sense to start with a small count and grow it later?
<details> <summary>Show answer</summary>Yes. The cost of adding partitions is that keys move and per-key order breaks, which does not matter to a reader that ignores order. Starting small keeps fewer logs to store, and the topic can grow when the data rate does.
</details>Reading Progress
0%
On This Page
What the Count Limits
Sizing by Data Rate
The Cost of Too Many
Adding Partitions Later
Choosing the Number
Using This in an Interview
Key Takeaways
Practice Questions