This project demonstrates the challenges of using traditional databases for real-time event streaming and how Apache Kafka solves those issues through practical, runnable examples.
- Understand why traditional databases struggle with high-frequency event streaming
- Learn how Apache Kafka excels at real-time event processing
- See practical performance differences through metrics and logs
- Explore event-driven architecture patterns
- Gain hands-on experience with Kafka producers and consumers
- Node.js (v16 or higher)
- Docker Desktop (for Kafka)
- Command Prompt (Windows)
# Clone or download this project
cd kafka-vs-database-demo
# Install dependencies and build
npm run setup
# Start Kafka infrastructure
npm run start-kafkanpm run demoThis launches an interactive menu where you can:
- Start Kafka infrastructure
- Run the database demo (shows limitations)
- Run the Kafka producer demo
- Run the Kafka consumer demo
- View project structure
This demo shows the challenges of using traditional databases for high-frequency event ingestion:
# Run with TypeScript directly (development)
npm run dev:db
# Or run compiled JavaScript
npm run start:db-demoWhat you'll observe:
- Slow write performance as event rate increases
- Database connection overhead for each write
- No built-in support for real-time processing
- Difficulty in handling backpressure
This demo shows how Kafka handles high-throughput event streaming efficiently:
# Start the Kafka producer (in one terminal)
npm run dev:kafka-producer
# Start the Kafka consumer (in another terminal)
npm run dev:kafka-consumerWhat you'll observe:
- High-throughput event production
- Asynchronous, decoupled processing
- Built-in durability and replay capabilities
- Easy horizontal scaling
- Performance Bottlenecks: Each event requires a separate database write operation
- Blocking Operations: Synchronous writes slow down the producer
- No Native Streaming: Databases aren't designed for continuous data streams
- Tight Coupling: Producer and consumer are tightly coupled through the database
- Limited Scalability: Difficult to scale horizontally for high event rates
- High Throughput: Designed for millions of events per second
- Asynchronous Processing: Producers and consumers operate independently
- Durability: Events are persisted and can be replayed
- Scalability: Easy horizontal scaling with partitions
- Real-time Processing: Built for streaming workloads
- Fault Tolerance: Replication and fault recovery built-in
The demo simulates realistic events:
- User Activity: clicks, page views, scrolls
- Financial Transactions: payments, transfers, deposits
- System Events: logins, logouts, errors
Each event includes:
- Unique event ID
- Timestamp
- User/session ID
- Event type and metadata
- Realistic data values
Run both demos and observe:
- Database Demo: Watch latency increase with event rate
- Kafka Demo: Consistent performance even at high rates
The console logs will show timing metrics and throughput statistics to highlight the performance differences.
The included docker-compose.yml sets up:
- Zookeeper: Kafka coordination service
- Kafka Broker: Single Kafka instance for development
- Kafka UI (optional): Web interface for monitoring
After running this demo, students should understand:
- When to use traditional databases vs. streaming platforms
- How event-driven architectures scale
- The benefits of decoupled producer-consumer patterns
- Why Kafka is the industry standard for event streaming
- Kafka connection errors: Ensure Docker containers are running
- Port conflicts: Check that ports 9092, 2181 are available
- Memory issues: Kafka may need more memory in Docker settings