Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

A single Kafka consumer handles its partitions on one thread, so a service that needs more throughput than that has to add parallelism somewhere.

Compare three ways to process Kafka records in parallel in a Java service: one consumer per thread, one consumer feeding a worker pool, and key-based parallel processing. What does each give up in ordering and offset safety?

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.

One consumer per thread (Spring's concurrency setting) is simple and keeps per-partition order and correct commits, but parallelism stops at the partition count. One consumer feeding a worker pool goes beyond partitions, but records finish out of order, so the safe commit is the highest offset below which everything is done, and per-key order is lost unless work is routed by key. Key-based processing routes records with the same key to the same worker queue, keeping per-key order while different keys run in parallel, and commits again follow the lowest unfinished offset; libraries such as the Confluent Parallel Consumer implement it.

Why this question is asked

Sooner or later, a service needs more throughput than one thread per partition can give, and the way we add parallelism decides what we can still promise about ordering and about not losing or repeating records. Interviewers ask this comparison to see whether we know the price of each model, not just its speed. In this answer, we will recap the single-thread baseline, then go through the three models, how each commits offsets safely, how the polling thread stays in the group, and which libraries and newer Kafka features help.

1A Quick Recap: One Consumer, One Thread

A KafkaConsumer must be used from a single thread, and it processes the records of its assigned partitions in order. A committed offset means 'everything before this has been processed'. With one thread processing records in order and committing after each batch, that meaning is automatically true, and records with the same key, which always land in the same partition, are processed in the order they were written.

The cost is that each partition is handled by one thread at a time, so per-partition throughput is limited by how fast one thread can process records.

2Model 1: One Consumer per Thread

The first way to scale is to run more consumers in the group, each on its own thread. In Spring Kafka, setting concurrency on the listener container creates that many consumers in one process.

Java
@KafkaListener(id = "payments", topics = "payments", concurrency = "6") // 6 consumers, 6 threads
void onPayment(ConsumerRecord<String, Payment> rec) {
ledger.apply(rec.value()); // in order per partition; commit after processing
}
// topic has 12 partitions -> each thread owns 2
// concurrency 20 on 12 partitions -> 8 threads sit idle

Notice what we keep: per-partition order, simple commits, and nothing new to reason about. What we give up is headroom: parallelism stops at the partition count, and adding partitions later changes which partition a key maps to, which is disruptive for keyed topics.

3Model 2: One Consumer Feeding a Worker Pool

The second model keeps one consumer per process and hands each record to a thread pool. Now a few partitions can keep dozens of threads busy, which is useful when processing is slow I/O, such as an HTTP call.

But records now finish out of order. Suppose offsets 100 to 104 are in the pool and 101, 102 and 104 finish first. Committing 105 would claim that 100 and 103 are done; a crash now would lose them. The safe commit is the highest offset below which every record is complete, here 100, since 100 itself is not done. When 100 finishes, the commit can move to 103. Each partition needs this small bookkeeping. Per-key order is also lost: two events for the same account can run at the same time on different threads, and the later one can finish first.

Which offset is safe to commit for one partition
OffsetStatusCommit after this step
100running100 (nothing before it is unfinished)
101donestill 100
102donestill 100
103runningwhen 100 finishes: 103
104donewhen 103 finishes: 105
The commit is always the lowest unfinished offset, so finished work above a gap may run again after a crash.

4Model 3: Key-Based Parallel Processing

The third model keeps the worker pool but routes records by key: every record with key 'account-42' goes to the same queue, processed by one worker at a time, while records with different keys go to other queues and run in parallel. Per-key order is preserved, which is usually the order that matters for business correctness, and parallelism can far exceed the partition count, since one partition contains many keys.

Commits follow the same rule as model 2, the lowest unfinished offset per partition, because records of different keys from the same partition still finish out of order. A hot key, one that receives a large share of traffic, limits throughput for that key, just as a hot partition does in model 1.

5Keeping the Polling Thread in the Group

In models 2 and 3, the consumer's polling thread must not wait for workers. It has to keep calling poll() within max.poll.interval.ms, or it will be removed from the group. When worker queues fill up, the polling thread calls pause() on its partitions, continues polling (which returns nothing for paused partitions), and calls resume() once the queues have room. On a rebalance, it must wait for or abandon in-flight records of revoked partitions and commit only what is complete.

This bookkeeping is easy to get subtly wrong, which is why libraries exist for it. The open-source Confluent Parallel Consumer implements key-based and unordered parallelism on top of the standard consumer, tracking per-record completion and encoding it into commit metadata so that, after a restart, it does not reprocess records it knows finished above the gap.

Kafka is also adding share groups (KIP-932), where several consumers can receive records from the same partition and acknowledge each record individually. They give queue-like parallelism without client bookkeeping, but without per-key order, and are still maturing, so check your Kafka version's status before relying on them.

The three models compared
ModelParallelismOrderingOffset safety
One consumer per threadup to partition countper partitionsimple commits
Consumer + worker poolbeyond partitionsnonecommit lowest unfinished offset
Key-based parallelbeyond partitionsper keycommit lowest unfinished offset

6Follow-up questions

Which model would you choose by default?

Model 1, until it is not enough. It is the simplest, Spring supports it with one setting, and it keeps every guarantee. When partitions become the limit and processing is I/O-bound, key-based processing, preferably through a library, keeps per-key order while adding parallelism.

Does key-based processing remove the need for idempotence?

No. After a crash, records that finished above the lowest unfinished offset may be processed again, because the commit could not move past the gap. Processing must still tolerate duplicates.

7Conclusion

In this answer, we compared three ways to add parallelism. One consumer per thread keeps order and simple commits but stops at the partition count. A worker pool goes beyond partitions but loses order and must commit only the lowest unfinished offset per partition. Key-based processing keeps per-key order while running different keys in parallel, with the same commit rule. In both pool models, the polling thread keeps polling and pauses partitions when queues fill, and libraries such as the Confluent Parallel Consumer, or share groups as they mature, take on the bookkeeping.

What to say in the interviewAbout 90 seconds aloud

Open with

The baseline is one consumer per thread, which in Spring is just the concurrency setting. Each thread owns some partitions and processes them in order, so per-partition and therefore per-key order is kept and commits are trivially correct.

Then, if they want more

The limit is that parallelism stops at the partition count; extra threads sit idle. The second model is one consumer feeding a worker pool.

That gives as much parallelism as I like, but records finish out of order, so I can only commit up to the lowest unfinished offset per partition: if 100 is still running, I cannot commit past it even if 101 to 104 are done. And per-key order is lost, two events for the same account can run at once.

The third model is key-based: records with the same key go to the same worker queue, so per-key order is preserved while different keys run in parallel. Commits follow the same lowest-unfinished rule. In both pool models, the polling thread must keep polling within max.poll.interval.ms and pause partitions when queues fill.

Processing still needs to be idempotent, since work above a gap can rerun after a crash. In practice I would start with concurrency, and if partitions became the limit I would use a library like the Confluent Parallel Consumer rather than writing the bookkeeping myself. Share groups are coming too, but without per-key order.

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 "Compare three ways to process Kafka records in parallel in a Java service: one consumer...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/82556eaf01/