Scrivio Interview library
DesignIntermediate6 min read · updated 13 November 2026

A slow external call for every record limits how fast a simple consumer can go, yet the business still needs ordering per account and a firm throughput target.

Design a consumer for a 6-partition topic where each record needs a 150 ms call to an external service, ordering must be kept per account id, and the target is 2,000 records per second. Describe the threading, how offsets are committed, and what happens on rebalance.

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.

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.

Java
int lanes = 400;
ExecutorService[] lane = new ExecutorService[lanes];
for (int i = 0; i < lanes; i++) lane[i] = Executors.newSingleThreadExecutor(Thread.ofVirtual().factory());
void dispatch(ConsumerRecord<String, Payment> r) {
int i = Math.floorMod(r.value().accountId().hashCode(), lanes); // account 17 -> always the same lane
tracker(r).started(r.offset());
lane[i].submit(() -> { callExternalService(r.value()); tracker(r).finished(r.offset()); });
}
Step throughInside one consumer instance
  1. Poll threadPolls 6 partitions, dispatches, commits, pauses and resumes
  2. Per-partition offset trackersRecord which offsets are started and finished
  3. 400 lanes by hash(accountId)One worker each, so each account runs in order
  4. 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.

Java
class PartitionTracker {
private final TreeSet<Long> inFlight = new TreeSet<>();
private long highestSeen = -1;
synchronized void started(long o) { inFlight.add(o); highestSeen = Math.max(highestSeen, o); }
synchronized void finished(long o) { inFlight.remove(o); }
synchronized long committable() { return inFlight.isEmpty() ? highestSeen + 1 : inFlight.first(); }
}
// started 10, 11, 12; finished 11 and 12 -> committable() = 10
// finished 10 -> committable() = 13

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.

Step throughRevoking a partition safely
GroupPoll threadLanesKafka offsets
onPartitionsRevoked(P3)
stop dispatching P3 records
wait up to 5 s for P3 calls
offsets 10..14 finished, 15 still running
commit P3 at 15
drop queued P3 records
partition released

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.

What to say in the interviewAbout 90 seconds aloud

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/