Trying Kafka in Python.
How to install Kafka varies among operating systems and is not the focus of this project.
Instructions and scripts for Mac OS are provided out of convenience.
At the time of writing (Aug 2025), Homebrew installs Kafka 4.0.0 in Kraft mode.
$ brew install kafkaKafka scripts are installed in /opt/homebrew/bin:
% ls /opt/homebrew/bin/kafka-*
/opt/homebrew/bin/kafka-acls
/opt/homebrew/bin/kafka-broker-api-versions
/opt/homebrew/bin/kafka-client-metrics
[...]$ bin/kafka-init.sh
Creating Kafka storage directories
Generating Kafka cluster id
_gxeSOELQHuz7zqkTjeGwA
Initializing broker storage
Formatting metadata directory ./kafka-storage/broker with metadata.version 4.0-IV3.
Initializing controller storage
Formatting dynamic metadata voter directory ./kafka-storage/controller with metadata.version 4.0-IV3.Run this in a new terminal
$ bin/kafka-start-controller.sh
Reading kafka cluster id
Setting IPv4 as preferred network stack
Starting controller
[2025-08-31 21:52:38,710] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)Run this in a new terminal
$ bin/kafka-start-broker.sh
Reading kafka cluster id
Setting IPv4 as preferred network stack
Starting broker
[2025-08-31 21:53:47,165] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)Create a topic with 1 partition
$ bin/kafka-create-topic.sh hello.world.1 1Messages are distributed among consumers in the same consumer group.
- Spin up two consumers in the same consumer group
- Each consumer writes the received messages in a text file
- Each file doesn't receive all messages, but collectively they contain all sent messages.
Consumers 1 and 2 in group1
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.1 -g "group1" -b 1000 -o consumer-1.txt
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.1 -g "group1" -b 1000 -o consumer-2.txtProducer:
$ uv run src/hello_kafka_python/main.py -p loop -t hello.world.1 -m "one partition" --count 100000Verify
$ cat consumer-1.txt consumer-2.txt | sort -k4 -n | wc -lResult: 100000
All consumer groups get all the messages.
- Spin up two consumer in different consumer groups
- Each consumer writes the received messages in a text file
- Each file contains all messages
Consumer 1 group1
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.1 -g "group1" -b 1000 -o consumer-1-g1.txtConsumer 1 group2
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.1 -g "group2" -b 1000 -o consumer-1-g2.txtVerify
$ cat consumer-1-g1.txt consumer-1-g2.txt| sort -k4 -n | wc -lResult: 200000
The number of partitions affects the data distribution within the Kafka cluster, but the single group / multi group semantics are the same.
Create a topic with 2 partitions
$ bin/kafka-create-topic.sh hello.world.2 2Consumers 1 and 2 in group1
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.2 -g "group1" -b 1000 -o consumer-1.txt
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.2 -g "group1" -b 1000 -o consumer-2.txtProducer:
$ uv run src/hello_kafka_python/main.py -p loop -t hello.world.2 -m "two partitions" --count 100000Verify
$ cat consumer-1.txt consumer-2.txt | sort -k4 -n | wc -lResult: 100000
Consumer 1 group1
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.2 -g "group1" -b 1000 -o consumer-1-g1.txtConsumer 1 group2
$ uv run src/hello_kafka_python/main.py -c commit -t hello.world.2 -g "group2" -b 1000 -o consumer-1-g2.txtVerify
$ cat consumer-1-g1.txt consumer-1-g2.txt | sort -k4 -n | wc -lResult: 200000
$ /opt/homebrew/bin/kafka-topics --bootstrap-server localhost:9092 --create --topic <topic_name> --partitions <num>$ /opt/homebrew/bin/kafka-topics --bootstrap-server localhost:9092 --delete --topic <topic_name>$ /opt/homebrew/bin/kafka-topics --bootstrap-server localhost:9092 --describe --topic <topic_name>$ /opt/homebrew/bin/kafka-topics --bootstrap-server localhost:9092 --list