A hands-on project demonstrating Apache Kafka fundamentals with Python, featuring a producer-consumer architecture for processing order events.
This project provides a practical introduction to Apache Kafka by implementing a complete event-driven system for order processing. It showcases:
- Event Production: Publishing order events to Kafka topics with message batching
- Event Consumption: Subscribing to topics and processing messages in real-time
- Data Validation: Using Pydantic models for type-safe message serialization/deserialization
- Modern Kafka: Running Kafka in KRaft mode (without ZooKeeper)
- Clean Architecture: Modular design with separate models, Kafka utilities, and application logic
- Comprehensive Testing: Unit tests for producers, consumers, and data models
basic-kafka-hands-on/
├── kafka/ # Kafka producer and consumer wrappers
│ ├── producer.py
│ ├── producer_with_backpressure.py
│ └── consumer.py
├── models/ # Pydantic data models
│ └── order.py
├── utils/ # Utility functions
│ └── partitioning.py # Compound partition key strategies
├── examples/ # 🎓 Comprehensive examples
│ ├── README.md # Examples guide and index
│ ├── offset_management.py
│ ├── idempotent_processing.py
│ ├── race_condition_handling.py
│ ├── backpressure_example.py
│ └── compound_keys_example.py
├── tests/ # Unit tests (62 tests)
│ ├── conftest.py
│ ├── test_kafka_producer.py
│ ├── test_kafka_consumer.py
│ ├── test_backpressure.py
│ ├── test_partitioning.py
│ └── test_order.py
├── docs/ # Documentation
│ ├── PROJECT_STRUCTURE.md # Project organization guide
│ ├── OFFSETS.md # Offset management deep dive
│ ├── BACKPRESSURE.md # Back pressure guide
│ └── PARTITIONING.md # Partitioning strategies guide
├── order_producer.py # Simple producer (getting started)
├── order_consumer.py # Simple consumer (getting started)
├── docker-compose.yml # Kafka infrastructure
└── requirements.txt # Python dependencies
Key Technologies:
- Python 3.11+ - Core programming language
- Apache Kafka 7.8.6 - Distributed event streaming platform (KRaft mode)
- Pydantic 2.10.5 - Data validation and settings management
- confluent-kafka 2.12.2 - High-performance Kafka client library
- pytest 7.4.3 - Testing framework
- Docker - Containerized Kafka deployment
Follow these instructions to get the project up and running on your local machine.
-
Python 3.11 or higher
python --version
-
Docker and Docker Compose
docker --version docker compose version
-
pip (Python package installer)
pip --version
-
Clone the repository
git clone https://github.com/yourusername/basic-kafka-hands-on.git cd basic-kafka-hands-on -
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Python dependencies
pip install -r requirements.txt
-
Start Kafka with Docker Compose
docker compose up -d
-
Verify Kafka is running
docker ps
You should see the
kafkacontainer running on port 9092. -
Create the
orderstopic (optional - auto-created on first message)docker exec -it kafka kafka-topics --create \ --bootstrap-server localhost:9092 \ --topic orders \ --partitions 3 \ --replication-factor 1
The producer creates and sends order events to the orders topic:
python order_producer.pyExample Output:
✅ Delivered: {"order_id":"abc-123","customer_name":"John Doe","item":"MacBook Pro",...}
✅ Topic: orders, Partition: 0, Offset: 42
The consumer subscribes to the orders topic and processes incoming messages:
python order_consumer.pyExample Output:
🔵 Consumer started. Waiting for messages...
Press Ctrl+C to stop.
✅ Order received: abc-123
Customer: John Doe, Item: MacBook Pro, Qty: 1
Press Ctrl+C to stop the consumer gracefully.
The examples/ directory contains comprehensive, production-ready examples demonstrating advanced Kafka patterns:
# Browse available examples
ls examples/
# Read the examples guide
cat examples/README.md
# Run specific examples
python examples/offset_management.py 1 # Manual offset commit
python examples/idempotent_processing.py 1 # Handle duplicates
python examples/race_condition_handling.py 2 # Prevent race conditions
python examples/backpressure_example.py # Flow control
python examples/compound_keys_example.py # Partition optimizationAvailable Examples:
- 📍 Offset Management (5 examples) - Manual commit, auto-commit, batch commit, seeking, lag monitoring
- 🔄 Idempotent Processing (4 examples) - Unique constraints, deduplication, state tracking
- 🔐 Race Condition Handling (5 examples) - Locks, transactions, optimistic locking
- ⚡ Back Pressure (4 demos) - High-volume production, buffer management
- 🎯 Compound Keys (5 strategies) - Region-based, segment-based, hash-based partitioning
👉 See examples/README.md for complete guide and learning paths!
Execute the test suite to verify functionality:
# Run all tests
pytest tests/ -v
# Run specific test file
pytest tests/test_kafka_producer.py -v
# Run with coverage
pytest tests/ --cov=kafka --cov=models --cov=utils -vTest Coverage:
- 78 tests total (all passing ✅)
- Producer tests: 9
- Consumer tests: 11
- Back pressure tests: 19
- Partitioning tests: 22
- Examples tests: 16 ⭐ NEW
- Model tests: 1
List all topics:
docker exec -it kafka kafka-topics --list --bootstrap-server localhost:9092Describe a topic:
docker exec -it kafka kafka-topics --describe --topic orders --bootstrap-server localhost:9092Consume messages from CLI:
docker exec -it kafka kafka-console-consumer \
--bootstrap-server localhost:9092 \
--topic orders \
--from-beginningCheck consumer group status:
docker exec -it kafka kafka-consumer-groups \
--bootstrap-server localhost:9092 \
--describe \
--group order-consumer-
Stop the consumer/producer - Press
Ctrl+C -
Stop Kafka
docker compose down
-
Remove volumes (clean slate)
docker compose down -v
- ✅ Message batching and buffering
- ✅ Delivery callbacks and error handling
- ✅ Topic partitioning with compound keys
- ✅ Back pressure management
- ✅ JSON serialization with Pydantic
- ✅ Consumer groups
- ✅ Offset management (earliest/latest)
- ✅ Manual vs auto-commit
- ✅ Polling and message processing
- ✅ Graceful shutdown
- ✅ Idempotent processing
- ✅ Error handling for malformed messages
- ✅ KRaft mode (no ZooKeeper required)
- ✅ Topics and partitions
- ✅ Producers and consumers
- ✅ Message ordering within partitions
- ✅ Consumer group coordination
Complete guides and examples are organized in docs/ and examples/ directories.
| Guide | Topics | Read Time |
|---|---|---|
| Kafka Concepts | Partitioning basics, message ordering | 10-15 min |
| Understanding Offsets | Offset management, commit strategies, delivery semantics | 15-20 min |
| Back Pressure | Flow control, buffer management, high-volume production | 20-25 min |
| Advanced Partitioning | Compound keys, hotspot prevention, load distribution | 20-25 min |
| Project Structure | Project organization, navigation, learning paths | 10 min |
| Docs Index | Complete documentation index and reading guide | 5 min |
| Example | What You'll Learn | Demos |
|---|---|---|
| Offset Management | Manual commit, auto-commit, batch commit, seeking, lag monitoring | 5 |
| Idempotent Processing | Unique constraints, deduplication, state tracking | 4 |
| Race Conditions | Locks, transactions, optimistic locking, coordination | 5 |
| Back Pressure | High-volume production, buffer management | 4 |
| Compound Keys | Region-based, segment-based, hash-based partitioning | 5 |
| Examples Guide | Complete index with learning paths | - |
Total: 23+ interactive examples 🎯
🎯 Path 1: Get Started (30 minutes)
1. Run order_producer.py
2. Run order_consumer.py
3. Explore docker-compose.yml
📚 Path 2: Production Consumer (2-3 hours)
1. Read docs/OFFSETS.md
2. Run examples/offset_management.py
3. Run examples/idempotent_processing.py
4. Review tests/test_kafka_consumer.py
⚡ Path 3: High-Performance Producer (2-3 hours)
1. Read docs/BACKPRESSURE.md
2. Read docs/PARTITIONING.md
3. Run examples/backpressure_example.py
4. Run examples/compound_keys_example.py
🎓 Path 4: Master All Concepts (6-8 hours)
1. Read all docs in docs/
2. Run all examples in examples/
3. Review all tests in tests/
4. Study implementation in kafka/, models/, utils/
# Check Docker logs
docker logs kafka
# Restart with clean state
docker compose down -v
docker compose up -d# Verify topic has messages
docker exec -it kafka kafka-console-consumer \
--bootstrap-server localhost:9092 \
--topic orders \
--from-beginning \
--max-messages 5
# If empty, produce some messages
python order_producer.py# Ensure virtual environment is activated
source venv/bin/activate # or venv\Scripts\activate on Windows
# Reinstall dependencies
pip install -r requirements.txt# Run tests with verbose output
pytest tests/ -v --tb=short
# Check specific test file
pytest tests/test_kafka_producer.py -vContributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new features
- Update documentation
- Submit a pull request
- Apache Kafka Documentation
- Confluent Kafka Python
- Pydantic Documentation
- Kafka: The Definitive Guide
- Best Practices for Developing Apache Kafka Applications
License: MIT Last Updated: February 2026