BROKABROKA
Sign inDownload CommunityPricing
Learn
Kafka fundamentalsLesson 6 of 6
TutorialBeginner

Consumer lag and resetting offsets safely

Run a producer that writes bike rides faster than one slow consumer can bill them, on a single-node Kafka 4.3 cluster, with the consumer in Java or in Python: watch the group's lag grow, pile up behind a crashed member and fall back to zero; then move the group's committed offsets safely, with a dry run first, and delete them.

Before you start
  • The first five lessons. A single-node Kafka cluster with KRaft, Topics, partitions and the log underneath, Producing: keys, partitions and ordering, Consuming: offsets, groups and where to start and Consumer groups and partition assignment explain the words this lesson builds on: broker, listener, topic, partition, offset, key, consumer group, committed offset and group member.
  • Docker with Compose v2.20 or later. The lesson was tested with Docker 29 and Compose v5.
  • A bash-style shell. The commands use bash; on Windows, run them in WSL.
  • Java or Python. JDK 21 with Maven 3.9 or later, or Python 3.12 to 3.14 with pip; one of the two is enough. The consumer and its commands are on the page in both languages: pick yours on any code tab, and the page keeps the choice. The first run downloads the Kafka client, so it needs an internet connection.
  • On Ubuntu, also under WSL. Ubuntu 24.04 ships Maven 3.8, which fails on this project with "Source option 5 is no longer supported"; install Maven 3.9 from maven.apache.org instead. For Python, install the python3-venv package first.
  • If you have Python 3.15. Check with python3 --version. The lab's Kafka client for Python, confluent-kafka 2.16.1, has no prebuilt package for Python 3.15, so pip tries to build it from source and usually stops with Failed building wheel for confluent-kafka. Install Python 3.14 as well, for example with brew install python@3.14 on a Mac. Then, in Falling behind, below, create the virtual environment with python3.14 -m venv --clear .venv instead of python3 -m venv .venv; --clear empties a .venv that an earlier attempt left behind.
  • Four terminals. Up to three members of the group run at a time, each in a terminal of its own, and a fourth terminal runs the commands. Tabs in one terminal window are fine.
  • TCP ports 19235 and 19135 free on your machine. The labs of the earlier lessons use 19130, 19131, 19232, 19132, 19233, 19133, 19234 and 19134, so all six can run side by side. You can change the ports; the lesson tells you where.
  • Room for a broker and a producer. The broker uses about 450 MB of memory, and the producer, a second container, about 110 MB. Both run from the image of the earlier lessons.

What you are about to run

The lab is one Docker Compose project with two services, both started from apache/kafka:4.3.2, the image of the earlier lessons. kafka is a single node that is broker and controller at once. producer writes two bike rides a second to the topic rides.completed, each keyed by a random bike, until you stop it. You create the topic, with three partitions, and run up to three copies of a small consumer on your machine, in Java or in Python, as members of one consumer group, ride-billing. Each copy bills one ride a second, so a copy on its own falls behind.

The producer is in compose.yml next to the broker:

compose.yml
  # Writes RATE rides a second to rides.completed until you stop it. It is in a profile of its own, so
  # docker compose up leaves it out: it starts when you name it, after you have created the topic.
  producer:
    image: apache/kafka:4.3.2
    profiles: ["rides"]
    depends_on:
      kafka:
        condition: service_healthy
    environment:
      RATE: ${RATE:-2}
      KAFKA_HEAP_OPTS: -Xmx128m
    volumes:
      - ./scripts:/scripts:ro
    entrypoint: ["bash", "/scripts/load.sh"]

It runs the image's console producer from a short script, so it needs no code of its own. profiles: ["rides"] puts it in a profile: docker compose up starts only the broker, and the producer starts when you name it. condition: service_healthy makes it wait until the broker is healthy. RATE, two by default, is the number of rides a second, and the script reads it:

scripts/load.sh
#!/usr/bin/env bash
# Produces RATE finished rides per second to rides.completed until stopped.
set -euo pipefail

BOOT="${BOOTSTRAP:-kafka:29092}"
RATE="${RATE:-2}"

