Scrivio Interview library
DesignAdvanced7 min read · updated 13 November 2026

In a multi-tenant SaaS event pipeline keyed by tenant id, 3 of 4,000 tenants produce 45 percent of all events, and the partitions they hash to are lagging while others are idle.

Design a partitioning approach that balances load while keeping the ordering the business actually needs.

Try it yourself

Consumer group: who reads which partition

Change the number of partitions and consumers, and see who owns what, who is idle, and where ordering holds.

Read this example as text

Who reads which partition

  1. 3 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2.
  2. 4 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2, P3.
  3. 3 partitions, 3 consumers. C0 reads P0; C1 reads P1; C2 reads P2.
  4. 6 partitions, 4 consumers. C0 reads P0, P1; C1 reads P2, P3; C2 reads P4; C3 reads P5.
  5. 2 partitions, 4 consumers. C0 reads P0; C1 reads P1; C2 is idle; C3 is idle.

A partition has exactly one reader in the group. A consumer beyond the number of partitions is idle.

Six records with 3 partitions and 2 consumers

  1. Record A1. Key A goes to P0, read by C0.
  2. Record B1. Key B goes to P1, read by C0.
  3. Record A2. Key A goes to P0, read by C0.
  4. Record C1. Key C goes to P2, read by C1.
  5. Record B2. Key B goes to P1, read by C0.
  6. Record A3. Key A goes to P0, read by C0.

A1, A2 and A3 sit in one partition and are read in that order. Nothing orders a record in one partition against a record in another.

What this example leaves out
  • One topic, and Kafka's range assignor, the first in the default list. Other assignors spread partitions differently.
  • A, B and C stand for key hashes 0, 1 and 2. The real client hashes the key bytes; the rule shown, same key to the same partition, is the same.
  • A rebalance is shown as instant. In a real group, consumers stop briefly while partitions move.

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.

Java
// Key by tenant and entity: a big tenant now spreads over many partitions (48 partitions)
producer.send(new ProducerRecord<>("tenant-events", "tenant-17:order-9001", e1)); // partition 41
producer.send(new ProducerRecord<>("tenant-events", "tenant-17:order-9002", e2)); // partition 8
producer.send(new ProducerRecord<>("tenant-events", "tenant-17:order-9003", e3)); // partition 16
// Before: key "tenant-17" -> every event in partition 22

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.

Java
public class TenantRangePartitioner implements Partitioner {
private Map<String, int[]> ranges; // "tenant-A" -> {0, 8}: start, size; loaded from config
public void configure(Map<String, ?> cfg) { ranges = RangeConfig.parse(cfg.get("tenant.ranges")); }
public int partition(String topic, Object key, byte[] keyBytes, Object v, byte[] vb, Cluster c) {
String[] parts = ((String) key).split(":", 2); // tenant:entity
int n = c.partitionCountForTopic(topic);
int[] r = ranges.get(parts[0]);
byte[] entity = parts[1].getBytes(StandardCharsets.UTF_8);
if (r != null) return r[0] + Utils.toPositive(Utils.murmur2(entity)) % r[1]; // inside the tenant's range
int shared = n - RangeConfig.reserved(ranges);
return RangeConfig.reserved(ranges) + Utils.toPositive(Utils.murmur2(keyBytes)) % shared;
}
public void close() {}
}
Options for a hot tenant
ApproachLoad spreadOrdering keptCost
Key by tenant (today)Poor: 3 partitions hotPer tenantNone, but lag
Key by tenant + entityGoodPer entityRe-key migration
Reserved ranges per big tenantGood within rangePer entity in rangeCustom partitioner everywhere
Topic per big tenantGood, isolatedPer tenant or entityMore topics to operate
Salted key (key + bucket)GoodOnly within a bucketConsumers 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.

What to say in the interviewAbout 90 seconds aloud

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/