Quick revision
The short answer
Run the replay in its own consumer group, with a listener that extends AbstractConsumerSeekAware: in onPartitionsAssigned call callback.seekToTimestamp(assignments.keySet(), startMillis), which moves each partition to the first record at or after 09:00. Capture each partition's end offset for 11:30 with offsetsForTimes (or Admin.listOffsets), skip and pause partitions once they pass it, and stop the container through KafkaListenerEndpointRegistry when every partition is done. It is safe alongside the live group because the live group's offsets are untouched, the replay is idempotent with side effects suppressed, and the listener has autoStartup = "false" so it only runs on demand.
What to remember
In this answer, we built a time-window replay in Spring Kafka. A listener extending AbstractConsumerSeekAware records each partition's end offset for 11:30 and calls seekToTimestamp for 09:00 on assignment, skips and pauses partitions as they pass their end, and stops the container through KafkaListenerEndpointRegistry from another thread once all are done. It runs in its own group so the live group is untouched, starts only on demand with autoStartup = "false", and processes idempotently with side effects suppressed in replay mode.
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.
Run the replay in its own consumer group, with a listener that extends AbstractConsumerSeekAware: in onPartitionsAssigned call callback.seekToTimestamp(assignments.keySet(), startMillis), which moves each partition to the first record at or after 09:00. Capture each partition's end offset for 11:30 with offsetsForTimes (or Admin.listOffsets), skip and pause partitions once they pass it, and stop the container through KafkaListenerEndpointRegistry when every partition is done. It is safe alongside the live group because the live group's offsets are untouched, the replay is idempotent with side effects suppressed, and the listener has autoStartup = "false" so it only runs on demand.
Why this question is asked
Replaying a window of data after a bug fix is a routine operation that goes wrong in surprising ways: the replay runs past its end, re-sends emails to customers, or moves the live group's offsets. Interviewers ask for this code to see whether we know Spring Kafka's seek support and, more importantly, the guard rails around it. In this answer, we will recap how a consumer can start at a time, write the listener, add the stop condition, start it on demand, and then look at what makes it safe beside the live group.
1A Quick Recap: Seeking by Time
Every Kafka record has a timestamp, by default the time the producer created it. Each partition keeps a time index, so the broker can answer 'what is the first offset whose timestamp is at or after T?'. The consumer API exposes this as offsetsForTimes(), and the admin API as listOffsets() with OffsetSpec.forTimestamp(). If no record is at or after T, the answer is empty (or -1), meaning 'nothing yet'.
Spring Kafka wraps seeking in ConsumerSeekAware. A listener bean that implements it, or extends the convenience class AbstractConsumerSeekAware, gets called when partitions are assigned and receives a ConsumerSeekCallback whose seekToTimestamp() performs the lookup and the seek on the consumer thread, before any records are delivered.
2The Listener
We give the replay its own group id, a listener id we can find in the registry, and autoStartup = "false". On assignment we record each partition's end offset for 11:30 and seek to 09:00.
Notice that the end is an offset, not a timestamp check on each record. Record timestamps are set by producers and are not strictly increasing within a partition, so one late-stamped record could stop a timestamp check too early. Capturing the first offset at or after 11:30 once, per partition, gives an exact boundary. Offsets.firstAtOrAfter is a small helper of ours that calls admin.listOffsets() with OffsetSpec.forTimestamp(END) and falls back to the partition's latest offset when no record is that recent.
3Stopping When Every Partition Has Passed the End
When a partition passes its end offset, we pause it so no more records arrive for it, and once all assigned partitions are done we stop the container. A partition with no records in the window at all would never call onRecord, so we also mark it done at assignment time if its start offset is already at or beyond its end; that check is left out above for brevity.
- Operator starts container orders-replay
autoStartupis false, so only on demand - On assignment: record end offsets, seek to 09:00
seekToTimestampper partition - Is the record's offset before the partition's end?YesApply in replay modeNoPause the partition
- Have all partitions passed their end?YesStop the container from another threadNoKeep consuming
4 steps in order. Play them, or step through them one at a time.
Notice that stop() is called on a separate thread. Stopping a container waits for its consumer thread to finish, and the listener runs on that very thread, so calling stop() directly from the listener would be waiting for itself.
4Starting It on Demand
Because autoStartup is false, the container is registered when the application starts but does nothing until someone starts it. A small admin endpoint, protected like any other operational action, starts it.
This also protects against accidents: a restart of the service does not quietly start another replay, and a forgotten listener does not keep consuming forever.
5Why It Is Safe Beside the Live Group
Three properties make it safe. First, the separate group id: the replay commits offsets only for orders-replay-2026-11-11, so the live group's committed offsets and assignment are untouched, and the live consumers never notice. Second, idempotence: the records in the window were already processed once with the bug, so processing must overwrite rather than add, for example an upsert keyed by order id with a version check, so applying an event twice leaves the same state. Third, side effects: anything that leaves the system, such as emails, payments or webhooks, must be suppressed in replay mode, which is why the listener passes Mode.REPLAY and the projection skips those calls.
Two smaller precautions help. The replay may compete with the live group for the same database, so we can throttle it, for example with a small max.poll.records, or run it outside peak hours. And when the replay is finished, we can delete its group with kafka-consumer-groups.sh --delete so it does not linger in dashboards.
6Follow-up questions
Why not reset the live group's offsets to 09:00 instead?
That would make the live consumers reprocess everything from 09:00 to now, not just the window, would need the live group stopped while resetting, and would mix replayed and current processing in one place. A separate group replays exactly the window while live processing continues normally.
What if the replay instance has several consumers and a rebalance happens?
onPartitionsAssigned runs again for the new assignment and would seek to 09:00 again, replaying from the start for those partitions. Idempotent processing makes that harmless, but for long replays we can seek to the committed offset when one exists, and only use the timestamp for partitions that have none.
7Conclusion
In this answer, we built a time-window replay in Spring Kafka. A listener extending AbstractConsumerSeekAware records each partition's end offset for 11:30 and calls seekToTimestamp for 09:00 on assignment, skips and pauses partitions as they pass their end, and stops the container through KafkaListenerEndpointRegistry from another thread once all are done. It runs in its own group so the live group is untouched, starts only on demand with autoStartup = "false", and processes idempotently with side effects suppressed in replay mode.
Open with
I would write a separate listener in its own consumer group, so the live group's offsets are never touched, and give it autoStartup false so it only runs when someone starts it from an admin endpoint through the KafkaListenerEndpointRegistry.
Then, if they want more
The listener extends AbstractConsumerSeekAware. In onPartitionsAssigned I first look up, for every assigned partition, the first offset at or after 11:30 using offsetsForTimes or Admin.listOffsets with a timestamp spec, falling back to the latest offset if nothing is that recent, and store it as the end.
Then I call callback.seekToTimestamp with the assigned partitions and 09:00, which seeks each partition to its first record at or after that time. In the listener, if a record's offset is at or past the end for its partition, I skip it and pause that partition; otherwise I process it in replay mode.
I use offsets for the end rather than comparing timestamps on each record, because producer timestamps are not strictly ordered. When every partition has reached its end, I stop the container, from another thread, because stop waits for the consumer thread.
To make it safe beside the live group, the processing must be idempotent, an upsert keyed by order id, and replay mode must suppress side effects like emails, since these records were already processed once.
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 "Show how you would seek to the start time on assignment and stop at the end time, and...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/33129d9320/