Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

A consumer must process events in order per account but one partition carries much more traffic than others and processing is slow. How can you get more parallelism without breaking per-account ordering?

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 confirm the skew by measuring lag per partition. Then the main option is to process records from one partition in parallel by key: records for different accounts go to different workers while each account's records stay in sequence, either with our own per-key queues or with a library such as Confluent Parallel Consumer. The catch is that offsets can only be committed up to the lowest unfinished record. Adding partitions helps only for new data and changes the key mapping, and a single hot account cannot be parallelised without giving up its own order.

Why this question is asked

Kafka's usual answer to 'we need more parallelism' is 'add partitions and consumers'. That answer stops working when one partition carries much more traffic than the rest, or when processing each record is slow because of a database or an external call. Interviewers ask this design question to see whether we understand that Kafka's ordering is per partition while the business needs ordering per account, and whether we can exploit the difference safely. In this answer, we will confirm the skew, look at what adding partitions really does, build key-level parallelism inside one partition, deal with the offset commit problem it creates, and handle a single hot key.

1Measure the Skew First

Before changing anything, we check that the problem really is one partition. kafka-consumer-groups.sh --describe shows lag per partition, and Spring Kafka exposes consumer metrics such as records-lag-max through Micrometer. If partition 3 has 400,000 records of lag and the other eleven have a few hundred, the topic as a whole is not short of capacity; one partition is.

Next we find out why. Skew usually comes from keys: a few large accounts producing most of the events, or many keys that happen to hash to the same partition. Counting records per key over an hour, for example with a small consumer that samples the partition, tells us which case we are in, and that decides the fix.

Properties
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group ledger
# TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
# ledger 2 8,120,400 8,120,690 290
# ledger 3 5,902,000 6,301,880 399,880 <- hot
# ledger 4 7,770,150 7,770,300 150

2What Adding Partitions Really Does

More partitions mean more consumers can work at once. But the partition for a key is hash(key) % partitionCount, so changing the count from 12 to 24 moves about half of all accounts to a different partition. Events already waiting in the old partition and new events in the new partition can then be processed in the wrong order for those accounts during the switch.

It can be done safely as a migration: create a new topic with more partitions, publish new events there, let consumers drain the old topic completely before reading the new one for each key, or pause producers briefly at the switch. That is real work, and it does not help at all if the skew is caused by one huge account, which will still land on one partition. So increasing partitions is a longer-term fix for overall growth, not a quick fix for a hot partition.

3Parallel by Key Inside One Partition

The insight is that we need order per account, not per partition. Partition 3 holds events for thousands of accounts, and events for account A and account B can be processed at the same time without breaking anything. Only events for the same account must stay in sequence.

So the consumer thread stops processing records itself. It reads a batch and dispatches each record to a worker chosen by key, for example hash(accountId) % 16. Each worker has its own queue and processes records one at a time, so all records for one account go through the same worker in the order they arrived. Sixteen workers give up to sixteen times the throughput for slow, I/O-bound processing.

Java
// Simplified: one single-threaded executor per lane keeps per-key order
ExecutorService[] lanes = IntStream.range(0, 16)
.mapToObj(i -> Executors.newSingleThreadExecutor()).toArray(ExecutorService[]::new);
void dispatch(ConsumerRecord<String, LedgerEvent> r) {
int lane = Math.floorMod(r.key().hashCode(), lanes.length); // account 77 -> lane 13 always
lanes[lane].submit(() -> ledger.apply(r.value())); // runs after earlier tasks in lane 13
}
Step throughKey-ordered parallel processing of one partition
  1. Poll a batch from partition 3offsets 100 to 199
  2. Route each record by keylane = hash(account) % 16
  3. Lanes run in paralleleach lane is single-threaded, so per-account order holds
  4. Track completed offsetssome finish out of offset order
  5. Commit up to the lowest unfinished offsete.g. 100-130 done, 131 pending -> commit 131

5 steps in order. Play them, or step through them one at a time.

