Running Kafka Locally
The idea in one sentence
Section titled “The idea in one sentence”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 fastest path: one Docker container
Section titled “The fastest path: one Docker container”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:
# Start a single-node Kafka 4.x broker in KRaft mode, port 9092 exposeddocker run -d --name kafka -p 9092:9092 apache/kafka:latestThat 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:
# 1. Generate a unique cluster IDKAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
# 2. Format the log directory for KRaft using that ID and the KRaft configbin/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.propertiesThe 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
Create a topic, produce, consume
Section titled “Create a topic, produce, consume”With the broker up, every command targets --bootstrap-server — the --zookeeper flag no longer exists. Create a topic first:
# 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 1Then open a producer in one terminal and type messages, one per line:
# Produce: each line you type becomes a recordkafka-console-producer.sh --bootstrap-server localhost:9092 --topic demoAnd a consumer in another terminal to read them back from the start:
# Consume: --from-beginning replays every record in the topickafka-console-consumer.sh --bootstrap-server localhost:9092 \ --topic demo --from-beginning