Quick revision
The short answer
KafkaConsumer is single-threaded by design: every call must come from the one thread that is using it, and when it detects a second thread it throws ConcurrentModificationException. The only method that is safe to call from another thread is wakeup(). The fix is to give the consumer one owning thread that runs the poll loop, and have the HTTP handler send commands such as seek or commit through a queue that the loop applies between polls; in Spring Kafka, the container already owns the thread and offers seek callbacks, Acknowledgment and pause/resume for this.
What to remember
In this answer, we saw that KafkaConsumer keeps all its state for one thread and throws ConcurrentModificationException when a second thread enters, with wakeup() as the single exception. The fix is structural: one thread owns the consumer and runs the loop, and other threads send seek, commit and pause commands through a queue that the loop applies between polls. To stop, another thread calls wakeup() and the owner closes the consumer. In Spring Kafka, the container already owns the thread, so we use seek callbacks, Acknowledgment and pause/resume instead.
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.
KafkaConsumer is single-threaded by design: every call must come from the one thread that is using it, and when it detects a second thread it throws ConcurrentModificationException. The only method that is safe to call from another thread is wakeup(). The fix is to give the consumer one owning thread that runs the poll loop, and have the HTTP handler send commands such as seek or commit through a queue that the loop applies between polls; in Spring Kafka, the container already owns the thread and offers seek callbacks, Acknowledgment and pause/resume for this.
Why this question is asked
Sharing a client object between threads feels natural, because many clients, such as HTTP clients and even KafkaProducer, are thread-safe. KafkaConsumer is the exception, and this error is how it tells us. Interviewers ask this because the fix shows whether we can restructure code around a single-owner object instead of sprinkling locks. In this answer, we will see why the consumer is single-threaded, reproduce the error, restructure the code with a command queue, stop the loop cleanly with wakeup(), and then do the same thing the Spring Kafka way.
1Why KafkaConsumer Is Single-Threaded
A consumer holds a lot of mutable state: its position in each partition, the fetch buffer, pending commits, its group membership and the current assignment. All of it changes inside poll(). Making every method safe for concurrent use would mean locking around all of that state, and it would still leave hard questions, such as what a seek means while another thread is halfway through processing the records of the previous poll.
So the consumer takes a simpler stance: one thread at a time. Each public method begins with a lightweight check that records which thread is inside the consumer. If a different thread enters while the first is still there, the call fails immediately with ConcurrentModificationException and the message 'KafkaConsumer is not safe for multi-threaded access'. The one exception is wakeup(), which is designed to be called from another thread to interrupt a blocking poll().
2Reproducing the Problem
The scheduler thread polls in a loop, and the HTTP handler, on a web server thread, calls methods on the same consumer object.
Notice that it fails 'under load'. With little traffic, the two threads rarely overlap, so the bug hides in testing. Under load, poll() spends more time inside the consumer, and the HTTP calls collide with it. Wrapping every call in synchronized would stop the exception, but the HTTP thread would then wait up to a second for poll() to return, and the seek would still race with records the loop is processing.
3Restructuring: One Owner Thread and a Command Queue
The clean structure gives the consumer to exactly one thread, the loop thread. Other threads never touch the consumer; they put commands on a queue. Between polls, the loop drains the queue and applies each command on its own thread, where it is always safe.
7 messages in order. Play them, or step through them one at a time.
Only the loop thread calls the consumer; other threads talk to the loop.
The HTTP handler now calls loop.seek(tp, 1200).get(5, SECONDS) and returns once the loop has applied it. The command runs between polls, so it never overlaps processing, and its effect is well defined: the next poll() returns records from offset 1200.
4Stopping the Loop From Outside
Shutdown follows the same rule. Another thread sets running to false and calls wakeup(). If the loop is blocked in poll(), that poll throws WakeupException at once, the loop sees running is false, and the finally block closes the consumer on its own thread. Closing commits offsets if auto-commit is on and tells the coordinator the member is leaving, so the group rebalances immediately instead of waiting for the session timeout.
Notice that close() is not called from the shutdown hook thread. Calling it there would be just another multi-threaded access and could throw the same exception.
5The Spring Kafka Way
In Spring Kafka, the listener container owns the consumer and its thread, so we should never reach for the consumer from elsewhere. The container offers the same capabilities through safe APIs. For seeks, a listener can implement ConsumerSeekAware or extend AbstractConsumerSeekAware; seek callbacks it keeps can be used from other threads, and the container queues those seeks and performs them on the consumer thread. For commits, a listener with AckMode.MANUAL or MANUAL_IMMEDIATE receives an Acknowledgment; the container performs the commit on the consumer thread. To stop consuming temporarily, we pause and resume the container.
Notice that none of these calls touches the KafkaConsumer from the HTTP thread. They all leave a request for the container, which applies it on the consumer thread, exactly like our hand-written command queue. Check your Spring Kafka version for the exact helper methods on AbstractConsumerSeekAware.
6Follow-up questions
Is KafkaProducer thread-safe?
Yes. A single producer instance is designed to be shared across threads, and sharing one is usually faster than having many, because records from different threads can share batches. Only the consumer has the single-thread rule.
Why not use one consumer per HTTP request instead?
Each consumer in a group triggers a rebalance when it joins and leaves, and creating one per request would make the group rebalance constantly. Even a standalone consumer with assign() pays for connections and metadata on every creation. Consumers are long-lived objects, owned by one thread for their whole life.
Can processing happen on other threads while one thread owns the consumer?
Yes, and that is a common design: the owner thread polls and hands records to workers. But the workers must report completion back to the owner, which then commits, and the owner must keep polling (pausing partitions when workers are busy) so it stays within max.poll.interval.ms.
7Conclusion
In this answer, we saw that KafkaConsumer keeps all its state for one thread and throws ConcurrentModificationException when a second thread enters, with wakeup() as the single exception. The fix is structural: one thread owns the consumer and runs the loop, and other threads send seek, commit and pause commands through a queue that the loop applies between polls. To stop, another thread calls wakeup() and the owner closes the consumer. In Spring Kafka, the container already owns the thread, so we use seek callbacks, Acknowledgment and pause/resume instead.
Open with
KafkaConsumer is deliberately single-threaded. It keeps positions, the fetch buffer, pending commits and group state that all change inside poll, and instead of locking all that, each method checks that only one thread is inside, and throws ConcurrentModificationException if a second one enters.
Then, if they want more
The only method meant to be called from another thread is wakeup. In this service the scheduler thread is in poll while the HTTP handler calls seek or commitSync, so under load they collide. Adding synchronized would hide the exception but make the HTTP thread wait for poll, and the seek would still race with processing.
I would restructure it so one thread owns the consumer and runs the loop. The HTTP handler puts a command on a queue, like seek this partition to this offset, then calls wakeup so a waiting poll returns.
Between polls the loop drains the queue and applies the commands on its own thread, and completes a future so the handler can reply. To stop, set a flag and call wakeup, and the owner thread closes the consumer in a finally block.
In Spring Kafka I would not touch the consumer at all: ConsumerSeekAware for seeks, which the container queues onto the consumer thread, Acknowledgment for commits, and pause and resume on the container through the endpoint registry.
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 "Under load the service throws ConcurrentModificationException: KafkaConsumer is not safe...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/e1c46e744e/