generate() {
  while true; do
    for ((i = 0; i < RATE; i++)); do
      bike="bike-$((RANDOM % 1000 + 1000))"
      printf '%s|{"bike":"%s","from":"station-%02d","to":"station-%02d","minutes":%d}\n' \
        "$bike" "$bike" $((RANDOM % 12 + 1)) $((RANDOM % 12 + 1)) $((RANDOM % 55 + 4))
    done
    sleep 1
  done
}

generate | /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server "$BOOT" --topic rides.completed \
  --reader-property parse.key=true --reader-property key.separator='|' --command-property linger.ms=5

Every second, generate prints RATE lines in the shape of the rides of the earlier lessons: a key, a | and a value, where the key is a random bike from bike-1000 to bike-1999 and the value is the ride. The console producer splits each line at the |, as parse.key=true and key.separator='|' did in the earlier lessons. linger.ms=5 makes it send each ride within five milliseconds instead of holding rides back to fill a batch.

The broker has the three listeners of the last three labs, on ports of its own:

compose.yml
      KAFKA_LISTENERS: INTERNAL://:29092,HOST://:19235,EXTERNAL://:19135,CONTROLLER://:9093
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:29092,HOST://localhost:${KAFKA_HOST_PORT:-19235},EXTERNAL://${KAFKA_ADVERTISED_HOST:-host.docker.internal}:${KAFKA_PORT:-19135}
Listener Port Advertised address For
INTERNAL 29092, inside the project only kafka:29092 Kafka's tools, run with docker compose exec, and the producer
HOST 19235 localhost:19235 your consumers, and other programs on your machine
EXTERNAL 19135 host.docker.internal:19135 BROKA and other clients in containers

CONTROLLER, on port 9093, is the quorum's, as before. As the third lesson explained, programs on your machine need HOST: EXTERNAL advertises host.docker.internal, a name your machine may not know.

Get the lab

terminal
git clone https://github.com/orchesta/broka.git
cd broka/learn/kafka/06-consumer-lag
ls -A

If you kept the clone from an earlier lesson, run git pull in it instead of cloning again, then change into the new folder.

output
.env.example
README.md
compose.yml
java
python
scripts

compose.yml is the project, and scripts holds load.sh, the producer's script. java and python hold the same consumer, one folder per language: java is a small Maven project whose one real dependency is the Kafka client, kafka-clients 4.3.2; python has the script and a requirements.txt that names confluent-kafka 2.16.1. .env.example holds the four settings you may want to change, and README.md has the run commands and the connection values.

The settings file is optional. Copy it when you need to change something:

terminal
cp .env.example .env
.env.example
# Host port of the HOST listener, for the programs you run on this machine. If you change it, change
# localhost:19235 in the Java and Python code too.
KAFKA_HOST_PORT=19235

# Host port of the EXTERNAL listener, for clients in other containers, such as BROKA.
KAFKA_PORT=19135

# Address the broker hands back to clients on the EXTERNAL listener. Keep host.docker.internal on Docker
# Desktop; on Linux set it to the host's IP address.
KAFKA_ADVERTISED_HOST=host.docker.internal

# Rides the producer writes each second.
RATE=2
  • KAFKA_HOST_PORT: change it when 19235 is taken. The consumer has localhost:19235 in its code, so change the port there too.
  • KAFKA_PORT: change it when 19135 is taken.
  • KAFKA_ADVERTISED_HOST: the address the broker hands to clients in other containers. On Linux with Docker Engine, put the IP address of your machine here; the first lesson explains why.
  • RATE: the rides the producer writes each second. The lesson uses the default, 2, until Try it yourself.

Start the broker and the producer

terminal
docker compose up -d --wait

--wait returns when the broker is healthy. The last lines look like this:

output
 Network broka-lab-kafka-consumer-lag_default  Created
 Volume broka-lab-kafka-consumer-lag_kafka-data  Created
 Container broka-lab-kafka-consumer-lag-kafka-1  Healthy

