# Exercise 03 — Consumer groups: who gets which partition, and the rebalance

**VERIFIED** 2026-11-21 — CLI rebalance and the Java lesson (`03 · consumer`)
both run; observed output in `../VERIFICATION.md` §3.

**Time:** ~45 min · **Prerequisites:** Exercises 01–02; `course.groups`
exists (created in Exercise 02, step 4).

## The situation

Two consumers join one group to share the work. Then one of them dies —
the classic 2 a.m. event. You will watch the coordinator reassign its
partitions, watch the survivor inherit the *committed offsets* (not the
in-flight ones), and meet the delivery guarantee that explains every
"why did we process this twice?" ticket you will ever file.

## Predict first

1. Six partitions, two consumers in one group: how are partitions
   assigned? What does each consumer *not* receive?
2. Consumer B dies. Which partitions stop being consumed — B's, or all six?
3. The records B had polled but not yet committed when it died: lost,
   duplicated, or exactly-once?
4. A second group consuming the same topic: does group 1's membership churn
   delay group 2?

## The experiment — CLI (working directory: `kafka-lab`; 12 records already
on `course.groups` from Exercise 02)

**1. Two consumers, one group** — run these as **two separate terminals**,
each with a distinguishable client id:

```bash
# terminal A
scripts/k kafka-console-consumer.sh --topic course.groups \
  --group course.workers --from-beginning --timeout-ms 60000 \
  --consumer-property client.id=worker-a

# terminal B
scripts/k kafka-console-consumer.sh --topic course.groups \
  --group course.workers --from-beginning --timeout-ms 60000 \
  --consumer-property client.id=worker-b
```

(For scripted verification we ran the same two commands in the background
with output redirected; interactively, two terminals is the honest way to
watch both sides at once.)

**2. The group's state while both are alive:**

```bash
scripts/k kafka-consumer-groups.sh --describe --state --group course.workers
scripts/k kafka-consumer-groups.sh --describe --members --group course.workers
```

**3. Kill consumer B** — in terminal B, `Ctrl-C`.

**4. The group's state again:**

```bash
scripts/k kafka-consumer-groups.sh --describe --state --group course.workers
scripts/k kafka-consumer-groups.sh --describe --group course.workers
```

## The experiment — Java (a scripted rebalance)

The lab's consumer lesson runs three scripted consumers in one group,
prints their revoked→assigned transitions, and closes one to force a live
rebalance:

```bash
scripts/run.sh lab.ConsumerLesson
```

Watch section `03 · three consumers, one group — then one leaves (live
rebalance)` and its summary line: the survivors picked up the leaver's
partitions **and its committed offsets**.

## Expected observations

- Step 2: `STATE Stable`, `#MEMBERS 2`, and each member assigned **3
  partitions** (6 ÷ 2, range strategy — visible in `--members`).
- Step 4, after B dies: `#MEMBERS 1`, still `Stable`, and the survivor's
  consumer-id now holds **all six partitions** in the plain `--describe`.
- In the verified run, worker-a's terminal printed **all 12 records** and
  worker-b's printed **0** — remember Exercise 02: all 12 records sat on
  *one* sticky partition, which was assigned to A. The group was fair; the
  *data* was skewed. (If your burst spread across partitions, each
  consumer's count will reflect its partitions' load instead — either way,
  the describe output, not the counts, is the assignment evidence.)
- The Java lesson's survivors "picked up its partitions AND its committed
  offsets" — the group's ledger is shared; a partition handover is an offset
  handover.

## Understanding checks

1. Groups broadcast to each other; partitions load-balance *within* a
   group. Which of your outputs is direct evidence of each half?
2. B polled a record, processed it, and died before the offset commit.
   Walk through what A does with that record after the rebalance. What is
   the delivery guarantee called?
3. What must every consumer-side handler therefore be, for correctness?
   (One word — and it is the title of the concept Exercise 04 depends on.)
4. During the rebalance window — who pauses: the survivor, the joining
   consumer, or the whole group? Which output line proves your answer?

## Hints

- For "who pauses": the coordinator reassigns *all* partitions at a
  rebalance in the default (eager) protocol. Look at what both terminals
  stop printing between B's death and the next records.
- For the duplicate question: an offset commit is a single number per
  partition. Can it record "processed but not committed"? Or does the next
  reader start at the last committed number?
- Worker-b printed nothing — before blaming the group, recall which
  partition(s) hold the records, and who was assigned them.

## Troubleshooting

- `#MEMBERS` never reaches 2 — one consumer exited early (check the
  `--timeout-ms` gives you time; raise it).
- Killing B froze terminal A too — that's the rebalance pause, not a hang;
  it clears in a few seconds. `../TROUBLESHOOTING.md` §5 has the timeout
  knobs if it doesn't.

## Verification

You are done when your own output shows all three:

1. `#MEMBERS 2` with 3 partitions each (step 2).
2. `#MEMBERS 1` with the survivor holding 6 partitions (step 4).
3. The Java lesson's rebalance summary line, and your own one-sentence
   statement of the delivery guarantee (at-least-once → handlers must be
   **idempotent**).

## Solution (open after your attempt)

→ [`../solutions/03-consumer-groups.md`](../solutions/03-consumer-groups.md)
