Lab 3 of 8 · Consumer groups, offsets and delivery guarantees
Share the work and replay events
Run two consumers in one group, see the partitions split between them, measure lag, and replay the topic by resetting offsets.
- Time
- 60 minutes
- Cost
- Free.
What you learn
Consumer groups let several consumers share the work of one topic, and committed offsets decide what is read again after a crash. You watch Kafka split partitions between consumers, measure lag and replay events.
- How a group divides partitions among its members
- Committed offsets, lag and rebalancing
- At-most-once, at-least-once and exactly-once delivery
- Resetting offsets to replay events safely
In the AI era: Ask AI for "exactly-once" and it may suggest settings that only cover the producer. Delivery guarantees depend on when you commit; test the failure cases yourself.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
- Multipass (a free Ubuntu server on your computer)Official install page ↗
brew install --cask multipassCheck it worked:
multipass version
Before you start
Start your server, open two shells on it (
multipass shell kafka-labin two terminals), and runsource .venv/bin/activatein~/kafka-streaming-labin both.
Step by step
1 Write a long-running group consumer
This consumer keeps running and prints which partitions it was given. Save it as
group_consumer.py.import json import sys from confluent_kafka import Consumer name = sys.argv[1] def assigned(consumer, partitions): print(f"{name} got partitions {[p.partition for p in partitions]}", flush=True) consumer = Consumer( { "bootstrap.servers": "localhost:9092", "group.id": "order-workers", "auto.offset.reset": "earliest", "enable.auto.commit": False, } ) consumer.subscribe(["orders"], on_assign=assigned) try: while True: msg = consumer.poll(1.0) if msg is None or msg.error(): continue order = json.loads(msg.value()) print(f"{name} handled order {order['order_id']} from partition {msg.partition()}", flush=True) consumer.commit(message=msg, asynchronous=False) except KeyboardInterrupt: pass finally: consumer.close()2 Start two workers
Run one worker in each shell. Kafka splits the three partitions between them.
# first shell python group_consumer.py worker-a # second shell python group_consumer.py worker-bYou should see:One worker gets two partitions and the other gets one, for example
worker-a got partitions [0, 1]andworker-b got partitions [2].3 Send more orders and watch them shared
In a third shell (or after stopping one worker with Ctrl+C), run the producer again. Each order is handled by exactly one worker.
cd ~/kafka-streaming-lab && source .venv/bin/activate python producer.pyYou should see:The new orders appear in the workers’ output, each in one worker only.
4 Stop both workers and measure lag
Stop the workers with Ctrl+C, produce once more, then describe the group.
LAGis how many events are waiting for this group.python producer.py > /dev/null kafka consumer-groups --describe --group order-workersYou should see:A row per partition with
CURRENT-OFFSET,LOG-END-OFFSETand aLAGthat adds up to 6.5 Replay the topic
Resetting offsets makes the group read everything again, for example after fixing a bug in a consumer. Always run with
--dry-runfirst; the group must have no running members.kafka consumer-groups --group order-workers --topic orders --reset-offsets --to-earliest --dry-run kafka consumer-groups --group order-workers --topic orders --reset-offsets --to-earliest --execute kafka consumer-groups --describe --group order-workersYou should see:After
--execute,CURRENT-OFFSETis back to 0 andLAGequals all the orders.6 Write down the delivery guarantee
Your workers commit after handling each order. Explain what happens if a worker crashes after handling an order but before committing it. Create it with
nano notes/03-groups.md.# Consumer groups and delivery - How the partitions were split: ... - What lag measured: ... - If a worker crashes after handling but before committing: ... - So our guarantee is: at-least-once / at-most-once (choose and explain) - How to make handling safe to repeat (idempotent): ...7 Commit and push
Save your work.
git add group_consumer.py notes git commit -m "Add consumer group experiments and notes" git push
You are done when
Two workers shared the three partitions.
You measured lag with
kafka consumer-groups --describe.You replayed the topic by resetting offsets, after a dry run.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.