up leaves the producer out: it is in a profile, rides, that you have not named. As in the earlier labs, the broker does not create a topic when a client first uses it: KAFKA_AUTO_CREATE_TOPICS_ENABLE is false. Create the topic, with three partitions:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:29092 --create --topic rides.completed --partitions 3 --replication-factor 1
output
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic rides.completed.

The warning is the one from the second lesson: every topic name with a dot draws it.

Now start the producer, by name:

terminal
docker compose up -d producer
output
 Container broka-lab-kafka-consumer-lag-kafka-1  Healthy
 Container broka-lab-kafka-consumer-lag-producer-1  Started

Naming a service in a profile starts it, even without --profile; Compose checks that the broker is healthy first. The producer prints nothing; it writes. Read the end of each partition's log, with the tool of the fourth lesson:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-get-offsets.sh --bootstrap-server localhost:29092 --topic rides.completed
output
rides.completed:0:0
rides.completed:1:6
rides.completed:2:4

As in the fourth lesson, each line is a topic, a partition and its log end offset: the offset the next record written there will get. Run the same command again a few seconds later:

output
rides.completed:0:5
rides.completed:1:11
rides.completed:2:8

The ends move: fourteen rides in seven seconds here, two a second. The keys are random, so your rides spread over the partitions differently, and your numbers differ.

A consumer that bills slowly

The consumer is the same program in both languages. Pick yours:

java/src/main/java/learn/BillingConsumer.javapython/billing_consumer.py
package learn;

import java.time.Duration;
import java.util.List;
import java.util.Properties;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.errors.WakeupException;
import org.apache.kafka.common.serialization.StringDeserializer;

public class BillingConsumer {
    public static void main(String[] args) throws InterruptedException {
        // The member's name, such as billing-1. Every line the program prints starts with it.
        String name = args[0];
        // With a number, the program stops abruptly after billing that many rides, as a crash would.
        int crashAfter = args.length > 1 ? Integer.parseInt(args[1]) : 0;

        var props = new Properties();
        props.put("bootstrap.servers", "localhost:19235");
        props.put("group.id", "ride-billing");
        // The name is also the client id, which the group tools show for each member.
        props.put("client.id", name);
        props.put("group.protocol", "consumer");
        props.put("key.deserializer", StringDeserializer.class.getName());
        props.put("value.deserializer", StringDeserializer.class.getName());
        props.put("auto.offset.reset", "earliest");
        // One ride per poll(), as in Python. Billing is slow, and the client leaves the group when more than
        // five minutes pass between two calls of poll(); 500 rides, the default, would take eight minutes.
        props.put("max.poll.records", "1");

        var consumer = new KafkaConsumer<String, String>(props);
        var main = Thread.currentThread();
        // Ctrl+C runs this hook: it wakes poll() up and waits until the loop below has closed the consumer.
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            consumer.wakeup();
            try {
                main.join();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }));

        try {
            consumer.subscribe(List.of("rides.completed"));
            int billed = 0;
            while (true) {
                for (var record : consumer.poll(Duration.ofSeconds(1))) {
                    System.out.println(name + ": " + record.key() + ", partition " + record.partition()
                            + ", offset " + record.offset());
                    // Billing a ride takes a second. The producer writes two a second.
                    Thread.sleep(1000);
                    if (++billed == crashAfter) {
                        System.out.println(name + ": crash");
                        Runtime.getRuntime().halt(1); // Skips close() and the commit it makes.
                    }
                }
            }
        } catch (WakeupException e) {
            // Ctrl+C: leave the loop.
        } finally {
            // Commits the position after the last ride poll() returned, then leaves the group.
            consumer.close();
        }
    }
}

Most of it is the consumer of the fifth lesson, without the rebalance listener: it connects to localhost:19235, joins the group ride-billing with the newer group protocol, takes its name as its first argument and uses it as the client id, starts a new group at the first record with auto.offset.reset=earliest, and commits by itself every five seconds. When you press Ctrl+C, it closes, which commits once more and leaves the group. Three things are new:

  • Billing takes a second. After printing a ride, the program sleeps for a second, as if billing it took that long. The producer writes two rides a second, so one copy falls behind by about a ride every second.
  • One ride per poll(), in Java. max.poll.records=1 makes each poll() return at most one ride; the Python client's poll() returns one message anyway. The client leaves the group when more than five minutes, max.poll.interval.ms, pass between two calls of poll(). The default batch, up to 500 rides, would take more than eight minutes to bill.
  • A crash on request. An optional second argument is a number of rides: after billing that many, the program ends at once, with Runtime.getRuntime().halt(1) in Java and os._exit(1) in Python. Neither runs close(), so the member makes no final commit and does not leave the group, as when its machine fails.

