PleinIQ
Menu
Language

Choose your language

This page is only in English for now. Choosing a language opens its home page.

Other languages

Your browser can translate any page into almost any language.

Other browsers: Open the browser menu and look for “Translate”.

Your resume, your downloads and lab commands are never translated.

Roadmaps / Kafka / Lab 3

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.

  • Install
    /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 ↗
    Install
    brew install --cask multipass

    Check it worked: multipass version

Before you start

  • Start your server, open two shells on it (multipass shell kafka-lab in two terminals), and run source .venv/bin/activate in ~/kafka-streaming-lab in both.

Step by step

  1. 1 Write a long-running group consumer

    This consumer keeps running and prints which partitions it was given. Save it as group_consumer.py.

    File: 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. 2 Start two workers

    Run one worker in each shell. Kafka splits the three partitions between them.

    Run this on your lab server
    # first shell
    python group_consumer.py worker-a
    # second shell
    python group_consumer.py worker-b
    You should see:

    One worker gets two partitions and the other gets one, for example worker-a got partitions [0, 1] and worker-b got partitions [2].

  3. 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.

    Run this on your lab server
    cd ~/kafka-streaming-lab && source .venv/bin/activate
    python producer.py
    You should see:

    The new orders appear in the workers’ output, each in one worker only.

  4. 4 Stop both workers and measure lag

    Stop the workers with Ctrl+C, produce once more, then describe the group. LAG is how many events are waiting for this group.

    Run this on your lab server
    python producer.py > /dev/null
    kafka consumer-groups --describe --group order-workers
    You should see:

    A row per partition with CURRENT-OFFSET, LOG-END-OFFSET and a LAG that adds up to 6.

  5. 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-run first; the group must have no running members.

    Run this on your lab server
    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-workers
    You should see:

    After --execute, CURRENT-OFFSET is back to 0 and LAG equals all the orders.

  6. 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.

    File: 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. 7 Commit and push

    Save your work.

    Run this on your lab server
    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.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.

Join PleinIQ Updates

What are you interested in?

Read how we handle your information in our privacy notice.