Scrivio Interview library
CodingIntermediate7 min read · updated 13 November 2026

Each record on the topic "video-jobs" starts a transcoding job that takes 2 to 20 minutes.

Outline a plain Java consumer loop that processes one job at a time without being removed from the group, and explain how it commits offsets.

Run each job on a worker thread and pause() the consumer's partitions while it runs, but keep calling poll(Duration.ofMillis(100)) on the consumer thread: polls on paused partitions return nothing, yet they keep the consumer inside max.poll.interval.ms. When the job finishes, commit that record's offset plus one with commitSync(Map.of(tp, new OffsetAndMetadata(offset + 1))) and resume() the partitions. Use max.poll.records=1, a ConsumerRebalanceListener that never commits for partitions the consumer no longer owns, and idempotent jobs, because a crash between finishing and committing reruns the job.

Why this question is asked

A 20-minute job breaks the usual consumer loop, because the consumer must call poll() at least every max.poll.interval.ms, 5 minutes by default, or it is removed from the group. The tempting fix, raising the interval to 25 minutes, works but makes a genuinely stuck consumer invisible for 25 minutes. Interviewers ask this coding question to see whether we know the pause-and-keep-polling pattern and the commit and rebalance details that make it safe. In this answer, we will recap the constraint, write the loop, look at how it commits, handle rebalances, explain why jobs must be idempotent, and then see how Spring Kafka changes things.

1A Quick Recap of the Constraint

A consumer in a group must call poll() regularly. If more than max.poll.interval.ms passes between two calls, the client concludes that our code is stuck, leaves the group, and its partitions are given to another member. Heartbeats run on a background thread, so the session stays alive meanwhile; the poll interval is the problem.

pause() is the tool that solves it. Pausing a partition tells the consumer to stop returning records for it from poll(), without leaving the group or losing the assignment. So a paused consumer can call poll() every 100 ms, get nothing back, and prove to the group that it is alive and making progress, while the real work happens on another thread.

2The Consumer Loop

Here is the outline. The consumer thread owns the consumer, as it must; a single worker thread runs the job.

Java
props.put("max.poll.records", 1); // never hold jobs we cannot start
props.put("enable.auto.commit", false);
consumer.subscribe(List.of("video-jobs"), rebalanceListener);
// fields of the JobConsumer class, so the rebalance listener below can see them
private final ExecutorService worker = Executors.newSingleThreadExecutor();
private Future<?> running; // the job in progress, or null
private ConsumerRecord<String, Job> current; // its record
// in run(), on the consumer thread:
while (!stopping) {
ConsumerRecords<String, Job> recs = consumer.poll(Duration.ofMillis(100)); // empty while paused
if (running == null && !recs.isEmpty()) {
current = recs.iterator().next(); // max.poll.records=1: one job
ConsumerRecord<String, Job> job = current;
running = worker.submit(() -> transcode(job.value())); // 2 to 20 minutes
consumer.pause(consumer.assignment()); // stop delivering, stay in the group
}
if (running != null && running.isDone()) {
TopicPartition tp = new TopicPartition(current.topic(), current.partition());
try {
running.get(); // rethrows a job failure
consumer.commitSync(Map.of(tp, new OffsetAndMetadata(current.offset() + 1)));
} catch (ExecutionException e) {
handleFailedJob(current, e.getCause()); // dead-letter or retry policy
consumer.commitSync(Map.of(tp, new OffsetAndMetadata(current.offset() + 1)));
}
running = null;
current = null;
consumer.resume(consumer.paused()); // next poll may return the next job
}
}
Step throughOne job through the loop
  1. poll() returns one recordmax.poll.records=1
  2. Submit job to worker, pause partitions
  3. poll(100 ms) on paused partitionsreturns nothing, keeps the member alive
  4. Is the job done?
    YesCommit offset + 1NoPoll again in 100 ms
  5. resume() the partitionsthe next poll can return the next job

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

The consumer thread never blocks on the job; it polls every 100 ms for the whole 20 minutes.

Notice that all consumer calls stay on the loop thread; the worker only runs transcode(), and the commit follows within 100 ms of the job finishing.

3How the Offsets Are Committed

A committed offset means 'the next record to read', so after finishing the job at offset 41 we commit 42. We pass it explicitly for that one partition with commitSync(Map.of(tp, new OffsetAndMetadata(offset + 1))) rather than calling the no-argument commitSync(). The reason is that the no-argument form commits the consumer's current position for every assigned partition, and the position has already moved past records that were fetched but not processed.

max.poll.records=1 matters for the same reason. With the default of 500, the first poll could return several jobs; we would start one and have to remember the rest, and a crash would lose track of which ones had started. With one record per poll, the consumer's notion of 'returned' and our notion of 'started' stay the same. The fetch buffer may still hold more records internally, but poll() will not return them while the partitions are paused.