Falling behind

Each member keeps running until you stop it, so each needs a terminal of its own. Keep the terminal you have used so far for the docker compose commands; this lesson calls it the command terminal. Open a second terminal, change into the lab's folder in it, learn/kafka/06-consumer-lag in your clone, and start the first member, billing-1:

cd java
mvn -q compile exec:java -Dexec.args=billing-1

The first Maven run downloads the Kafka client and Maven's plugins; -q keeps Maven quiet, so only the program's lines appear. pip installs one package, confluent-kafka, and prints its own lines first (if it stops with Failed building wheel for confluent-kafka, see the Python 3.15 item under Before you start). Then the member prints a line for each ride it bills, one a second:

billing-1: bike-1319, partition 0, offset 0
billing-1: bike-1401, partition 0, offset 1
billing-1: bike-1925, partition 0, offset 2
billing-1: bike-1077, partition 0, offset 3
billing-1: bike-1368, partition 0, offset 4
billing-1: bike-1234, partition 0, offset 5
billing-1: bike-1688, partition 0, offset 6
billing-1: bike-1533, partition 1, offset 0

The group is new, so earliest starts each partition at offset 0. The member bills a run of rides from one partition before it moves to another: the client fetches rides in batches, one per partition, and hands them over one batch after another. Which partition comes first can differ.

After about half a minute, describe the group in the command terminal:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --describe --group ride-billing
GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID            HOST            CLIENT-ID
ride-billing    rides.completed 0          7               27              20              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 1          14              28              14              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 2          7               31              24              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
  • CURRENT-OFFSET is the offset the group last committed on the partition: the next ride the group will bill there.
  • LOG-END-OFFSET is the end of the partition's log: the offset the next ride written there will get.
  • LAG is the difference: the rides on the partition that the group has not committed yet. As the fourth lesson showed, the member commits every five seconds, so the lag is only as fresh as the last commit: the member may have billed a few of those rides already.

The last three columns name the member that holds the partition: CLIENT-ID is the name you gave it. CONSUMER-ID is random, so yours differs; HOST is the address the broker saw the member come from, so it may differ too.

Twenty seconds later, describe the group again; the Up arrow brings the command back:

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID            HOST            CLIENT-ID
ride-billing    rides.completed 0          23              43              20              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 1          14              39              25              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 2          9               46              37              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1

Add the lag up. In the Java run, it was 58 rides at the first describe and 82 at the second, 21 seconds later; in the Python run, 56 and then 78, 22 seconds later. The producer writes two rides a second and the member bills one, so the total lag grows by about one ride a second, and keeps growing as long as the member is alone. Across the partitions, the lag is uneven, because the member bills one partition's batch at a time. In the Java run, between the two describes, the committed offset on partition 0 went from 7 to 23 and its lag stayed at 20, while on partition 1 the committed offset stayed at 14 and the lag grew from 14 to 25.

Keep billing-1 running.

A member that dies

A member that crashes does not leave the group: it stops sending heartbeats, and the broker waits for its session to time out before it gives the member's partitions to the other members. Watch what that does to the lag. First, note the time in the command terminal; Resetting offsets, below, uses it:

terminal
date -u +%FT%T.000
2026-11-18T12:48:10.000

-u prints the time in UTC, and +%FT%T.000 prints it in the format the reset tool takes, with the milliseconds set to .000. Yours is the time you run it.

Open a third terminal, change into the lab's folder, and start a second member, billing-2, with 5 as a second argument:

cd java
mvn -q compile exec:java -Dexec.args="billing-2 5"

