Lab 1 of 8 · Kafka basics: brokers, topics and partitions
Start Kafka and send your first events
Create a lab server, run a Kafka broker on it, and produce and consume keyed events across three partitions.
- Time
- 60 minutes
- Cost
- Free. Everything runs on your own computer.
What you learn
Kafka stores events in topics, split into partitions and kept on brokers. You start a real Kafka broker on a free lab server, create a topic, and see how keys decide which partition an event lands in.
- What brokers, topics, partitions and offsets are
- Running Kafka in KRaft mode (no ZooKeeper) with Docker
- Producing and consuming with the command-line tools
- Why events with the same key stay in order
In the AI era: AI assistants explain Kafka concepts well, but they often mix up older ZooKeeper-era commands with today’s KRaft setup. Check every command against the Kafka documentation for your version.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
- A free GitHub accountOfficial sign-up ↗
- Multipass (a free Ubuntu server on your computer)Official install page ↗
brew install --cask multipassCheck it worked:
multipass version
Before you start
Your computer needs about 15 GB of free disk space and 8 GB of memory (the lab server uses 4 GB).
Step by step
1 Create your lab server
This starts an Ubuntu 24.04 server called
kafka-labon your computer. Kafka, Python and everything else in this roadmap run there.multipass launch 24.04 --name kafka-lab --cpus 2 --memory 4G --disk 15G multipass listYou should see:kafka-labisRunningwith an IPv4 address.2 Open a shell on it
From now on, steps labelled "Run this on your lab server" run in this shell. Type
exitto leave it.multipass shell kafka-labYou should see:Your prompt changes to
ubuntu@kafka-lab:~$.3 Install Docker, Python and the GitHub CLI
Adding yourself to the
dockergroup lets you run Docker withoutsudo. When it finishes, typeexit, then runmultipass shell kafka-labagain so the group change applies.sudo apt-get update sudo apt-get install -y docker.io docker-compose-v2 python3-venv gh jq sudo usermod -aG docker "$USER"You should see:The packages install without errors.
4 Start a Kafka broker
The official
apache/kafkaimage starts a single-node cluster in KRaft mode, where Kafka manages its own metadata. It listens onlocalhost:9092on your lab server. Thekafkashell function saves typing:kafka topics ...runskafka-topics.shinside the container.docker run -d --name kafka --restart unless-stopped -p 9092:9092 apache/kafka:4.1.0 echo 'kafka() { docker exec -i kafka "/opt/kafka/bin/kafka-$1.sh" --bootstrap-server localhost:9092 "${@:2}"; }' >> ~/.bashrc source ~/.bashrc sleep 15 kafka topics --list && echo "Kafka is up"You should see:Kafka is up(the topic list is empty for now).5 Create a topic with three partitions
A partition is an ordered log. More partitions let more consumers read in parallel.
kafka topics --create --topic first-orders --partitions 3 kafka topics --describe --topic first-ordersYou should see:PartitionCount: 3, and one line per partition withLeader: 1(your only broker).6 Produce keyed events
Each line is
key:value. The key is the customer, so all of one customer’s orders go to the same partition and stay in order.printf 'alice:book\nbob:pen\ncarol:lamp\ndave:mug\nalice:desk\nerin:chair\n' | kafka console-producer --topic first-orders --property parse.key=true --property key.separator=:You should see:No output means the events were written.
7 Read them back
The consumer prints each event’s partition and key. Notice that both of alice’s orders share a partition, while other customers are spread across partitions 0, 1 and 2.
kafka console-consumer --topic first-orders --from-beginning --property print.partition=true --property print.key=true --timeout-ms 5000You should see:Six lines such as
Partition:0 alice book. The command stops by itself after 5 seconds with no new events.8 Connect GitHub and create your repository
Use your own name and GitHub email.
gh auth loginshows a one-time code and a web address: open the address in the browser on your computer and enter the code.git config --global user.name "Your Name" git config --global user.email "you@example.com" gh auth login --hostname github.com --git-protocol https --web gh auth setup-git gh repo create kafka-streaming-lab --public --add-readme --clone cd kafka-streaming-labYou should see:A new
kafka-streaming-labfolder that you are now inside.9 Write your notes
Create it with
mkdir -p notes && nano notes/01-basics.md(save with Ctrl+O, exit with Ctrl+X).# Kafka basics - Broker: ... - Topic: ... - Partition: ... - Offset: ... - Which partition each customer landed in: ... - Why one customer's orders stay in order: ...10 Commit, push and stop the server
Type
exitafter pushing, then stop the server from your computer. Kafka starts again with it next time.git add notes git commit -m "Add Kafka basics notes" git pushYou should see:The commit appears on GitHub. Then run
multipass stop kafka-labon your computer.
You are done when
A Kafka broker runs on your lab server.
first-ordershas three partitions and six events.Your notes explain why keyed events stay in order.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.