Kafka Tutorial for Beginners: Run Your First KRaft Cluster

This Kafka tutorial for beginners walks you through Apache Kafka the way it works today: no ZooKeeper, one small KRaft cluster on your own machine, and real commands you can run in about half an hour. You will create a topic, send and read messages, watch a consumer group track its position, and see where a Java producer fits in.

A lot of Kafka material online still starts with “first, install ZooKeeper”, which no longer applies to Kafka 4.x. Everything here was checked against the official Apache Kafka 4.4 documentation. If you later want guided, hands-on practice with an instructor, our live Apache Kafka training goes much deeper into producers, Connect, Streams and operations.

Key takeaways

  • Kafka stores streams of events in topics. Topics are split into partitions, and every record in a partition has an offset.
  • Since Kafka 4.0 (March 2025) ZooKeeper is gone. Kafka runs only in KRaft mode, and brokers need Java 17 or newer.
  • A single-node cluster takes three commands: generate a cluster ID, format storage, start the server. Docker users can run the official apache/kafka image instead.
  • Consumers in the same group share a topic’s partitions; each partition is read by one consumer in that group at a time.
  • Producers default to acks=all with idempotence enabled, so the safe settings are already on for new projects.

What is Apache Kafka?

Apache Kafka is an open-source event streaming platform, released under the Apache License 2.0. Applications called producers write events (an order placed, a sensor reading, a page view) to Kafka, and other applications called consumers read and process those events, either right away or later. Kafka keeps the events durably on disk for as long as you configure, so a consumer that was offline can catch up, and a new application can replay history.

Advertisement

The official documentation lists use cases such as messaging, website activity tracking, metrics, log aggregation, stream processing, event sourcing and acting as a commit log for distributed systems. In practice, US and UK job ads mention Kafka mostly in two contexts: data engineering pipelines and event-driven microservices.

Kafka core concepts every beginner needs

Before running anything, it helps to know the handful of terms you will see in every command and error message.

Concept What it means Why it matters
Broker A Kafka server that stores data and serves clients. A cluster is several brokers sharing the load.
Topic A named stream of events, such as orders. Producers write to topics; consumers subscribe to them.
Partition A topic is split into partitions spread across brokers. Partitions are the unit of parallelism. Order is guaranteed only within a partition.
Offset The position of a record inside a partition. A consumer’s progress is simply the offset of the next record it will read.
Replication factor How many copies of each partition exist, leader included. A common production setting is 3, so data survives a broker failure.
ISR The in-sync replicas that are caught up with the leader. With acks=all, a write is acknowledged once all current in-sync replicas have it.
Consumer group A set of consumers that share the work of reading a topic. Add consumers (up to the partition count) to process faster.
Retention and compaction Rules for deleting old data by time or size, or keeping the latest value per key. Kafka is not a queue that empties; data stays until retention removes it.

What changed in Kafka 4, and why old tutorials confuse beginners

Kafka moved fast between 2025 and 2026. If a guide you find disagrees with this one, check which version it was written for. The changes that trip up beginners most:

  • ZooKeeper is removed. The upgrade notes for 4.0 say Kafka “only supports KRaft mode”. Kafka now manages its own metadata with a built-in Raft quorum of controllers, and older clusters must migrate to KRaft before they can upgrade.
  • The --zookeeper flag is gone from the tools. Every command uses --bootstrap-server instead.
  • Java requirements went up. Brokers, Connect and the tools need Java 17 or newer; the clients and Kafka Streams can still run on Java 11. Kafka 4.4 fully supports Java 17, 21 and 25.
  • Console tool options were renamed in 4.2. For example --property became --reader-property on the console producer and --formatter-property on the console consumer. The old names still work but are deprecated and scheduled for removal in Kafka 5.0.
  • New consumer group protocol. The rebalance protocol from KIP-848 became generally available in 4.0. Clients still default to the classic protocol, and you opt in with group.protocol=consumer.
  • Queues for Kafka. Share groups (KIP-932) add queue-style consumption, where consumers in a share group can work through records from the same partitions together. The feature was a preview in 4.1 and became production-ready in 4.2.

At the time of writing, the latest release is Kafka 4.4.0 (October 2026). The commands below use it; swap in a newer version number from the official downloads page if one is available.

Step 1: Install Kafka 4.4

You have two easy options. Pick one.

Option A: the binary download

Install Java 17 or newer, then download the Kafka 4.4.0 binary (Scala 2.13 build) from the Apache Kafka downloads page and extract it:

java -version
tar -xzf kafka_2.13-4.4.0.tgz
cd kafka_2.13-4.4.0

The scripts in bin/ are shell scripts for Linux and macOS. On Windows, WSL2 or Docker is the simplest way to follow along.

Option B: Docker

If you already use Docker, the official image starts a single-node KRaft broker on port 9092 with no extra configuration:

docker pull apache/kafka:4.4.0
docker run -p 9092:9092 apache/kafka:4.4.0

A GraalVM-based apache/kafka-native image is also published. With Docker you can still run the command-line tools shown below from a downloaded copy of Kafka, or from inside the container.

Step 2: Format storage and start a KRaft broker

With the binary download, Kafka needs a cluster ID and formatted log directories before the first start. The quickstart does this in two commands, then starts the server:

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

The --standalone flag is the quickstart’s option for a single-node setup, which is exactly what you want on a laptop. The bundled config/server.properties runs one process as both broker and controller (combined mode). Production clusters normally run separate controller nodes, but combined mode keeps learning simple. Leave this terminal open; the broker logs here.

Step 3: Create your first topic

Open a second terminal in the same folder. Create a topic called orders with three partitions so you can see partitioning in action later. On a one-broker cluster the replication factor has to be 1:

bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders --partitions 3 --replication-factor 1
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders

The describe output lists each partition with its leader, replicas and ISR. On your laptop all three partitions live on node 1. On a real cluster you would see leaders spread across brokers, which is how Kafka shares load.

A tip worth remembering: brokers allow automatic topic creation by default, with one partition and a replication factor of 1. That is convenient in development and a common source of surprises in production, so many teams create topics explicitly and switch auto-creation off.

Step 4: Produce and consume messages from the console

Start the console producer and type a few lines. Each line becomes one event:

bin/kafka-console-producer.sh --topic orders --bootstrap-server localhost:9092
> order 1001 placed
> order 1002 placed
> order 1001 shipped

Press Ctrl+C to stop. Now read everything from the start of the topic in another terminal:

bin/kafka-console-consumer.sh --topic orders --from-beginning --bootstrap-server localhost:9092

Two things are worth noticing. First, the messages are still there after the producer stopped, because Kafka stores them until retention removes them (seven days by default on the broker). Second, the order you see across a multi-partition topic may not match the order you typed. Records without a key are spread over partitions, and Kafka only guarantees order inside a single partition. If all events for one order must stay in sequence, give them the same key (for example the order ID) so they land in the same partition.

Without --from-beginning, a brand new consumer starts at the end of the log and only sees new records. That matches the consumer default auto.offset.reset=latest, and it explains the classic beginner question “why does my consumer see nothing?”.

Advertisement

Step 5: Write a small Java producer and consumer

The console tools are great for learning, but real systems use client libraries. The official Java client is the org.apache.kafka:kafka-clients artifact (version 4.4.0 to match the broker). This producer, based on the example in the official KafkaProducer Javadoc, sends ten keyed order events:

import java.util.Properties;
import org.apache.kafka.clients.producer.*;

public class OrderProducer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        try (Producer<String, String> producer = new KafkaProducer<>(props)) {
            for (int i = 1; i <= 10; i++) {
                String orderId = "order-" + (1000 + i);
                producer.send(new ProducerRecord<>("orders", orderId, orderId + " placed"));
            }
        }
    }
}

You do not need to set acks or enable.idempotence here. In current Kafka, acks defaults to all and idempotence is on by default when no conflicting settings are present, so retries will not create duplicate records in the log. Closing the producer (the try-with-resources block does it) flushes anything still buffered.

The matching consumer joins a group called order-service and polls in a loop, again following the pattern in the official KafkaConsumer Javadoc:

import java.time.Duration;
import java.util.List;
import java.util.Properties;
import org.apache.kafka.clients.consumer.*;

public class OrderConsumer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("bootstrap.servers", "localhost:9092");
        props.setProperty("group.id", "order-service");
        props.setProperty("auto.offset.reset", "earliest");
        props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
            consumer.subscribe(List.of("orders"));
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(500));
                for (ConsumerRecord<String, String> r : records) {
                    System.out.printf("partition=%d offset=%d key=%s value=%s%n",
                        r.partition(), r.offset(), r.key(), r.value());
                }
            }
        }
    }
}

Setting auto.offset.reset to earliest means a group with no saved position starts from the oldest data instead of the newest. Offsets are committed automatically by default, so if you stop the consumer and start it again, it continues where the group left off rather than re-reading everything.

Step 6: Watch a consumer group share the work

Run two copies of OrderConsumer at the same time. Because both use group.id=order-service, Kafka assigns the three partitions between them: one consumer gets two partitions and the other gets one. Each partition is read by exactly one consumer in the group at any given moment. Stop one copy and, after a short rebalance, the survivor takes over all three partitions.

To see what the group is doing, use the consumer groups tool:

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group order-service

The describe output shows, per partition, the current offset, the log end offset and the lag between them. Lag is the number of records the group has not processed yet, and it is the first metric most teams watch in production. Start a third copy and each consumer gets one partition, but a fourth copy would sit idle, because a group cannot use more active consumers than the topic has partitions (share groups are the exception, which is exactly why Queues for Kafka was added).