In Python, the virtual environment is there already; the new terminal only has to activate it. The group gives the new member one of the three partitions, partition 2 in the runs for this lesson; yours may get another. It bills five rides from that partition, prints crash and ends:

billing-2: bike-1412, partition 2, offset 9
billing-2: bike-1288, partition 2, offset 10
billing-2: bike-1645, partition 2, offset 11
billing-2: bike-1781, partition 2, offset 12
billing-2: bike-1917, partition 2, offset 13
billing-2: crash

In the command terminal, describe the group again at once:

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID            HOST            CLIENT-ID
ride-billing    rides.completed 0          28              59              31              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 1          25              48              23              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 2          9               57              48              dll1h3S8Tk-DNbaW3kuNww /192.168.65.1   billing-2

billing-2 still holds partition 2: the broker has not noticed the crash yet. Nobody bills that partition now, so its lag grows with every ride the producer writes there. Describe the group again a few times. About fifteen seconds later:

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID            HOST            CLIENT-ID
ride-billing    rides.completed 0          42              68              26              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 1          29              58              29              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 2          9               70              61              dll1h3S8Tk-DNbaW3kuNww /192.168.65.1   billing-2

In the Java run, the lag on partition 2 grew from 48 to 61 in 16 seconds, while the lag of partitions 0 and 1 together, which billing-1 still bills, went from 54 to 55. In the Python run, the lag on partition 2 grew from 29 to 41, and on partitions 0 and 1 together from 59 to 64. Then the broker removes billing-2 from the group: its session times out 45 seconds after its last heartbeat, the default of group.consumer.session.timeout.ms. In the runs for this lesson, that came about 48 seconds after the crash. For a few seconds after that, a describe may print Warning: Consumer group 'ride-billing' is rebalancing. and show - as partition 2's member. Then billing-1 gets partition 2 back:

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID            HOST            CLIENT-ID
ride-billing    rides.completed 0          58              84              26              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 1          46              87              41              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 2          9               89              80              2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1

billing-1 holds all three partitions again. Partition 2 now has the highest lag: 80 rides in the Java run, 57 in the Python run.

Catching up

Stop the producer, in the command terminal:

terminal
docker compose stop producer
output
 Container broka-lab-kafka-consumer-lag-producer-1  Stopped

The ends of the logs stop moving. Read them once more, with the kafka-get-offsets.sh command of Start the broker and the producer:

rides.completed:0:85
rides.completed:1:91
rides.completed:2:92

In the third terminal, start billing-2 again, this time without a second argument:

mvn -q compile exec:java -Dexec.args=billing-2

Then open a fourth terminal, change into the lab's folder, and start a third member, billing-3:

cd java
mvn -q compile exec:java -Dexec.args=billing-3

The group now has three members, one for each partition. Describe the group:

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID            HOST            CLIENT-ID
ride-billing    rides.completed 2          24              92              68              9blGQvVOTBOZPIUl7WVNmg /192.168.65.1   billing-3
ride-billing    rides.completed 0          85              85              0               2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 1          63              91              28              FzfQuJpXSgmjILB3smrzxg /192.168.65.1   billing-2

This time the rows come in a different order; each still names its partition and its member. In the Java run, partition 0, which billing-1 kept, has already caught up: it is at 85, the end of its log. Describe the group every few seconds until the lag is 0 on every partition; it took between one and two minutes in the runs for this lesson:

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID            HOST            CLIENT-ID
ride-billing    rides.completed 2          92              92              0               9blGQvVOTBOZPIUl7WVNmg /192.168.65.1   billing-3
ride-billing    rides.completed 0          85              85              0               2cSGCbCKSbGkCcQL6ea_qw /192.168.65.1   billing-1
ride-billing    rides.completed 1          91              91              0               FzfQuJpXSgmjILB3smrzxg /192.168.65.1   billing-2

The group has caught up: three members bill three rides a second, and the producer writes none. More members bill faster, up to one per partition; a fourth would have nothing to read, as the fifth lesson showed.

Now look at the first lines of the two new members:

