Learn Labs
Foundations

CLI Basics

Producing, consuming, groups, offsets, and configs from the command line

The command-line tools are how you'll inspect and fix things in production, so they're worth knowing before any client code. All commands below assume the helper from Setup:

K() { docker exec -i kafka-1 /opt/kafka/bin/"$@"; }
B="--bootstrap-server localhost:19092"     # bash; in zsh, type it out or use an array

Topics

kafka-topics.sh $B --listkafka-topics.sh $B --create --topic orders --partitions 6 --replication-factor 3kafka-topics.sh $B --create --topic audit --config retention.ms=259200000030 dayskafka-topics.sh $B --describe --topic orderskafka-topics.sh $B --describe --under-replicated-partitionsanything not fully replicatedkafka-topics.sh $B --alter --topic orders --partitions 12partitions can only growkafka-topics.sh $B --delete --topic orders

Producing and consuming

# produce lines from stdin; key:value with a separator
printf 'k0:v1\nk1:v2\nk0:v3\n' | K kafka-console-producer.sh $B --topic demo \
--reader-property parse.key=true --reader-property key.separator=:

# consume from the beginning, showing keys, partitions, and offsets
K kafka-console-consumer.sh $B --topic demo --from-beginning \
--formatter-property print.key=true \
--formatter-property print.partition=true \
--formatter-property print.offset=true

The console consumer without --group uses a throwaway group, reads, and commits nothing. Add --group my-app to consume as part of a real group. (Older tutorials use --property for these options; Kafka 4.3 still accepts it but warns that it's deprecated.)

Offsets

K kafka-get-offsets.sh $B --topic demo
demo:0:0
demo:1:0
demo:2:5        # partition 2 holds offsets 0-4; the next record gets offset 5

All five keyed records above went to partition 2 in this lab: keys k0 and k1 happen to hash to the same partition with 3 partitions. That's fine, and a reminder that keys spread records only statistically.

Consumer groups

kafka-consumer-groups.sh $B --listkafka-consumer-groups.sh $B --describe --group billingper-partition offset, end, LAGkafka-consumer-groups.sh $B --describe --group billing --memberswho has which partitionskafka-consumer-groups.sh $B --describe --group billing --stateStable, PreparingRebalance, Empty...kafka-consumer-groups.sh $B --group billing --topic payments --reset-offsets --to-earliest --executekafka-consumer-groups.sh $B --group billing --topic payments --reset-offsets --to-datetime 2026-10-12T00:00:00.000 --executekafka-consumer-groups.sh $B --delete --group billing
GROUP      TOPIC   PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG   CONSUMER-ID
analytics  clicks  0          342             1370            1028  rdkafka-13b4...
analytics  clicks  1          294             1217            923   rdkafka-13b4...

LAG = log-end offset − committed offset: how many records the group hasn't processed yet. It's the single most important consumer metric.

Configs

Topic and broker settings can be changed at runtime:

kafka-configs.sh $B --describe --entity-type topics --entity-name demokafka-configs.sh $B --alter --entity-type topics --entity-name demo --add-config retention.ms=3600000kafka-configs.sh $B --alter --entity-type topics --entity-name demo --delete-config retention.mskafka-configs.sh $B --describe --entity-type brokers --entity-name 1 --allevery broker settingkafka-configs.sh $B --alter --entity-type brokers --entity-default --add-config log.retention.ms=...

Inspecting the data on disk

docker exec kafka-1 /opt/kafka/bin/kafka-dump-log.sh \
--files /var/lib/kafka/data/demo-2/00000000000000000000.log --print-data-log

baseOffset: 0 lastOffset: 4 count: 5 baseSequence: 0 lastSequence: 4 producerId: 2001 ...
| offset: 0 CreateTime: 1791799990488 keySize: 2 valueSize: 2 sequence: 0 key: k0 payload: v1
| offset: 1 ... key: k1 payload: v2
...

kafka-dump-log.sh must run on a broker that holds that replica (here, demo-2 exists on every broker because the replication factor is 3). You'll use it in the next chapter.

Performance tools

K kafka-producer-perf-test.sh --topic demo --num-records 500000 --record-size 200 \
--throughput -1 --command-property bootstrap.servers=localhost:19092 acks=all

K kafka-consumer-perf-test.sh $B --topic demo --messages 500000

Lab

  1. Create orders with 6 partitions, produce 10 keyed records, and consume them showing partition and offset.
  2. Consume with --group lab and stop with Ctrl-C. Describe the group.
  3. Produce 5 more, describe again, and read the LAG.
  4. Reset the group to the earliest offset with --dry-run, then --execute, and consume again.
  5. Set retention.ms=60000 on orders, then delete the override.

On this page