Where Kafka Connect and Kafka Streams fit

Kafka ships with five APIs: Admin, Producer, Consumer, Streams and Connect. Once producers and consumers make sense, the other two are the natural next steps.

  • Kafka Connect runs reusable connectors that copy data between Kafka and external systems (databases, files, cloud storage) without custom code. The official quickstart includes a simple file source and file sink you can try.
  • Kafka Streams is a Java library for stream processing: filtering, joining and aggregating events as they arrive, with exactly-once processing support. Kafka 4.4 added transactional state stores (KIP-892) to Streams.

Many pipelines combine both: Connect brings data in, Streams or another engine transforms it, and Connect writes the results to a warehouse or lakehouse. If you work with platforms like Databricks, our Databricks interview questions guide shows where streaming ingestion appears in data engineering interviews.

Common beginner mistakes with Kafka

  • Following a ZooKeeper guide on Kafka 4.x. Commands with --zookeeper or a zookeeper-server-start.sh step will not work. Use the KRaft steps above.
  • Expecting global ordering. Order holds within one partition. Use a meaningful key when related events must stay in sequence.
  • Too few partitions. Partition count caps how many consumers in a group can work in parallel. Plan it when you create the topic.
  • Sending large payloads. Kafka is built for streams of modest-sized events. Store big files elsewhere and send a reference.
  • Ignoring lag. A consumer that silently falls behind is the most common production problem. Check lag from day one.
  • Forgetting to clean up. When you are done experimenting, stop the processes and delete the local data, as the quickstart suggests: rm -rf /tmp/kafka-logs /tmp/kraft-combined-logs.

What to learn after this Kafka tutorial for beginners

After this tutorial, a sensible order is: topic and key design, producer settings (batching with linger.ms, which defaults to 5 ms since Kafka 4.0, and batch.size), consumer offset handling, delivery guarantees and transactions, then Connect, Streams and finally operations such as security, monitoring and upgrades. Running Kafka on Kubernetes is common in larger companies, so our Kubernetes interview questions article is useful background if you are heading toward platform roles. For orchestrating batch jobs next to streaming, the Apache Airflow course covers the scheduling side.

Browse all our live online IT courses to see how Kafka fits alongside cloud, data engineering and DevOps skills.

Learn Apache Kafka with live, instructor-led training

Our live online Apache Kafka course covers KRaft clusters, Java producers and consumers, delivery guarantees, Kafka Connect, Kafka Streams and operations, with hands-on labs in every module. You also get guidance on the Confluent CCDAK and CCAAK certifications.

Enroll Now Request a Free Demo

Frequently asked questions

Does Kafka still need ZooKeeper?

No. Kafka 4.0, released on March 18, 2025, removed ZooKeeper mode entirely, and every later release runs only in KRaft mode. If you manage an older ZooKeeper-based cluster, you must migrate it to KRaft on a 3.x release before upgrading to 4.x.

Is Kafka hard to learn?

The basics are approachable: you can run a broker and pass messages within an hour, as this tutorial shows. The learning curve gets steeper with partition design, delivery guarantees, rebalancing and operations. Practising each concept hands-on, rather than only reading about it, makes the biggest difference.

What is Kafka used for?

Common uses include real-time data pipelines, event-driven microservices, website activity tracking, metrics and log aggregation, stream processing and event sourcing. Companies use it wherever many systems need to react to the same events reliably.

Is Apache Kafka free?

Yes. Apache Kafka is open source under the Apache License 2.0, so you can download and run it at no cost. Vendors such as Confluent also sell commercial platforms and managed cloud services built around it.

What language should I use with Kafka?

The official client and Kafka Streams are Java libraries, so Java is the natural starting point and the language of most official examples. Clients exist for other languages too, so Python, Go or .NET developers can use Kafka from their own stack.

Sources

  • Apache Kafka, Quickstart (kafka.apache.org/quickstart)
  • Apache Kafka 4.4 documentation: Introduction, Use cases, Design, Upgrading, Compatibility, Java version, Basic operations
  • Apache Kafka 4.4 configuration reference: producer, consumer and broker configs
  • Apache Kafka 4.4 Javadoc: KafkaProducer and KafkaConsumer
  • Apache Kafka downloads page and release announcements (4.0.0 to 4.4.0)
  • Apache Kafka LICENSE file on GitHub (Apache License 2.0)
Advertisement

About Tutorials Mania Editorial Team

The Tutorials Mania Editorial Team writes and maintains the site's technology tutorials, interview guides and coverage of AI tools and platforms. Every article is researched against official documentation, product announcements and primary sources, written to explain complex technology in plain language, and updated when products change.
View all posts by Tutorials Mania Editorial Team →