4Handling a Rebalance in the Middle of a Job

A rebalance can happen at any time, for example when another instance starts. The consumer calls our ConsumerRebalanceListener inside poll(), on the loop thread. In onPartitionsRevoked we must decide what to do with the running job if its partition is being taken away. We cannot wait 20 minutes there, because the rebalance waits for us. The usual choice is to cancel the job, or let it run but forget about it, and never commit for that partition, since we no longer own it; the new owner will start from the last committed offset and run the job again.

There is a second detail. Pausing is lost for partitions that are revoked and reassigned, and newly assigned partitions start unpaused. So in onPartitionsAssigned, if a job is still running, we pause the new partitions too.

Java
ConsumerRebalanceListener rebalanceListener = new ConsumerRebalanceListener() {
public void onPartitionsRevoked(Collection<TopicPartition> parts) {
if (current != null && parts.contains(new TopicPartition(current.topic(), current.partition()))) {
running.cancel(true); // the new owner reruns it; we do not commit
running = null; current = null;
}
}
public void onPartitionsAssigned(Collection<TopicPartition> parts) {
if (running != null) consumer.pause(parts); // still busy: do not take a second job
}
};

5Why the Job Must Be Idempotent

There is always a moment between 'transcode finished' and 'commit succeeded'. If the process crashes there, or a rebalance moves the partition, the job runs again. For transcoding, that means writing output to a path derived from the job id, overwriting rather than appending; checkpoints make a rerun cheaper.

6How This Changes With Spring Kafka

Spring Kafka's listener container runs the poll loop for us, and the listener method is called on the consumer thread. If we simply block in the listener for 20 minutes, the container cannot poll, so the simplest Spring approach is to set max.poll.records=1, AckMode.RECORD and a max.poll.interval.ms above the longest job, say 25 minutes, accepting slower detection of stuck consumers.

To keep the short poll interval, we can mirror the plain Java design: hand the job to an executor, pause the container (or the partition) through KafkaListenerEndpointRegistry, and acknowledge through the Acknowledgment when the job completes. The container keeps polling while paused, and it performs the commit on its own thread. Check your Spring Kafka version for how acknowledgements from other threads are handled.

7Follow-up questions

How would this change with Spring Kafka's listener container?

The container owns the loop, so we either raise max.poll.interval.ms above the longest job with max.poll.records=1 and let the listener block, or keep the interval short by handing the job to an executor, pausing the container or partition through the endpoint registry, and acknowledging with manual ack mode when the job finishes. The container keeps polling while paused and commits on the consumer thread, so the same rules hold without us writing the loop.

Why not simply raise max.poll.interval.ms to 30 minutes?

It is valid and simpler, but a consumer stuck on a deadlocked job would hold its partitions for 30 minutes before anyone notices, and a rebalance would wait for it for up to that long. The pause pattern keeps detection fast while allowing jobs of any length.

8Conclusion

In this answer, we wrote a consumer loop that runs a 2 to 20 minute job on a worker thread while the consumer thread keeps polling every 100 ms with its partitions paused, so it never exceeds max.poll.interval.ms. It commits exactly the finished record's offset plus one for its partition, then resumes. max.poll.records=1 keeps returned and started jobs the same, a rebalance listener cancels jobs for revoked partitions and re-pauses new ones, and idempotent jobs make reruns harmless. In Spring Kafka, we either raise the poll interval or use container pause with manual acknowledgement.

What to say in the interviewAbout 90 seconds aloud

Open with

The constraint is that the consumer must call poll within max.poll.interval.ms, five minutes by default, and a job can take twenty. So I would run the job on a worker thread and keep the consumer thread polling.

Then, if they want more

With max.poll.records set to one, a poll returns one job; I submit it to a single-thread executor and pause all assigned partitions. Then the loop calls poll with 100 milliseconds every time round. Paused partitions return no records, but the consumer stays in the group because it keeps polling. Each loop checks whether the job's future is done.

When it is, I call get to surface a failure, commit exactly that partition with commitSync of a map from the topic partition to offset plus one, and resume the partitions so the next poll can return the next job. I commit explicitly rather than with the no-argument commit, because the consumer's position can be ahead of what I processed.

I add a rebalance listener: on revocation of the job's partition I cancel the job and do not commit, since the new owner will rerun it, and on assignment I pause new partitions if a job is still running. Finally the job must be idempotent or resumable, because a crash between finishing and committing runs it again.

In Spring Kafka I would either raise the poll interval or pause the container and acknowledge manually.

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 "Outline a plain Java consumer loop that processes one job at a time without being...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/8f19c6e491/