Skip to content

Running Kafka Locally

Running Kafka 4.x locally means starting a single broker in KRaft mode — no ZooKeeper, no external dependencies — then talking to it entirely through --bootstrap-server localhost:9092 to create a topic, produce records, and consume them.

The official apache/kafka image ships a KRaft-ready broker that formats its own storage and starts as a combined broker plus controller. One command gives you a working cluster:

Terminal window
# Start a single-node Kafka 4.x broker in KRaft mode, port 9092 exposed
docker run -d --name kafka -p 9092:9092 apache/kafka:latest

That is the whole cluster. There is nothing else to install — no ZooKeeper process exists in Kafka 4.x. The container comes with the CLI scripts baked in, so you can run admin commands inside it with docker exec, or install the Kafka tarball locally and point the scripts at localhost:9092.

The manual path: format storage, then start

Section titled “The manual path: format storage, then start”

If you run the downloaded tarball instead of Docker, KRaft needs a one-time storage format step. You generate a cluster UUID and write it into the log directory before the first start:

Terminal window
# 1. Generate a unique cluster ID
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
# 2. Format the log directory for KRaft using that ID and the KRaft config
bin/kafka-storage.sh format --standalone \
-t "$KAFKA_CLUSTER_ID" \
-c config/server.properties
# 3. Start the broker (it is also the controller in a single-node setup)
bin/kafka-server-start.sh config/server.properties

The random-uuid plus format pair is the KRaft bootstrap ritual — it replaces the old ZooKeeper handshake. You only do it once per data directory.

flowchart LR
  fmt["kafka-storage.sh format (KRaft)"] --> start["kafka-server-start.sh (broker + controller)"]
  start --> topic["kafka-topics.sh --create"]
  topic --> prod["kafka-console-producer.sh"]
  topic --> cons["kafka-console-consumer.sh"]
  prod -->|"records"| cons
Local KRaft workflow: format, start, then produce and consume

With the broker up, every command targets --bootstrap-server — the --zookeeper flag no longer exists. Create a topic first:

Terminal window
# Create a topic named "demo" with 1 partition (fine for local)
kafka-topics.sh --bootstrap-server localhost:9092 \
--create --topic demo --partitions 1 --replication-factor 1

Then open a producer in one terminal and type messages, one per line:

Terminal window
# Produce: each line you type becomes a record
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic demo

And a consumer in another terminal to read them back from the start:

Terminal window
# Consume: --from-beginning replays every record in the topic
kafka-console-consumer.sh --bootstrap-server localhost:9092 \
--topic demo --from-beginning
What do you need to install alongside Kafka 4.x to run it locally?
What is the purpose of `kafka-storage.sh format` in the manual startup flow?
Which flag do Kafka 4.x CLI tools use to address the cluster?
How do you replay every record already in a topic with the console consumer?