Quick revision
The short answer
Six partitions processed one record at a time give only about 40 records per second, so the parallelism has to come from inside the consumer: about 300 calls in flight at once. We route each record to one of a few hundred single-threaded lanes by a hash of the account id, so one account stays in order, track completed offsets per partition and commit only below the oldest unfinished record, pause partitions when lanes fill up, and on rebalance commit what is finished and rely on idempotent processing for the rest.
What to remember
In this answer, we saw that 150 ms per record caps a sequential consumer at about 40 records per second on 6 partitions, and that 2,000 per second needs about 300 calls in flight. Lanes chosen by a hash of the account id give that parallelism while keeping each account in order. A per-partition tracker commits only below the oldest unfinished offset, pause() and resume() bound the queued work, and on rebalance we commit what finished and rely on idempotent calls for the records that are redelivered.
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.
Six partitions processed one record at a time give only about 40 records per second, so the parallelism has to come from inside the consumer: about 300 calls in flight at once. We route each record to one of a few hundred single-threaded lanes by a hash of the account id, so one account stays in order, track completed offsets per partition and commit only below the oldest unfinished record, pause partitions when lanes fill up, and on rebalance commit what is finished and rely on idempotent processing for the rest.
Why this question is asked
A slow call per record is the most common reason a Kafka consumer cannot keep up, and the first instinct, adding partitions, is usually the wrong lever. Interviewers ask this design because it tests whether we can do the arithmetic, keep ordering where the business needs it, and avoid the classic bug of committing offsets for work that has not finished. In this answer, we will work out the numbers, design the lanes that give parallelism with per-account ordering, build the offset tracking, add back-pressure, handle rebalances, and finish with how we would test it.
1Doing the Arithmetic First
A partition is an ordered log, and a consumer group gives each partition to exactly one consumer. The simplest consumer handles a partition one record after another, so a 150 ms call means at most 1 / 0.15, about 6.7 records per second per partition. With 6 partitions that is roughly 40 records per second, fifty times below the target of 2,000.
To find how much concurrency we need, we use Little's law: work in flight equals arrival rate times time per item. 2,000 records per second times 0.15 seconds is 300 calls in flight at any moment. Adding partitions to reach that would mean 300 partitions and 300 consumers, which is expensive and still fragile. So the plan is to keep 6 partitions and run about 300 calls concurrently inside the consumer instances, with headroom on top.
2Lanes Keyed by Account Id
Ordering only matters per account, not per partition. So we hash the account id to pick a lane, a small single-threaded queue with its own worker. Records for account 17 always land in the same lane and run one after another, while records for different accounts run in parallel in other lanes.
With 400 lanes we have room above the 300 calls we need, which absorbs uneven account distribution. Each lane can be a platform thread, but on Java 21 and later a virtual thread per lane is cheap and suits blocking HTTP calls well.
- Poll threadPolls 6 partitions, dispatches, commits, pauses and resumes
- Per-partition offset trackersRecord which offsets are started and finished
- 400 lanes by hash(accountId)One worker each, so each account runs in order
- External serviceAbout 300 calls in flight at 2,000 records per second
4 layers, from the top down. Step through them one at a time.
Notice that the poll loop never makes the external call itself. It only dispatches, so it stays fast and keeps calling poll() well inside max.poll.interval.ms. The KafkaConsumer is not thread-safe, so only the poll thread ever touches it; the lanes report completion through the tracker.
3Committing Only What Is Really Finished
Lanes finish out of order. Offset 12 can finish before offset 10 because they belong to different accounts. If we committed 13 at that moment and then crashed, offset 10 would never be processed. So each partition keeps the set of offsets still in flight, and the offset we may commit is the oldest unfinished one. Remember that a Kafka commit stores the next offset to read, not the last one processed.
The poll thread calls committable() for each partition every second or so and commits with commitAsync, plus a commitSync on shutdown. A few hundred milliseconds of extra redelivery after a crash is the price, and that is fine because processing is idempotent.
4Back-Pressure With pause() and resume()
If the external service slows to 500 ms, records arrive faster than lanes drain, and an unbounded queue would grow until the JVM runs out of memory. So we bound the work: when the total queued records pass a limit, say 4,000, the poll thread calls consumer.pause() on the partitions and keeps calling poll(). A paused partition returns no records, but polling keeps the consumer alive in the group. When the queues fall below 2,000, it calls resume(). The gap between the two thresholds stops the consumer flapping between states.
5What Happens on Rebalance
A rebalance moves partitions between consumers, for example when an instance starts or stops. In a ConsumerRebalanceListener, onPartitionsRevoked runs before we lose the partitions. There we stop dispatching new records for them, wait a short bounded time (for example 5 seconds) for in-flight calls, commit the committable offset for each, and drop whatever is still queued. The new owner will start from our commit, so the dropped and unfinished records are delivered again.
That is why idempotency is not optional here. The external call should carry a request id, such as topic, partition and offset, or a business id, so the service can ignore a repeat. With the cooperative rebalancing in recent Kafka versions only the moved partitions are revoked, so the other partitions keep flowing during the pause.
7 messages in order. Play them, or step through them one at a time.
Records from 15 onward are delivered again to the new owner, so the call must be idempotent.
6Follow-up questions
How would you test that commits never skip an unfinished record?
We test the tracker on its own first, with a property-based test: generate random offsets, finish them in random orders, and after every step assert that committable() is never greater than the smallest unfinished offset. Then we run an integration test with Testcontainers Kafka and a fake external service that records every call and randomly delays or fails some of them. We kill the consumer at random points, restart it, and at the end assert that every offset produced was processed at least once and that per-account order held. A deliberate mutation, such as committing highestSeen + 1, should make that test fail, which proves the test can catch the bug.
Why not just set the listener concurrency to 300?
Concurrency in Spring Kafka creates consumers, and each consumer needs at least one partition. With 6 partitions, 294 of those consumers would sit idle. Concurrency above the partition count adds nothing.
Is there a library that does this?
Yes. The Confluent Parallel Consumer, an open-source library, implements key-ordered parallel processing with offset tracking, and it is worth evaluating before writing our own, because the offset bookkeeping is where home-grown versions usually fail.
7Conclusion
In this answer, we saw that 150 ms per record caps a sequential consumer at about 40 records per second on 6 partitions, and that 2,000 per second needs about 300 calls in flight. Lanes chosen by a hash of the account id give that parallelism while keeping each account in order. A per-partition tracker commits only below the oldest unfinished offset, pause() and resume() bound the queued work, and on rebalance we commit what finished and rely on idempotent calls for the records that are redelivered.
Open with
First the numbers: sequential processing gives one over 0.15, about 6.7 records per second per partition, so 40 per second on six partitions. Little's law says 2,000 per second at 150 ms needs about 300 calls in flight, so I would not add partitions; I would add concurrency inside the consumer.
Then, if they want more
The poll thread dispatches each record to one of about 400 single-threaded lanes chosen by a hash of the account id, using virtual threads on a recent Java. That keeps each account in order and runs different accounts in parallel.
Because lanes finish out of order, I keep a tracker per partition with the offsets in flight, and I commit only the oldest unfinished offset, never past it. To bound memory, when the queues pass a threshold I pause the partitions and keep polling so the consumer stays in the group, and resume when they drain.
On rebalance, in onPartitionsRevoked I stop dispatching for those partitions, wait a few seconds for in-flight calls, commit what is finished and drop the rest. Those records come back to the new owner, so the external call must be idempotent, for example with a request id.
I would also look at the Confluent Parallel Consumer before building this myself, and I would test the tracker with randomised completion orders plus a crash-and-restart integration test.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Java 21
- 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 consumer for a 6-partition topic where each record needs a 150 ms call to an...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/46c843b7ee/