billing-2: bike-1409, partition 1, offset 48
billing-2: bike-1933, partition 1, offset 49
billing-2: bike-1164, partition 1, offset 50
billing-3: bike-1412, partition 2, offset 9
billing-3: bike-1288, partition 2, offset 10
billing-3: bike-1645, partition 2, offset 11

A member that takes a partition over starts at the offset last committed there. In the Java run, billing-2 had committed nothing before it crashed, so partition 2 was still at 9, where billing-1 had left it. billing-3 started there and billed bike-1412, bike-1288 and bike-1645 a second time: the first three of the five rides billing-2 billed before its crash. The other two came next. In the Python run, billing-2 had committed 23 before its crash, the offset after the last ride it billed, so the member that took the partition over started at 23 and nothing was billed twice. Which of the two you see depends on where the crash falls between two automatic commits, five seconds apart: the rides billed after the last commit come again. A later lesson shows how to commit by hand, so that fewer rides are billed twice after a crash.

Resetting offsets

Resetting a group's offsets means writing new committed offsets for it, so that its members start somewhere else: back, to bill rides again, or forward, to skip them. Kafka resets the offsets of a group only while the group has no members; a member that crashed still counts until its session times out. Try it while the three members still run, here with --shift-by -3, which moves each partition back three rides, and with --dry-run, which only shows what the reset would do:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --reset-offsets --group ride-billing --topic rides.completed --shift-by -3 --dry-run
output
Error: Assignments can only be reset if the group 'ride-billing' is inactive, but the current state is Stable.

GROUP           TOPIC           PARTITION  NEW-OFFSET

The tool refuses, and prints the heading of an empty plan. The group is Stable, with members reading: a running member would commit its own position over the new offsets.

Stop the three members with Ctrl+C. Each closes, which commits the position after its last ride. Their last lines:

billing-1: bike-1337, partition 0, offset 83
billing-1: bike-1534, partition 0, offset 84
billing-2: bike-1239, partition 1, offset 89
billing-2: bike-1019, partition 1, offset 90
billing-3: bike-1038, partition 2, offset 90
billing-3: bike-1037, partition 2, offset 91

Check that the group is empty, with --state:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --describe --group ride-billing --state
output
Consumer group 'ride-billing' has no active members.

GROUP           COORDINATOR (ID)          ASSIGNMENT-STRATEGY  STATE                #MEMBERS
ride-billing    kafka:29092  (1)          uniform              Empty                0

Empty, with 0 members: the group still exists, and so do its committed offsets. Now the tool can reset them.

Suppose the billing went wrong from the moment you noted the time in A member that dies, and every ride written since then has to be billed again. --to-datetime finds, on each partition, the first ride written at or after a time. Run it with your time in place of the one below, and without --dry-run or --execute:

docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --reset-offsets --group ride-billing --topic rides.completed --to-datetime 2026-11-18T12:48:10.000

The tool prints a warning, then the plan: the new offset it would give each partition.

WARN: No action will be performed as the --execute option is missing. In version 5.0, this command will require either --dry-run or --execute to be specified. You should add the --dry-run option explicitly if you are scripting this command and want to keep the current default behavior without prompting.

GROUP           TOPIC           PARTITION  NEW-OFFSET
ride-billing    rides.completed 2          46
ride-billing    rides.completed 0          43
ride-billing    rides.completed 1          39

Without either option, the tool only prints the plan, as --dry-run does, and warns that from Kafka 5.0 on it will ask for one of the two. Always give --dry-run first: it shows the plan and changes nothing. A time without a zone, like this one, is read as UTC, which is why you noted it with date -u.

Each new offset is the first ride written at or after your time. They are close to the log end offsets of your second describe in Falling behind, which you ran just before you noted the time; in the runs for this lesson, they were the same. Billing everything again from there would mean 140 rides in the Java run and 128 in the Python run.

That many rides would take a while to bill again. To see a reset work, move the group back only three rides on each partition, with --shift-by -3, as a dry run first:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --reset-offsets --group ride-billing --topic rides.completed --shift-by -3 --dry-run
GROUP           TOPIC           PARTITION  NEW-OFFSET
ride-billing    rides.completed 2          89
ride-billing    rides.completed 0          82
ride-billing    rides.completed 1          88

