Quick revision
The short answer
The default partitioner hashes the serialized key with murmur2 and takes it modulo 48, so 20 warehouse ids can reach at most 20 partitions, and hash collisions put several warehouses on the same partition. The expected number of distinct partitions for 20 keys over 48 is 48 x (1 - (47/48)^20), about 16.5, which matches the 16 we see, and partitions that drew three warehouses run three times hotter. Consumers beyond the active partitions get nothing, so parallelism is capped by key cardinality. We should key at the level where ordering is really needed, probably warehouse plus SKU, and roll the change out to a new topic or at a quiet point.
What to remember
In this answer, we saw that the default partitioner sends each key to murmur2(key) modulo 48, so 20 warehouse keys can use at most 20 partitions, and random collisions mean about 48 x (1 - (47/48)^20), roughly 16.5, partitions are used in expectation, with some holding three warehouses and running three times hotter. Consumers beyond the active partitions sit idle, because parallelism is capped by key cardinality. Keying by warehouse plus SKU, or SKU alone, gives thousands of keys that spread evenly, and the switch goes to a new topic or happens at a quiet point with consumers that tolerate it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The default partitioner hashes the serialized key with murmur2 and takes it modulo 48, so 20 warehouse ids can reach at most 20 partitions, and hash collisions put several warehouses on the same partition. The expected number of distinct partitions for 20 keys over 48 is 48 x (1 - (47/48)^20), about 16.5, which matches the 16 we see, and partitions that drew three warehouses run three times hotter. Consumers beyond the active partitions get nothing, so parallelism is capped by key cardinality. We should key at the level where ordering is really needed, probably warehouse plus SKU, and roll the change out to a new topic or at a quiet point.
Why this question is asked
This is a satisfying debugging question because the symptoms look like a broken consumer group, but the cause is arithmetic. Teams often scale a topic to 48 partitions and 48 consumers expecting 48-way parallelism, without asking how many distinct keys will feed it. Interviewers ask it to see whether we understand how keys become partitions, and whether we can explain the observed numbers rather than just guessing. In this answer, we will recap the default partitioner, compute the expected partition spread, see why 32 consumers are idle, choose a better key, and plan the switch.
1A Quick Recap of the Default Partitioner
When a record has a key, the Java producer's default partitioning takes the serialized key bytes, hashes them with the murmur2 hash function, makes the result positive, and takes it modulo the number of partitions. The same key bytes always give the same partition while the partition count is unchanged; that is what keeps each key's records in order.
Notice the consequence: the number of partitions that can receive data is at most the number of distinct keys. With 20 warehouses, at most 20 of the 48 partitions can ever be used, however many consumers we start.
The exact spread depends on the actual ids, but running this small calculation over the real warehouse ids reproduces the monitoring picture exactly, which is a good way to confirm the diagnosis.
2Why Only About 16 Partitions Are Used
Even 20 keys rarely reach 20 different partitions, because hashing behaves like throwing balls into bins at random, and collisions are common. For one particular partition, the chance that a given key misses it is 47/48. The chance that all 20 keys miss it is (47/48)^20, about 0.656. So the chance that the partition receives at least one key is about 0.344.
Multiplying by 48 partitions gives the expected number of partitions in use: 48 x (1 - (47/48)^20), about 48 x 0.344 = 16.5. Our 16 is exactly what we should expect. This is the same effect as the birthday problem: with 20 people and 48 possible days, shared days are almost certain.
Collisions also explain the hot partitions. Twenty warehouses on 16 partitions means some partitions hold more than one warehouse. If two partitions each drew three warehouses, they carry about three times the traffic of a partition with one warehouse, assuming warehouses are similarly busy.
| Quantity | Value |
|---|---|
| Partitions | 48 |
| Distinct keys (warehouses) | 20 |
| Chance a partition gets no key | (47/48)^20 = 0.656 |
| Expected partitions in use | 48 x 0.344 = 16.5 |
| Observed partitions in use | 16 |
| Idle consumers in a 48-member group | 32 |
3Why 32 Consumers Are Idle
In a consumer group, each partition is assigned to exactly one consumer. With 48 partitions and 48 consumers, each consumer gets one partition. The 32 consumers whose partitions receive no records simply poll and get nothing, and the two consumers on the hot partitions do three times the work of the others.
Adding more consumers or more partitions does not help. Parallelism here is capped by key cardinality, the number of distinct keys, not by partition count or consumer count. With 20 keys, no layout can give more than 20-way parallelism, and hashing gives about 16.
4Choosing a Better Key
The key should be the smallest unit that needs ordering. The question to ask is: which records must be processed in order relative to each other? For inventory, it is usually the stock of one SKU in one warehouse: a reservation and a release for SKU A1 in warehouse W3 must apply in order, but A1 in W3 and B7 in W3 are independent.
So a key of warehouse id plus SKU, such as W3:A1, keeps the order that matters and has thousands or millions of distinct values, which hash evenly across all 48 partitions. If inventory is global per SKU, the SKU alone may be the right key. With thousands of keys, each partition receives roughly 1/48 of the traffic and all 48 consumers work.
5Rolling Out the New Key Safely
Changing the key changes which partition a given stock item lands on. During a switch on the same topic, old records for W3:A1 may sit on one partition while new ones go to another, and a consumer could apply them out of order. There are two safe ways.
The cleaner one is a new topic, inventory.v2, keyed the new way. Producers switch to it, consumers first drain the old topic to the end, then start on the new one, so no stock item has records in both places being processed at the same time. The other is to switch at a quiet point: stop producers briefly, let consumers catch up to zero lag, then restart producers with the new key. In both cases consumers should tolerate the switch, for example by checking a per-item version so a late old event is ignored.
6Follow-up questions
Could we write a custom partitioner that assigns each warehouse its own partition?
It would spread 20 warehouses over 20 partitions without collisions, but parallelism would still be capped at 20, and a busy warehouse would still make its partition hot. It also creates a mapping to maintain when warehouses are added. A finer key solves the actual problem, which is too few keys.
Does the sticky partitioner change any of this?
No. The sticky behaviour applies only to records without a key, where the producer fills a batch for one partition before moving to another. Keyed records always go to the hash-chosen partition.
7Conclusion
In this answer, we saw that the default partitioner sends each key to murmur2(key) modulo 48, so 20 warehouse keys can use at most 20 partitions, and random collisions mean about 48 x (1 - (47/48)^20), roughly 16.5, partitions are used in expectation, with some holding three warehouses and running three times hotter. Consumers beyond the active partitions sit idle, because parallelism is capped by key cardinality. Keying by warehouse plus SKU, or SKU alone, gives thousands of keys that spread evenly, and the switch goes to a new topic or happens at a quiet point with consumers that tolerate it.
Open with
The cause is the key, not the consumers. The default partitioner hashes the serialized key with murmur2 and takes it modulo the partition count, so with 20 warehouse ids at most 20 of the 48 partitions can ever get data, and hash collisions put several warehouses on the same partition.
Then, if they want more
The expected number of distinct partitions for 20 keys over 48 is 48 times one minus 47 over 48 to the power 20, which is about 16.5, so seeing 16 is exactly what we should expect. The two hot partitions are the ones that drew three warehouses each.
In a consumer group each partition goes to one consumer, so the 32 consumers on empty partitions are idle, and adding consumers or partitions will not help: parallelism is capped by key cardinality. I would choose the key at the level where ordering is actually needed.
For inventory that is probably warehouse plus SKU, or SKU alone, which has thousands of values and spreads evenly.
Because changing the key moves records to different partitions, I would roll it out to a new topic, letting consumers drain the old one first, or switch at a quiet point with zero lag, and make consumers tolerate the switch with a per-item version check.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "The consumer group has 48 consumers, but monitoring shows only 16 partitions receiving...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/87458e590b/