# Exercise 01 — Your first message (and what reading really does)

**VERIFIED** 2026-11-21 — every command below was run against Kafka 3.9.0
(3-broker KRaft compose); observed output is in `../VERIFICATION.md` §1.

**Time:** ~30 min · **Prerequisites:** `SETUP.md` done; terminal ready.

## The situation

A teammate who only knows queues says: *"I'll read the events, process them,
and they're gone."* You are about to find the three sentences that prove
them wrong — and the one place they're accidentally right.

## Predict first — write your answers before running anything

1. You send three records, then start a **brand-new consumer group** with no
   `--from-beginning` flag. What will it print?
2. Same group, second run, now **with** `--from-beginning`: what changes?
3. A *different brand-new* group with `--from-beginning`: what does it see?
4. After all that reading, is any record gone from the topic?

## The experiment

Working directory: your `kafka-lab` checkout. Records produced: **3** total.

**1. Create a one-partition topic (ordering is trivially true here):**

```bash
scripts/k kafka-topics.sh --create --topic course.notes \
  --partitions 1 --replication-factor 3
# Created topic "course.notes".
```

**2. Produce three records** (the console producer reads lines from stdin;
`Ctrl-D`/EOF ends it — here a `printf` pipe does the same):

```bash
printf 'hello\nworld\nkafka\n' | \
  scripts/k kafka-console-producer.sh --topic course.notes
```

**3. Read 1 — a brand-new group, no `--from-beginning`.** The consumer
exits after 4 idle seconds (`--timeout-ms`):

```bash
scripts/k kafka-console-consumer.sh --topic course.notes \
  --group course.readers --timeout-ms 4000
```

**4. Read 2 — same group, now with `--from-beginning`:**

```bash
scripts/k kafka-console-consumer.sh --topic course.notes \
  --group course.readers --from-beginning --timeout-ms 4000
```

**5. Read 3 — a different new group with `--from-beginning`:**

```bash
scripts/k kafka-console-consumer.sh --topic course.notes \
  --group course.readers-2 --from-beginning --timeout-ms 4000
```

**6. Inspect the group's ledger** — the three columns your on-call reads:

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

## Expected observations (compare with your predictions)

- Read 1 prints **nothing** — a new group with no committed offset defaults
  to `auto.offset.reset=latest` and starts at the end of the log.
- Read 2 prints **nothing either** — this is the trap. `--from-beginning`
  only decides where a group starts when it has **no committed offset**;
  read 1's exit committed one.
- Read 3 prints all three records, in order.
- The describe shows `CURRENT-OFFSET 3`, `LOG-END-OFFSET 3`, `LAG 0` —
  three reads later, the log still holds all three records. Reading moved
  the **cursor**, not the data.

## Understanding checks

1. Where exactly does `--from-beginning` stop being a "rewind"? Whose state
   wins, the flag or the committed offset?
2. Which of your four predictions was wrong, and what assumption made it
   wrong?
3. If `course.readers` had processed a record and crashed *before* the
   offset commit, what would its next start point be — and what does that
   imply about "at-least-once delivery"?

## Hints (try these before the solution)

- The default for a consumer with no group offset is a *consumer config*,
  not a broker law. Which one? (`auto.offset.reset` — see the Kafka docs on
  consumer configs.)
- Read 2's surprise is not about the flag. Look at step 6's columns: who
  wrote `CURRENT-OFFSET 3`?
- Kafka is a **log**: an append-only file per partition. Queues delete on
  read; logs keep a *cursor* into the file. What in your output is the
  cursor, and what is the file?

## Troubleshooting

- "Consumer did not get any messages / exits immediately" — expected in
  reads 1 and 2; not a failure. See `../TROUBLESHOOTING.md` §2 if *read 3*
  prints nothing.
- Connection refused — `../TROUBLESHOOTING.md` §1.

## Verification

You are done when you can show, from your own terminal:

```bash
scripts/k kafka-consumer-groups.sh --describe --group course.readers
# CURRENT-OFFSET 3, LOG-END-OFFSET 3, LAG 0
```

…and explain, in one sentence each, why reads 1 and 2 printed nothing while
read 3 printed three records.

## Solution (open after your attempt)

→ [`../solutions/01-first-message.md`](../solutions/01-first-message.md)
