Quick revision
The short answer
First find the ordering the business really needs; it is usually per entity within a tenant, such as an order or an account, not per tenant. Keying by tenant plus entity then spreads large tenants across many partitions while keeping each entity in order. Where some tenant-wide order is truly required, we isolate the largest tenants in their own topics or in reserved partition ranges with a deterministic custom partitioner used by every producer. Salting a hot key spreads it but breaks order across buckets, partitioning changes are planned as migrations, and per-partition bytes and lag reveal new hot tenants early.
What to remember
In this answer, we saw that tenant keys concentrate a few large tenants on a few partitions, so the fix starts with the ordering the business really needs. Usually that is per entity, and a tenant-plus-entity key spreads load while keeping each entity in order. Where tenant-wide order is needed, large tenants get their own topics or reserved ranges through a deterministic custom partitioner used by every producer. Salting is a last resort because it breaks order across buckets. Partitioning changes are migrations, and per-partition bytes and lag reveal the next hot tenant early.
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.
First find the ordering the business really needs; it is usually per entity within a tenant, such as an order or an account, not per tenant. Keying by tenant plus entity then spreads large tenants across many partitions while keeping each entity in order. Where some tenant-wide order is truly required, we isolate the largest tenants in their own topics or in reserved partition ranges with a deterministic custom partitioner used by every producer. Salting a hot key spreads it but breaks order across buckets, partitioning changes are planned as migrations, and per-partition bytes and lag reveal new hot tenants early.
Why this question is asked
Keying by tenant id feels natural in a multi-tenant system: each tenant's events stay together and in order. It works until a few tenants grow far larger than the rest, and then their partitions lag while most of the cluster sits idle. Adding partitions does not help, because each tenant still hashes to one partition. Interviewers ask this design question to see whether we start from the business's ordering need instead of the data's current shape. In this answer, we will recap why the skew happens, question the ordering requirement, look at re-keying, isolation with a custom partitioner and salting, and then cover how to change partitioning safely and watch for the next hot tenant.
1A Quick Recap: Why One Tenant Overloads One Partition
With tenant id as the key, the producer hashes the key and every event for a tenant goes to one partition. That partition is read by one consumer in each group, so a tenant's whole event rate must be handled by a single consumer thread. Three tenants produce 45 percent of all events, so up to three partitions carry nearly half the load, and their consumers fall behind while consumers of the other partitions have nothing to do.
Hashing spreads keys evenly, but it cannot spread traffic that is concentrated in a few keys. The fix has to change what the key is, or route large keys differently.
2Start From the Ordering the Business Needs
The tenant key was chosen to keep a tenant's events in order. The real question is which events must be ordered relative to each other. For an invoice service, events for one invoice must be in order (created, updated, paid), but there is rarely any meaning in the order between two different invoices of the same tenant. Most business rules are per entity: an order, an account, a device, a user.
If that is true here, we key by tenant plus entity, for example tenant-17:order-9001. Each order stays in one partition and in order, while a large tenant's thousands of orders spread across all partitions.
Consumers that need tenant-level views, such as a per-tenant count, can still build them, because aggregations over many partitions do not need cross-entity order.
3When Tenant-Wide Order Is Required: Isolate the Giants
Some flows really do need tenant-wide order, for example an audit log that must be replayed exactly. For those, we isolate the largest tenants. The simplest option is a topic per large tenant, with enough partitions for its load if it can be split by entity, or a single fast partition if it truly cannot. A second option keeps one topic but reserves partition ranges: partitions 0 to 7 for tenant A, 8 to 15 for tenant B, and the rest shared by everyone else, with a custom partitioner that routes each tenant to its range and hashes the entity within it.
| Approach | Load spread | Ordering kept | Cost |
|---|---|---|---|
| Key by tenant (today) | Poor: 3 partitions hot | Per tenant | None, but lag |
| Key by tenant + entity | Good | Per entity | Re-key migration |
| Reserved ranges per big tenant | Good within range | Per entity in range | Custom partitioner everywhere |
| Topic per big tenant | Good, isolated | Per tenant or entity | More topics to operate |
| Salted key (key + bucket) | Good | Only within a bucket | Consumers must merge or not care |
The partitioner must be deterministic and must be used by every producer of the topic, in every language. If one producer uses the default partitioner, its records land in the wrong place, as with any mismatch in partitioning. For non-Java producers that means an equivalent implementation and a shared test with sample keys.
4Salting, and Why It Is the Last Resort
Salting adds a bucket number to a hot key, for example tenant-17#0 to tenant-17#7, chosen randomly or round-robin, so one tenant spreads over eight partitions. It is simple, but events of the same tenant, and even of the same entity, can now land in different buckets and be processed out of order. It is acceptable only where consumers do not need that order, such as metrics or counters that are summed, or where they can merge the buckets using a sequence number. If entity order matters, salting by entity id instead of by random bucket is just the tenant-plus-entity key again.
5Changing Partitioning Safely and Watching for New Hot Tenants
Changing the key, the partitioner or the partition count remaps keys, so events of one entity written before and after the change may sit in different partitions. We treat it as a migration: create a new topic with the new rule, have producers switch at a controlled point (or write to both for a while), let consumers finish the old topic before reading the new one for the same entities, and then retire the old topic.
Finally, we monitor bytes in and consumer lag per partition, and the top tenants by event rate. A new tenant that grows into the top few shows up there weeks before it causes lag, and with a routing table in the partitioner's configuration, isolating it is a planned change rather than an incident.
6Follow-up questions
How would you move an existing tenant to its own partitions without breaking its order?
Make the switch at a barrier. The partitioner reads a versioned routing table. At cutover, the tenant's producers pause briefly (or buffer), we record the end offsets of the partitions the tenant used, and we wait until the consumers have processed past those offsets. Then we activate the new routing and resume producers. Because no event is written to the new partitions until everything in the old ones has been consumed, each entity's events stay in order. If a pause is not acceptable, producers can attach per-entity sequence numbers and consumers hold events from the new partitions until the old ones pass the barrier, which is more complex but avoids stopping producers.
Why not just add more partitions?
With tenant id as the key, each tenant still maps to one partition, so a giant tenant stays on one partition no matter how many we add. More partitions also remap existing keys, which breaks order across the change. Partitions help only once the key itself is fine-grained enough.
7Conclusion
In this answer, we saw that tenant keys concentrate a few large tenants on a few partitions, so the fix starts with the ordering the business really needs. Usually that is per entity, and a tenant-plus-entity key spreads load while keeping each entity in order. Where tenant-wide order is needed, large tenants get their own topics or reserved ranges through a deterministic custom partitioner used by every producer. Salting is a last resort because it breaks order across buckets. Partitioning changes are migrations, and per-partition bytes and lag reveal the next hot tenant early.
Open with
The first thing I'd do is challenge the requirement behind the key. Tenant id was chosen to keep a tenant's events in order, but usually the business only needs order per entity inside a tenant: per order, per account, per invoice.
Then, if they want more
Nobody cares whether invoice 1 and invoice 2 of the same tenant are processed in order. If that's the case, I key by tenant plus entity, so a giant tenant's events spread across all partitions while each order stays in sequence. That alone usually fixes the skew.
If some flows truly need tenant-wide order, I isolate the largest tenants: either their own topics, or reserved partition ranges on the shared topic with a custom partitioner that routes a big tenant to its range and hashes the entity inside it.
That partitioner has to be deterministic, driven by a shared routing config, and used by every producer of the topic in every language, with a shared test. Salting, adding a bucket number to the hot key, spreads load but breaks order across buckets, so I'd only use it for things like counters that don't need order.
Any change of key, partitioner or partition count remaps keys, so I'd run it as a migration with a cutover barrier, not an in-place change. And I'd monitor bytes in and lag per partition plus top tenants by volume, so the next big tenant gets isolated before it causes lag.
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 "Design a partitioning approach that balances load while keeping the ordering the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/f93a83e234/