Kafka
This guide shows you how to work with Apache Kafka locally using Docker Compose and Python. This lab demonstrates how to produce and consume events on a topic with the confluent-kafka client, inspect them in Kafka UI, and operate the broker with the Kafka CLI tools.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in Container- Start Infrastructure: Launch the Kafka broker and Kafka UI containers:
Terminal window docker compose up -d - Run the setup script to install tools and dependencies:
Terminal window scripts/setup.sh
- Start Infrastructure: Launch the Kafka broker and Kafka UI containers:
-
Run the Example:
-
Run the full round trip. It publishes the events, consumes them back and exits:
Terminal window python main.py -
Split it across two terminals.
-ckeeps the consumer listening untilCTRL+C, and-ponly publishes:Terminal 1:
Terminal window python main.py -cTerminal 2:
Terminal window python main.py -p
- Produce an event by typing it in the console producer:
Terminal window scripts/kafka.sh kafka-console-producer.sh --topic user-events - Consume the events from the beginning of the topic:
Terminal window scripts/kafka.sh kafka-console-consumer.sh --topic user-events --from-beginning
- Install: Tools for Apache Kafka.
- Connect: The
localcluster is preconfigured in.vscode/settings.jsonfromKAFKA_BOOTSTRAP_SERVERS. - Open: Open
playgrounds/user_events.kafka. - Run: Click Produce record or Start consumer above each block.
-
How to debug
Section titled “How to debug”-
main.py:
- Open: Open
main.py. - Breakpoints: Set breakpoints in
publish_eventsorprint_event. - Run: Press
F5and select Python: Main, which runs the full round trip.
- Open: Open
-
Tests:
- Open: Open a test file (e.g.,
tests/components/test_event_consumer.py). - Breakpoints: Set breakpoints in the test code.
- Run: Use the VS Code Testing tab and click the Debug Test icon next to the test you want to debug.
- Open: Open a test file (e.g.,
How to test
Section titled “How to test”- All tests: Run the automated script:
Terminal window scripts/run_tests.sh - Individually: Use the VS Code Testing tab to run or debug specific test cases.
Validate results
Section titled “Validate results”- Open: Browse to the UI (port from
KAFKA_UI_PORTin your.env):Terminal window http://localhost:8080 - Verify: Navigate to local → Topics →
user-events→ Messages. - Monitor: Check Consumers to see the lag of the
user-events-consumergroup.
- List the topics:
Terminal window scripts/kafka.sh kafka-topics.sh --list - Read every event stored in the topic:
Terminal window scripts/kafka.sh kafka-console-consumer.sh --topic user-events --from-beginning - Verify the offsets and lag of the consumer group:
Terminal window scripts/kafka.sh kafka-consumer-groups.sh --describe --group user-events-consumer
- Install: Tools for Apache Kafka.
- Browse: Open the Kafka panel in the Activity Bar and expand the
localcluster. - Verify: Expand Topics →
user-eventsto inspect partitions and offsets. - Interactive: Use
playgrounds/user_events.kafkato produce and consume records from the editor.
Clean Up
Section titled “Clean Up”docker compose down -vTroubleshooting
Section titled “Troubleshooting”| Issue | Solution |
|---|---|
The producer fails with Connection refused. | The broker takes a few seconds to start. Check it is ready with docker compose logs kafka. |
| You publish events but no consumer prints them. | List the members of the group with scripts/kafka.sh kafka-consumer-groups.sh --describe --group user-events-consumer --members. No members: nothing is consuming, start a consumer with python main.py -c. More than one member: the topic has a single partition, so only one member is assigned to it and that one is receiving the events — stop the extra consumers. |
| Kafka UI shows no cluster. | The UI reaches the broker on the internal listener kafka:9093. Make sure both containers are up with docker compose ps. |