Kafka CLI Quick Reference
Create topics, produce and consume messages, and inspect consumer-group lag from the command line.
The rows use the Apache Kafka scripts in bin/; Confluent packages drop the .sh. Every tool needs --bootstrap-server host:9092, left out below to save space, and a secured cluster also needs a client properties file (--command-config).
The model: a topic is split into partitions, each an append-only log in which every message gets an offset. Producers append; consumers in a group divide the partitions among themselves and commit how far they have read. Messages stay until retention removes them, read or not, which is what makes replay possible.
--describe prints one row for each.kafka-topics.sh
- --create --topic orders
- with
--partitions 6and--replication-factor 3; RF cannot exceed the broker count - --list · --describe
- all topics · partitions, leaders, replicas and ISR for
--topic orders - --alter --partitions 12
- add partitions — never fewer, and existing keys then map to different partitions
- --delete --topic orders
- delete a topic (allowed by default via
delete.topic.enable) - --config k=v
- set topic config at create time, e.g.
retention.ms=86400000(one day) - --unavailable-partitions
- with
--describe: partitions without a leader;--under-replicated-partitionsshows lagging replicas - --if-not-exists
- make
--createsafe to rerun in scripts;--if-existsdoes the same for alter and delete - --command-config c.props
- client settings for a secured cluster: SASL, TLS, credentials
- --topics-with-overrides
- with
--describe: only topics whose config differs from the broker defaults
kafka-console-producer.sh
- --topic orders
- one message per line from stdin; Ctrl-D to finish
- parse.key=true
- with
--property, pluskey.separator=:, readskey:valuelines - acks=all
- via
--producer-property: wait for every in-sync replica — the default since Kafka 3.0 - --topic t < file.jsonl
- bulk-load a file, one message per line
kafka-console-consumer.sh
- --from-beginning
- read from the oldest retained offset; without it you only see new messages
- --group my-group
- join a consumer group, so offsets are committed and partitions shared
- print.key=true
--property print.key=true; alsoprint.timestamp,print.partition,print.offset- --partition 0 --offset 42
- read one partition from an exact offset;
--offsetrequires--partition - --max-messages 10
- stop after 10 messages — peek without scrolling forever
- --timeout-ms 5000
- exit when nothing arrives for 5 seconds — handy in scripts
- read_committed
- with
--isolation-level: hide writes from aborted transactions
kafka-consumer-groups.sh
- --list
- every consumer group the cluster knows
- --describe --group g
- per partition: CURRENT-OFFSET, LOG-END-OFFSET, LAG and the consumer that owns it
- --describe --all-groups
- lag for every group in one call
- --members --verbose
- with
--describe: each member and its assigned partitions - --state
- with
--describe: Stable, Empty or rebalancing, plus the coordinator broker - --reset-offsets
--to-earliest,--to-latest,--shift-by -100; only a dry run until--execute- --to-datetime
- reset to a timestamp such as
2026-01-01T00:00:00.000— replay from a point in time - --by-duration PT1H
- reset to one hour ago (an ISO-8601 duration)
- Reset gotcha
- offsets reset only while the group has no active members — stop the consumers first
- --delete --group g
- delete an inactive group and its committed offsets
- --delete-offsets
- with
--group g --topic t: forget the group’s offsets for one topic only
kafka-configs.sh & key settings
- --entity-type topics
--entity-name orders --describeshows overrides; add--allfor every value- --alter --add-config
- change a topic live, e.g.
retention.ms=3600000;--delete-configrestores the default - retention.ms · .bytes
- how long or how large a partition grows before old segments are deleted
- cleanup.policy=compact
- keep only the latest value per key — changelogs and table-like topics
- min.insync.replicas=2
- with
acks=alland RF 3, writes fail rather than land on fewer than 2 replicas - enable.auto.commit
- commits on a timer inside
poll()— safe only if each batch is finished before the next poll - linger.ms · batch.size
- producer batching: wait a few ms to fill bigger batches; add
compression.type=lz4 - auto.offset.reset
- where a group with no committed offset starts:
latest(default) orearliest
Other tools
- kafka-get-offsets.sh
--topic orders --time -1latest offsets,-2earliest; older Kafka:GetOffsetShell- kafka-delete-records.sh
- truncate partitions up to the offsets in
--offset-json-file— removes poison data - kafka-leader-election.sh
--election-type preferredwith--all-topic-partitionsrebalances leaders after a restart- kafka-acls.sh --list
- show ACLs;
--add --allow-principal User:appgrants access per topic or group - kafka-metadata-quorum.sh
describe --statusshows the KRaft controller quorum (no ZooKeeper)
Mental model
- Partition = ordering unit
- Order is guaranteed within a partition, never across them. Same key → same partition → ordered.
- Consumer group
- Each partition is read by exactly one consumer in the group — parallelism is capped at the partition count.
- Offset
- A consumer’s bookmark per partition. Committing it is what makes progress durable across restarts.
- Lag
- LOG-END-OFFSET − CURRENT-OFFSET: how far behind a consumer is. Growing lag = consumers can’t keep up.
- Retention ≠ queue
- Kafka keeps messages for a retention window even after consumption — replay is a feature, not a bug.
- Rebalance
- a consumer joining, leaving or exceeding
max.poll.interval.msreassigns partitions; processing pauses - Delivery guarantee
- at-least-once by default: commit after processing and make handlers idempotent
Worked example: size a consumer group from two lag readings
Suppose the billing group reads orders, which has 6 partitions, with 3 consumers owning 2 partitions each, and lag alerts are firing. Take two readings 60 seconds apart. Columns are trimmed, and since every partition behaves alike only partition 0 is shown:
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group billing
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
billing orders 0 481000 490000 9000 (all 6: 54000)
60 seconds later:
billing orders 0 498000 510000 12000 (all 6: 72000)
- Production rate. LOG-END-OFFSET rose by 20,000 in 60 s: 333 messages per second per partition, 2,000/s for the topic.
- Consumption rate. CURRENT-OFFSET rose by 17,000: 283/s per partition, 1,700/s in total, so each consumer, with two partitions, handles about 567/s. Lag grows by 300/s and will never recover by itself.
- Four consumers is not enough. Six partitions over four consumers split 2, 2, 1, 1. The two consumers still holding two partitions face 667/s against a capacity of about 567/s, so their partitions keep falling behind while the other two consumers have spare time.
- Six consumers works. One partition each means 333/s of demand against about 567/s of capacity, leaving about 233/s per partition for the backlog. Each partition is about 12,000 behind (72,000 / 6), so it clears in roughly 12,000 / 233 ≈ 51 seconds, plus the rebalance pause.
- A seventh consumer would sit idle. A partition has only one owner in a group, so past 6 the options are more partitions or a faster handler.
The model assumes a consumer’s throughput is set by its handler rather than by how many partitions it owns, which holds when processing, not fetching, is the bottleneck. Check the per-partition LAG column before scaling: if one partition grows while the rest stay flat, the cause is a hot key or a stuck consumer, and more consumers will not help.
Recipes
Create a topic and check it end to end
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders --partitions 6 --replication-factor 3
echo 'o-1:{"total":42}' | kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders --property parse.key=true --property key.separator=:
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --from-beginning --property print.key=true --max-messages 1Replication factor 3 needs at least three brokers; use 1 on a single-broker dev cluster. The consumer prints the key and value, then exits after one message.
Diagnose a lagging consumer group
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group billing
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group billing --members --verboseLag rising on every partition means the consumers are too slow overall (scale out, up to the partition count); lag on one partition points to a hot key or a stuck consumer.
Replay the last hour of events
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group billing --topic orders --reset-offsets --by-duration PT1H --dry-run
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group billing --topic orders --reset-offsets --by-duration PT1H --executeStop the group first, because offsets only reset when it has no active members. The dry run prints the new offset for each partition before anything changes.
Shorten retention on a topic that is filling the disk
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name logs --alter --add-config retention.ms=21600000
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name logs --describeSix hours of retention applies live, but space returns only as closed segments age out, so it is not instant. --delete-config retention.ms restores the broker default.
Measure producer and consumer throughput
kafka-producer-perf-test.sh --topic perf --num-records 1000000 --record-size 1000 --throughput -1 --producer-props bootstrap.servers=localhost:9092 acks=all
kafka-consumer-perf-test.sh --bootstrap-server localhost:9092 --topic perf --messages 1000000--throughput -1 removes the rate cap, and the producer summary reports records per second, MB per second and latency percentiles. Rerun with acks=1 to see what durability costs.
When to use it, and when not
The CLI tools are for operators and for checking things by hand: creating a topic in development, confirming a message landed, reading lag during an incident, resetting a group for a replay, changing retention on the spot. They ship with every Kafka download and talk to any cluster you can reach.
- Manage production topics as code (Terraform, Strimzi’s topic operator, or your platform’s API) so partition counts and configs are reviewed and repeatable. A one-off
--alteris easy to forget and impossible to review. - Use a real client for real traffic. The console producer and consumer are for peeking and smoke tests: one message per line of text, with no schema handling in the Apache scripts.
- Watch lag with monitoring, such as a lag exporter or Burrow feeding your alerting, rather than a cron job that scrapes
--describe.
Trade-offs and limits
| Choice | Gives you | Costs |
|---|---|---|
| More partitions | more consumers in parallel, more throughput | more files and leader elections; never reducible; keys remap when added |
| Replication factor 3 | copies on three brokers | three times the disk and replication traffic |
acks=all + min.insync.replicas=2 | an acknowledged write survives losing a broker | higher produce latency; writes fail while fewer than 2 replicas are in sync |
| Longer retention | replay further back | disk, on every replica |
cleanup.policy=compact | the latest value per key, kept indefinitely | history is gone; deleting a key takes a tombstone (a null value) |
enable.auto.commit | no commit code to write | a crash can reprocess a batch, or skip one if processing is asynchronous |
Common mistakes
- A constant key. Every message with the same key lands on the same partition, so a key such as
"orders"makes one partition hot while the others idle. Key by the entity whose order matters, such as the order ID. - Slow work inside the poll loop. If one batch takes longer than
max.poll.interval.ms(5 minutes by default), the consumer is removed from the group, its partitions move, and the uncommitted batch is processed again, often in a loop. Lowermax.poll.records(default 500) or speed up the handler. - acks=all with min.insync.replicas=1. The write is then acknowledged when only the leader has it, and if that broker dies the acknowledged data is gone. Set
min.insync.replicas=2with replication factor 3. - Rerunning --from-beginning with the same --group. The flag only applies when the group has no committed offsets, so the second run shows nothing old. Use a fresh group name, or no group, to peek again.
- Sending key:value lines without parse.key. The console producer then sends the whole line as the value with a null key, and the messages spread across partitions instead of following their keys.
Check yourself
A partition shows CURRENT-OFFSET 1200 and LOG-END-OFFSET 1950. What is the lag, and what offset will the next message get?
Lag 750. The log-end offset is the offset the next message will get, so 1950.
A topic has 6 partitions and the group runs 8 consumers. What happens?
Six consumers each own one partition and two sit idle. They are not wasted entirely: they take over if an active consumer leaves.
You grow a topic from 6 to 12 partitions. Roughly what share of existing keys now map to a different partition?
About half. The default partitioner uses hash % partitions, and hash % 12 is either hash % 6 or that plus 6, each for half of all hashes. Ordering per key breaks across the change for those keys.
Replication factor 3, min.insync.replicas=2, acks=all. Two brokers die; the leader survives. Can producers write? Can consumers read?
Writes fail with NOT_ENOUGH_REPLICAS, because only one replica is in sync. Consumers can still read what is already committed from the surviving leader.
Questions about Kafka CLI Cheat Sheet
How do I check consumer lag in Kafka?
Run kafka-consumer-groups.sh with --bootstrap-server, --describe and --group followed by the group name. For each partition it prints CURRENT-OFFSET, the position the group last committed, LOG-END-OFFSET, the offset the next message will get, and LAG, the difference between the two. Use --all-groups to see every group at once. Lag that grows steadily means consumers cannot keep up, while lag stuck on one partition usually points to a hot key or a stalled consumer. The figures come from committed offsets, so a group that commits rarely looks further behind than it is.
Why is my Kafka consumer not receiving old messages?
A consumer group with no committed offsets starts where auto.offset.reset says, and the default is latest, so it only sees messages produced after it joined. Use --from-beginning with the console consumer, set auto.offset.reset to earliest in your client, or reset an existing group’s offsets with kafka-consumer-groups.sh. If the group already has committed offsets, those win over both settings. Also check retention: messages older than retention.ms have already been deleted and cannot be read by anyone.
Can I reduce the number of partitions in a Kafka topic?
No. Kafka can add partitions to a topic but never remove them, because each partition is an independent log and deleting one would delete its data. Even adding partitions has a cost: the default partitioner maps a key to a partition by hashing it against the partition count, so keys move to new partitions and per-key ordering breaks across the change. If you really need fewer partitions, create a new topic with the right count, copy or re-produce the data into it, and switch producers and consumers over.
What is the difference between Kafka and a message queue?
A traditional queue such as RabbitMQ or SQS removes a message once a consumer acknowledges it and hands messages to consumers one at a time. Kafka is a replicated, append-only log: messages stay for the retention period whether or not anyone read them, each consumer group tracks its own offset, and any number of groups can read the same data independently. That makes replay, auditing and adding new consumers easy, while ordering holds only within a partition and parallelism is capped by the partition count.