Writing this correctly by hand is harder than it looks, mostly because of offsets, error handling and rebalances. That is why libraries exist: Confluent Parallel Consumer offers a KEY ordering mode that does exactly this, with concurrency in the thousands and offset tracking built in. Spring Kafka itself processes each partition on one thread, so this is something we add around or instead of a plain listener.

4The Offset Commit Problem

Kafka stores one committed offset per partition, meaning 'everything before this is done'. With parallel lanes, records finish out of offset order: lane 2 may finish offset 150 while lane 13 is still working on offset 131. We may only commit 131, because committing 151 would claim that 131 is done. If the service crashed, offset 131 would then never be processed.

So we keep track of completed offsets per partition and commit only up to the lowest one still in progress. After a crash, everything from that point is processed again, including records that had in fact finished, so the consumer must be idempotent. One slow record also holds back the commit point, which means more reprocessing after a crash, and we need back-pressure: if lanes fall far behind, the consumer must pause fetching rather than keep millions of records in memory. Parallel Consumer handles this by storing extra completion data in the commit metadata, but the idempotency requirement remains.

5When One Key Is the Hot Spot

If the skew comes from a single account producing most of the partition's events, key-level parallelism does not help: all of that account's events go through one lane, one at a time, because that is what per-account order requires. The only way to parallelise that key is to give up some of its order, which the business may not allow.

The options then change. We can reduce the work per event: batch the database writes for that account, cache lookups, or move slow side calls out of the ordered path. Or we can split the key: if the account's events are really about independent sub-entities, such as separate wallets or cards inside one corporate account, keying by account plus wallet restores parallelism while keeping the order that actually matters. Both need a conversation with the business about what 'in order' means for that account.

6Follow-up questions

Why not just increase listener concurrency?

Because concurrency in Spring Kafka creates more consumers, and Kafka assigns each partition to only one consumer in a group. With 12 partitions, concurrency above 12 leaves the extra consumers idle, and partition 3 is still handled by a single thread.

How do rebalances interact with parallel lanes?

When partition 3 is revoked, the consumer must stop dispatching its records, wait for the lanes to finish or drop their queued work, and commit the safe offset before giving the partition up. Otherwise the new owner and the old lanes could both process the same records. This is one of the main reasons to prefer a library over hand-written lanes.

Is Kafka's share groups feature an answer here?

Share groups, from KIP-932 and introduced as a preview around Kafka 4.0, let several consumers take records from the same partition with per-record acknowledgement, like a queue. They do not preserve per-key order, so they suit independent work items rather than an ordered ledger. Check your version for its status.

7Conclusion

In this answer, we saw that a hot partition is a per-partition limit while the business only needs per-account order. After measuring per-partition lag and finding the cause, we can process one partition in parallel by key with per-key lanes or Confluent Parallel Consumer, committing only up to the lowest unfinished offset and keeping the consumer idempotent. Adding partitions helps new data but remaps keys and needs a migration, and a single hot key can only be helped by doing less work per event or splitting the key.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd first confirm it with per-partition lag, from kafka-consumer-groups describe or the records-lag metrics, and then look at the key distribution to see whether it's many keys or one huge account.

Then, if they want more

Adding partitions is the obvious idea, but the key mapping is hash modulo the partition count, so changing it moves keys and can reorder events during the switch; it's a planned migration, and it doesn't help a single hot key at all. The more direct fix is that we only need ordering per account, not per partition.

So within one partition I can process in parallel by key: the consumer dispatches each record to a lane chosen by hashing the account id, each lane is single-threaded, so one account's events stay in order while different accounts run concurrently. I'd rather use a library like Confluent Parallel Consumer in key-ordering mode than hand-roll it.

The catch is commits: records finish out of offset order, and I can only commit up to the lowest unfinished offset, so after a crash some finished records are replayed, which means the consumer has to be idempotent, and I need back-pressure and careful handling of rebalances.

If one account is the hot spot, nothing can parallelise it without breaking its order, so I'd reduce work per event, for example batching writes, or split the key if the business allows it.

About this answer

Last updated
12 November 2026
Versions it names
Kafka 4.0
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.
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 "A consumer must process events in order per account but one partition carries much more...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/152ee3dfff/