Each new offset is the committed offset minus three. The plan lists the partitions in an order of its own. Nothing has moved yet; to move the offsets, run the same command with --execute in place of --dry-run:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --reset-offsets --group ride-billing --topic rides.completed --shift-by -3 --execute

It prints the same plan, and this time writes it. Describe the group:

Consumer group 'ride-billing' has no active members.

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
ride-billing    rides.completed 0          82              85              3               -               -               -
ride-billing    rides.completed 1          88              91              3               -               -               -
ride-billing    rides.completed 2          89              92              3               -               -               -

The lag is 3 on each partition, and the member columns show -: the group has no members. Start billing-1 again, in its terminal; the Up arrow brings its command back. It bills the last three rides of each partition a second time:

billing-1: bike-1203, partition 0, offset 82
billing-1: bike-1337, partition 0, offset 83
billing-1: bike-1534, partition 0, offset 84
billing-1: bike-1709, partition 1, offset 88
billing-1: bike-1239, partition 1, offset 89
billing-1: bike-1019, partition 1, offset 90
billing-1: bike-1653, partition 2, offset 89
billing-1: bike-1038, partition 2, offset 90
billing-1: bike-1037, partition 2, offset 91

The last two on each partition are the rides the three members billed last, shown above, at the same offsets. Stop billing-1 with Ctrl+C.

The tool has other ways to pick the new offsets; a reset uses one of them:

Option New offset on each partition
--to-earliest the first offset still in the log
--to-latest the end of the log
--to-offset 42 offset 42
--shift-by -3 the committed offset, moved by the number: back if negative, forward if positive
--to-datetime 2026-11-18T12:48:10.000 the first offset written at or after that time
--by-duration PT10M the first offset written at or after that long ago, here ten minutes
--to-current the committed offset: nothing moves
--from-file plan.csv the offsets listed in a file

The partitions a reset applies to come from another option: --topic rides.completed takes every partition of the topic, --topic rides.completed:0,2 only partitions 0 and 2, and --all-topics every topic the group has committed offsets on; --from-file takes its partitions from the file. If a new offset falls outside the log, before its first record or past its end, the tool takes the nearest end instead and logs a warning.

Deleting a group's offsets

Resetting moves a group's offsets; you can also delete them. With no member running, delete the group's offsets on the topic:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --delete-offsets --group ride-billing --topic rides.completed
output
Request succeeded for deleting offsets from group ride-billing.

TOPIC           PARTITION  STATUS
rides.completed 0          Successful
rides.completed 1          Successful
rides.completed 2          Successful

Describe the group:

output
Consumer group 'ride-billing' has no active members.

No rows: the group has no offsets left. It still exists, though:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --list
output
ride-billing

Start billing-1 again. With nothing committed, it starts where auto.offset.reset says, earliest, at the beginning of each partition, and bills the oldest rides again:

billing-1: bike-1319, partition 0, offset 0
billing-1: bike-1401, partition 0, offset 1
billing-1: bike-1925, partition 0, offset 2
billing-1: bike-1077, partition 0, offset 3
billing-1: bike-1368, partition 0, offset 4
billing-1: bike-1234, partition 0, offset 5
billing-1: bike-1688, partition 0, offset 6

Stop it with Ctrl+C after a few rides, and describe the group:

Consumer group 'ride-billing' has no active members.

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
ride-billing    rides.completed 0          7               85              78              -               -               -
ride-billing    rides.completed 1          0               91              91              -               -               -
ride-billing    rides.completed 2          0               92              92              -               -               -

The two clients differ here. The Java client commits a position on every partition the member holds, even one it has not billed from yet: its describe shows all three partitions, two of them at 0 with the whole log as lag. The Python client commits a position only on the partitions it has read from, so its describe shows one row. Either way, the next member starts at the beginning of each partition that billing-1 has not billed from.

Finally, delete the group itself:

terminal
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --delete --group ride-billing
output
Deletion of requested consumer groups ('ride-billing') was successful.

