Apache Kafka Tools and CLI Commands
Apache Kafka ships with a set of command-line tools in its bin directory. They let you create and inspect topics, send and read test messages, check consumer lag, reset offsets, inspect the KRaft quorum and measure performance, all without writing code.
This tutorial walks through the tools you use most often, in the order you would use them on a new cluster, with real output. Every tool connects with --bootstrap-server; the old --zookeeper options were removed in Kafka 4.0. The outputs shown were produced with the Kafka 4.0 tools against a local single-broker cluster; IDs, ports and numbers will differ on yours.
1. What Are the Kafka Tools?
The tools are shell scripts (.sh on Linux and macOS, .bat under bin/windows) that run Java classes from the Kafka distribution. Most of them use the Admin, producer or consumer APIs, so anything they do can also be done from code.
.sh suffix, for example kafka-topics with Homebrew. The options are the same.2. Starting a Local KRaft Cluster
A single-node cluster for development needs a cluster ID and formatted storage before the first start:
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
bin/kafka-storage.sh format --standalone -t $KAFKA_CLUSTER_ID -c config/server.properties
bin/kafka-server-start.sh config/server.properties
3. Managing Topics with kafka-topics.sh
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic invoices --partitions 3 --replication-factor 1
bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic invoices
Created topic invoices.
invoices
Topic: invoices TopicId: GHz919jiS0OcZaKCo8FopA PartitionCount: 3 ReplicationFactor: 1 Configs: min.insync.replicas=1
Topic: invoices Partition: 0 Leader: 0 Replicas: 0 Isr: 0 Elr: LastKnownElr:
Topic: invoices Partition: 1 Leader: 0 Replicas: 0 Isr: 0 Elr: LastKnownElr:
Topic: invoices Partition: 2 Leader: 0 Replicas: 0 Isr: 0 Elr: LastKnownElr:
--describe shows the leader, replicas and ISR of each partition; recent versions also show the Eligible Leader Replicas (Elr). Other useful options are --alter --partitions 6 (partitions can only be increased), --delete, --under-replicated-partitions and --unavailable-partitions. Production topics should use --replication-factor 3.
4. Changing Configuration with kafka-configs.sh
bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type topics \
--entity-name invoices --add-config retention.ms=86400000
bin/kafka-configs.sh --bootstrap-server localhost:9092 --describe --entity-type topics --entity-name invoices
Completed updating config for topic invoices.
Dynamic configs for topic invoices are:
retention.ms=86400000 sensitive=false synonyms={DYNAMIC_TOPIC_CONFIG:retention.ms=86400000}
Use --delete-config retention.ms to return to the default. The same tool changes dynamic broker configs (--entity-type brokers), client quotas (--entity-type users or clients) and SCRAM credentials.
5. Sending and Reading Messages
The console producer reads one record per line. With parse.key=true, the part before the separator becomes the key:
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic invoices \
--property parse.key=true --property key.separator=:
>INV-1:{"amount":120}
>INV-2:{"amount":75}
>INV-3:{"amount":310}
The console consumer prints records. --from-beginning applies only when the group has no committed offsets:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic invoices --group invoice-audit \
--from-beginning --property print.key=true --property print.partition=true --timeout-ms 8000
Partition:1 INV-3 {"amount":310}
Partition:0 INV-1 {"amount":120}
Partition:0 INV-2 {"amount":75}
Processed a total of 3 messages
Order is guaranteed only within a partition: INV-1 and INV-2 share partition 0 and appear in order, while INV-3 from partition 1 can appear before them. Without --timeout-ms or --max-messages, the consumer runs until you press Ctrl+C.
6. Consumer Groups and Lag
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group invoice-audit
invoice-audit
Consumer group 'invoice-audit' has no active members.
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
invoice-audit invoices 1 1 1 0 - - -
invoice-audit invoices 0 2 2 0 - - -
invoice-audit invoices 2 0 0 0 - - -
LAG is LOG-END-OFFSET - CURRENT-OFFSET, the number of records the group has not yet processed. While consumers are running, the last three columns show which member owns each partition. Add --members --verbose to list members and their assignments, or --state to see the group state.
6.1 Resetting offsets
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group invoice-audit \
--topic invoices --reset-offsets --to-earliest --dry-run
GROUP TOPIC PARTITION NEW-OFFSET
invoice-audit invoices 1 0
invoice-audit invoices 0 0
invoice-audit invoices 2 0
--dry-run only shows the plan. Replace it with --execute to apply it; the group must have no active members. More details are in Kafka Offset Management.
7. Partition End Offsets with kafka-get-offsets.sh
bin/kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic invoices
invoices:0:2
invoices:1:1
invoices:2:0
The format is topic:partition:offset. By default it prints the latest offset; --time -2 gives the earliest offset and --time <epoch-ms> the first offset at or after a timestamp.
8. Checking the KRaft Quorum
bin/kafka-metadata-quorum.sh --bootstrap-server localhost:9092 describe --status
ClusterId: D59JgZPbRrq3ROvNY99_2A
LeaderId: 0
LeaderEpoch: 1
HighWatermark: 215
MaxFollowerLag: 0
MaxFollowerLagTimeMs: 0
CurrentVoters: [{"id": 0, "directoryId": null, "endpoints": ["CONTROLLER://localhost:55775"]}]
CurrentObservers: []
LeaderId is the active controller. In a healthy multi-controller cluster, MaxFollowerLag stays close to zero. describe --replication shows the lag of every voter and observer. See Leader Election in Apache Kafka for how the controller is chosen.
9. Measuring Performance
bin/kafka-producer-perf-test.sh --topic invoices --num-records 10000 --record-size 200 \
--throughput -1 --producer-props bootstrap.servers=localhost:9092
10000 records sent, 60241.0 records/sec (11.49 MB/sec), 26.32 ms avg latency, 102.00 ms max latency, 30 ms 50th, 42 ms 95th, 44 ms 99th, 45 ms 99.9th.
These numbers come from a single-broker test cluster on a laptop and only show the format. Run perf tests against a cluster similar to production, with realistic record sizes, acks and compression; kafka-consumer-perf-test.sh measures the read side.
10. Other Useful Tools
| Tool | Purpose |
|---|---|
kafka-reassign-partitions.sh | Move partitions between brokers or change replica lists |
kafka-leader-election.sh | Trigger preferred or unclean leader elections |
kafka-log-dirs.sh | Show partition sizes per broker and log directory |
kafka-dump-log.sh | Print the contents of segment files for debugging |
kafka-delete-records.sh | Delete records before an offset |
kafka-acls.sh | Manage access control lists |
kafka-transactions.sh | List, describe and abort hanging transactions |
kafka-features.sh | Show and upgrade feature levels such as metadata.version |
kafka-share-groups.sh | Inspect share groups (KIP-932, Kafka 4.x) |
kafka-streams-application-reset.sh | Reset a Kafka Streams application for reprocessing |
Every tool prints its options with --help. Pass security settings with --command-config client.properties (or --producer.config and --consumer.config for the console tools).
11. Common Mistakes
- Using
--zookeeperoptions from old blog posts; they no longer exist in Kafka 4.x. - Resetting offsets with
--executewhile consumers run, which fails; stop the group first. - Reducing partitions with
--alter, which is not supported; create a new topic instead. - Treating console consumer order as topic order; order is per partition.
- Running admin commands without TLS/SASL settings on secured clusters and getting timeouts instead of clear errors.
12. Conclusion
Kafka's CLI tools cover the full life cycle of a cluster: kafka-storage to format it, kafka-topics and kafka-configs to manage topics, the console tools to test data flow, kafka-consumer-groups and kafka-get-offsets to follow progress, kafka-metadata-quorum to check KRaft, and the perf tools to measure capacity. Keep commands in scripts or runbooks, always try --dry-run where it exists, and use the Admin API when you need the same operations from code.
13. References
Kafka Tools and CLI Interview Questions and Answers
Common interview questions on Kafka Tools and CLI, from basic to scenario-based, with short answers you can explain in your own words.
Basic questions
Q. How do you find partitions that are at risk after a broker problem?
Answer: Use kafka-topics.sh --describe with a filter: --under-replicated-partitions lists partitions whose ISR is smaller than the replica list, --under-min-isr-partitions lists those that reject acks=all writes, and --unavailable-partitions lists partitions without an available leader. Run them after maintenance and before restarting the next broker.
Q. Why does kafka-console-consumer with --from-beginning sometimes show no old messages?
Answer: --from-beginning only takes effect when the consumer group has no committed offsets. If you reuse a --group that has already consumed the topic, it continues from its committed position. Use a new group name, omit --group, or reset the group's offsets.
Intermediate questions
Q. How do you increase the partition count of a topic, and what is the side effect?
Answer: kafka-topics.sh --alter --topic <t> --partitions <n> with a larger number. Existing data is not moved, but new records with a given key may now hash to a different partition, so per-key ordering across the change is lost. Plan partition counts up front for keyed topics.
Q. Which tool shows the active KRaft controller?
Answer: kafka-metadata-quorum.sh --bootstrap-server <broker> describe --status. LeaderId is the active controller, and CurrentVoters lists the controller quorum. describe --replication shows each voter's lag.
Tricky and scenario questions
Q. A transaction is stuck and blocks read_committed consumers. Which tool helps?
Answer: kafka-transactions.sh. Use find-hanging to locate transactions that are open on a partition without a coordinator state, describe to inspect a transactional ID, and abort to abort the hanging transaction so the last stable offset can move forward.