Spring Kafka
Kafka Offset
Consumer Group ID
Technology
Apache Kafka

Spring Kafka - How to reset offset to latest with a group id?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Resetting a Kafka consumer group to the latest offset means telling that group to ignore old records and continue only from the current end of each partition. In Spring Kafka, the exact approach depends on whether the group has committed offsets already and whether you are doing a one-time reset or trying to influence startup behavior for a brand-new group.

Understand auto.offset.reset First

A common source of confusion is the auto.offset.reset=latest setting. It does not force an existing group to jump to the end. It is only used when Kafka cannot find a committed offset for that consumer group and partition.

In other words:

  • new group with no committed offsets: latest starts from the end
  • existing group with committed offsets: Kafka resumes from those committed positions

That means changing the property alone will not reset an already-used group.

java
props.put(ConsumerConfig.GROUP_ID_CONFIG, "orders-group");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");

This configuration is correct for a fresh group, but not for forcibly skipping backlog on an existing one.

One-Time Reset by Seeking to End on Assignment

If you control the consumer lifecycle, you can seek assigned partitions to the end when they are assigned. This is useful when you intentionally want to discard backlog for a known group in a controlled environment.

java
1import org.apache.kafka.clients.consumer.Consumer;
2import org.apache.kafka.common.TopicPartition;
3import org.springframework.kafka.listener.ConsumerAwareRebalanceListener;
4
5import java.util.Collection;
6
7public class SeekToLatestRebalanceListener implements ConsumerAwareRebalanceListener {
8    @Override
9    public void onPartitionsAssigned(Consumer<?, ?> consumer, Collection<TopicPartition> partitions) {
10        consumer.seekToEnd(partitions);
11    }
12}

In Spring Kafka, attach that listener through the container properties so the seek happens during partition assignment.

java
factory.getContainerProperties()
    .setConsumerRebalanceListener(new SeekToLatestRebalanceListener());

This approach is operationally simple, but it should be used carefully because it discards unread data for that group.

Reset Offsets Outside the Application

For many teams, the safest method is to reset offsets as an operational action while the consumer is stopped. Kafka ships a consumer-group tool for exactly this kind of administrative reset.

bash
1kafka-consumer-groups.sh \
2  --bootstrap-server localhost:9092 \
3  --group orders-group \
4  --topic orders \
5  --reset-offsets \
6  --to-latest \
7  --execute

This is often preferable in production because the reset is explicit, reviewable, and separate from application startup logic.

Spring Kafka Listener Design Considerations

If you are using @KafkaListener, remember that the container may start quickly and begin polling before any custom recovery logic you imagined elsewhere. If offset resets are a normal operational activity, keep them out of the listener code path and run them as a separate administrative step.

If you need application-driven seeking, use lifecycle hooks or rebalance listeners instead of trying to fake the effect with auto.offset.reset.

Verify the Group State Before and After

Before resetting, inspect the group's lag and current positions. After resetting, verify that the committed offsets moved where you expected.

bash
1kafka-consumer-groups.sh \
2  --bootstrap-server localhost:9092 \
3  --group orders-group \
4  --describe

That check is important because "latest" means the current log end offset at the time of reset. New messages published afterward will still be consumed normally.

Common Pitfalls

  • Assuming auto.offset.reset=latest will reset offsets for a group that already has committed positions.
  • Seeking to end automatically on every rebalance and unintentionally skipping valid unread data.
  • Resetting offsets while consumers are still actively running against the same group.
  • Mixing operational reset procedures into normal application startup without clear intent.
  • Forgetting to verify the group state before and after the reset.

Summary

  • 'auto.offset.reset=latest only affects groups without committed offsets.'
  • Existing groups must be reset explicitly if you want to skip old records.
  • In Spring Kafka, use rebalance hooks for controlled in-app seeking to end.
  • In production, the Kafka consumer-group reset command is often the safest approach.
  • Always verify consumer group offsets before and after the change.

Course illustration
Course illustration

All Rights Reserved.