Lesson 04 of 17 · Foundations
Lab: your first Kafka cluster
Run a real single-node Kafka 4.x cluster in Docker and use the command-line tools for every core idea: create and describe a topic, produce keyed records and see which partition each lands in, consume with a group, watch lag grow, reset offsets to replay, and change a topic's retention. Every output shown was captured from a real run.
You will run a one-node Kafka cluster on your laptop and touch every idea from lesson 3. You need Docker and about 1 GB of free memory. The outputs below are from a real run of Kafka 4.1; IDs and timestamps will differ on yours. Already have a Kubernetes cluster? Do this lab first anyway (it is the quickest way to learn the tools), then lesson 5 runs the same ideas on Kubernetes with Strimzi.
Step 1: start Kafka
$ docker run -d --name kafka -p 9092:9092 apache/kafka:4.1.0
$ docker logs kafka 2>&1 | grep -m1 "Kafka Server started"
This one container is both broker and KRaft controller. The tools live in /opt/kafka/bin; to keep commands short, open a shell inside the container:
$ docker exec -it kafka bash
$ cd /opt/kafka/bin
$ export B="--bootstrap-server localhost:9092"
Step 2: create and describe a topic
$ ./kafka-topics.sh $B --create --topic orders --partitions 3 --replication-factor 1
Created topic orders.
$ ./kafka-topics.sh $B --list
orders
$ ./kafka-topics.sh $B --describe --topic orders
Topic: orders TopicId: 8mTfBX5FSZS2auLxG0_-1g PartitionCount: 3 ReplicationFactor: 1 Configs: min.insync.replicas=1,segment.bytes=1073741824
Topic: orders Partition: 0 Leader: 1 Replicas: 1 Isr: 1 Elr: LastKnownElr:
Topic: orders Partition: 1 Leader: 1 Replicas: 1 Isr: 1 Elr: LastKnownElr:
Topic: orders Partition: 2 Leader: 1 Replicas: 1 Isr: 1 Elr: LastKnownElr:
| Field | Meaning |
|---|---|
Leader: 1 |
Broker 1 serves reads and writes for this partition |
Replicas: 1 |
Brokers holding a copy (only one here) |
Isr: 1 |
In-sync replicas: copies that are fully up to date (lesson 8) |
Elr |
Eligible leader replicas, a newer safety feature for leader election |
Step 3: produce keyed records
The console producer reads lines from standard input. With parse.key=true, everything before : is the key:
$ ./kafka-console-producer.sh $B --topic orders --property parse.key=true --property key.separator=:
>order-1:{"id":1,"total":20}
>order-2:{"id":2,"total":35}
>order-3:{"id":3,"total":12}
>order-1:{"id":1,"status":"paid"}
> (press Ctrl+D to finish)
Step 4: consume with a group
$ ./kafka-console-consumer.sh $B --topic orders --from-beginning --group billing \
--property print.key=true --property print.partition=true --property print.offset=true
Partition:0 Offset:0 order-2 {"id":2,"total":35}
Partition:0 Offset:1 order-3 {"id":3,"total":12}
Partition:1 Offset:0 order-1 {"id":1,"total":20}
Partition:1 Offset:1 order-1 {"id":1,"status":"paid"}
^CProcessed a total of 4 messages
Look closely:
- Both
order-1records are in partition 1, in the order they were sent. Same key, same partition, order kept. - Offsets are counted per partition: partition 0 and partition 1 both have an offset 0.
- Records from different partitions arrive interleaved: there is no order across partitions.
Step 5: offsets and lag
The consumer committed its position for the group billing:
$ ./kafka-consumer-groups.sh $B --describe --group billing
Consumer group 'billing' has no active members.
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
billing orders 0 2 2 0 - - -
billing orders 1 2 2 0 - - -
billing orders 2 0 0 0 - - -
Now produce ten more records while the group is stopped, all with the same key:
$ seq 1 10 | sed 's/^/order-x:/' | ./kafka-console-producer.sh $B --topic orders \
--property parse.key=true --property key.separator=:
$ ./kafka-consumer-groups.sh $B --describe --group billing
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
billing orders 0 2 2 0 - - -
billing orders 1 2 2 0 - - -
billing orders 2 0 10 10 - - -
Lag = LOG-END-OFFSET − CURRENT-OFFSET: ten records are waiting for billing, all in partition 2 because they share one key. Lag is the single most important consumer health number.
Step 6: replay by resetting offsets
Because data stays in the log, a group can go back. Always try --dry-run first:
$ ./kafka-consumer-groups.sh $B --group billing --topic orders --reset-offsets --to-earliest --dry-run
GROUP TOPIC PARTITION NEW-OFFSET
billing orders 0 0
billing orders 1 0
billing orders 2 0
$ ./kafka-consumer-groups.sh $B --group billing --topic orders --reset-offsets --to-earliest --execute
The next time billing starts, it re-reads everything. Other options include --to-latest, --to-datetime 2026-11-08T10:00:00.000 and --shift-by -100. The group must have no running members, or they would overwrite the reset.
Step 7: topic settings and offsets
$ ./kafka-configs.sh $B --entity-type topics --entity-name orders --alter --add-config retention.ms=86400000
Completed updating config for topic orders.
$ ./kafka-configs.sh $B --entity-type topics --entity-name orders --describe
Dynamic configs for topic orders are:
retention.ms=86400000 sensitive=false synonyms={DYNAMIC_TOPIC_CONFIG:retention.ms=86400000}
$ ./kafka-get-offsets.sh $B --topic orders
orders:0:2
orders:1:2
orders:2:10
Retention is now one day for this topic only. kafka-get-offsets.sh shows the end offset of each partition.
The consumer group is like a bookmark in a shared book. Reading doesn't tear pages out; the bookmark just moves. Resetting offsets is moving your bookmark back to page one so you can read the story again, while everyone else's bookmarks stay where they were.
Step 8: clean up
$ exit
$ docker rm -f kafka
Try it: on your own
- Create a topic
clickswith 6 partitions. Produce 20 records without keys and check how they spread across partitions. - Start two console consumers in the same group in two terminals, produce more records, and see each consumer receive a different share.
- Start a third consumer in a different group and confirm it receives every record.
- Stop all consumers, reset one group with
--to-datetimeto a time in the middle of your test, and replay. - Run
kafka-consumer-groups.sh --describe --group <g> --memberswhile consumers are running and match members to partitions.
Going deeper
- The console tools are perfect for learning and debugging, but production producers and consumers are applications using a client library (Java, Go, Python, .NET). They expose the same settings you will meet in lessons 6 and 7.
- Put frequently used flags in a client properties file and pass it with
--command-config(admin tools) or--producer.config/--consumer.config; lesson 14 uses this for TLS.
Recap
- One container runs a full KRaft Kafka; the CLI tools are in
/opt/kafka/bin. - Same key → same partition → order kept; offsets are per partition.
- A group's committed offsets and lag are visible with
kafka-consumer-groups.sh. - Resetting offsets replays data; topic settings change with
kafka-configs.sh.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.