--delete removes the group and all its offsets, and, like a reset, works only when no member runs: while one does, it fails with GroupNotEmptyException. --list now prints nothing. The next member that joins ride-billing creates the group anew, and starts at the beginning.

Try it yourself

Each of these starts with no members running: stop them all with Ctrl+C before you begin the next one.

  1. Start the producer again, with docker compose up -d producer, and wait about twenty seconds, so that rides pile up. Then skip them. The group no longer exists, but a reset creates it: run the reset command of Resetting offsets with --to-latest in place of --shift-by -3, first with --dry-run, then with --execute. The two plans may differ by a ride or two, because the producer writes between them. Start billing-1: it bills only the rides written after the reset. Moving forward like this skips rides for good: nothing bills the rides that piled up.
  2. Recreate the producer with four rides a second: RATE=4 docker compose up -d producer. Start three members, and describe the group twice, half a minute apart: the total lag still grows, by about a ride a second, because three members bill three rides a second and the producer writes four. In the Java run, it went from 71 to 105 in 31 seconds. A fourth member would not help: with three partitions, it would have nothing to read. The number of partitions caps how many members can share the work. Stop the members, then the producer, with docker compose stop producer.
  3. Start billing-1, and run the --delete-offsets command of Deleting a group's offsets while it runs. The tool prints Error: Encountered some partition-level error, see the follow-up details., and each partition's row shows GroupSubscribedToTopicException: Deleting offsets of a topic is forbidden while the consumer group is actively subscribed to it. Stop billing-1.
  4. Save the group's committed offsets before a reset, and put them back. With no member running, save them to a file with --to-current --dry-run --export, move the group to the beginning, copy the file into the container, and reset from it:
    terminal
    docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --reset-offsets --group ride-billing --topic rides.completed --to-current --dry-run --export > ride-billing.csv
    docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --reset-offsets --group ride-billing --topic rides.completed --to-earliest --execute
    docker compose cp ride-billing.csv kafka:/tmp/ride-billing.csv
    docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:29092 --reset-offsets --group ride-billing --from-file /tmp/ride-billing.csv --execute
    ride-billing.csv has one line per partition: the topic, the partition and the offset, as in rides.completed,0,160. A partition with nothing committed is saved at the end of its log, so a reset from the file skips its rides. Describe the group after each step: the second command moves every partition to 0, and the last puts back the saved offsets. The tool runs inside the container and reads the file there, which is why docker compose cp copies it in first. Delete ride-billing.csv afterwards if you want.

What to remember

  • A group's lag on a partition is the log end offset minus the group's committed offset: the rides written there that the group has not committed yet. With automatic commits, every five seconds, it is only as fresh as the last commit.
  • The lag grows by the difference between what producers write and what the members bill. More members bill faster, up to one per partition.
  • A member that crashes keeps its partitions until its session times out, 45 seconds by default, and their lag piles up meanwhile. The member that takes a partition over starts at the offset last committed there, so the rides billed after that commit are billed again.
  • Kafka resets a group's offsets only while the group is Empty, with no members. Run a reset with --dry-run first, then with --execute; with neither, the tool only prints the plan, and Kafka 5.0 will ask for one of the two.
  • --to-datetime reads a time without a zone as UTC; --shift-by moves the committed offset by a number. A new offset outside the log goes to the nearest end.
  • Moving back bills rides again; moving forward skips them for good. Save the offsets first with --to-current --dry-run --export, and put them back with --from-file.
  • --delete-offsets deletes a group's offsets on a topic, but only while no member reads it; the next member then starts where auto.offset.reset says. --delete removes the whole group, but only when it has no members.

Clean up, or carry on

To remove the lab and its data, run this in the lab's folder:

terminal
docker compose --profile rides down -v

The producer is in a profile, rides, so name the profile: without --profile rides, down may leave the producer running. Your runs left java/target or python/.venv in the lab's folder; delete it too if you want the folder as you cloned it. Or keep everything: with the producer stopped, nothing in this lab writes on its own, so it stays as you left it, on ports 19235 and 19135, next to the earlier labs if they run too. The lab's README has the short form of these commands and the connection values.