Video in progress

Strong Middle 📨 Kafka and messaging

How do you rewind a consumer to a specific offset or point in time: when do you need `seek()`, `offsetsForTimes()` and `--reset-offsets`?

In code I move a partition to any offset with seek(), and to rewind by time I first find the offset with offsetsForTimes(). To rewind a whole group I use kafka-consumer-groups.sh --reset-offsets, but that only works while the group is stopped.

📖 Answer

In Kafka messages aren't deleted; a consumer only stores a "where am I" number. So going back is just changing that number.

Rewinding by time with the plain client:

var tp = new TopicPartition("orders", 3);
long from = Instant.parse("2026-11-01T00:00:00Z").toEpochMilli();
OffsetAndTimestamp ot = consumer.offsetsForTimes(Map.of(tp, from)).get(tp);
if (ot != null) {
    consumer.seek(tp, ot.offset());   // the next poll() starts from here
}

offsetsForTimes() returns the offset of the first message at or after the given time; it looks at the .timeindex files. If there's no such message, it returns null.

In Spring Kafka, if the listener extends AbstractConsumerSeekAware, methods like seekToTimestamp(...) and seekToBeginning() are ready to use. For example, it's easy to expose an admin endpoint that says "re-read this partition".

The operational way is the CLI:

kafka-consumer-groups.sh --bootstrap-server kafka:9092 --group billing \
  --topic orders --reset-offsets --to-datetime 2026-11-01T00:00:00.000 --dry-run
# if the result looks right, replace --dry-run with --execute

People trip up here a lot: if the group has an active member, the command is rejected. You have to stop the service first (or scale the replicas to 0). I never skip --dry-run: one wrong date and you reprocess two weeks of messages.