Event Streaming with Kafka
Learn the log-based model behind Kafka: topics, partitions, offsets and consumer groups, and when a durable event stream beats a classic queue.
Intermediate · about 5 hours · difficulty 3/5
Learn first: Message Queues and Async Processing
Apache Kafka is a distributed, append-only log. Producers append events; consumers read at their own pace and track their own position. Unlike a classic queue, messages are not deleted when read, so many independent consumers can replay the same history.
Core concepts
- Topic: a named stream of events (orders, payments, location-updates).
- Partition: a topic is split into ordered partitions spread across brokers. Ordering is guaranteed only within a partition.
- Key: events with the same key (order_id, driver_id) go to the same partition, so they stay in order.
- Offset: a consumer's position in a partition. Consumers commit offsets to resume after restarts.
- Consumer group: partitions are divided among the group's consumers. Parallelism is capped by the partition count.
- Replication: each partition has a leader and followers; acks=all plus min.insync.replicas gives durability.
- Retention: keep events for days or forever; log compaction keeps the latest value per key.
When Kafka shines
- High-throughput event pipelines (clickstream, location pings, logs).
- Event-driven architecture: one OrderPlaced event feeds payments, notifications, analytics and search indexing.
- Change data capture with Debezium, streaming DB changes to caches and search.
- Stream processing with Kafka Streams or Flink for real-time aggregations.
Swiggy, Flipkart, Uber and Hotstar all run Kafka at large scale for exactly these uses.
Pitfalls
- Too few partitions, capping consumer parallelism; too many, adding overhead.
- Choosing a key with skew (one huge merchant) and creating a hot partition.
- Expecting global ordering across a topic.
- Treating Kafka as a task queue with per-message retries and delays; it can be done, but a queue is simpler.
You are ready to move on when you can design the topics, keys and consumer groups for an order pipeline and explain how a new analytics service could replay last week's events.
Key concepts
- Topics and partitions
- Keys and per-partition ordering
- Offsets and consumer groups
- Replication and acks
- Retention and log compaction
- Change data capture (Debezium)
- Stream processing (Kafka Streams, Flink)
Other options for this step
- RabbitMQ and Managed Queues (SQS, Pub/Sub): Use a classic broker with routing, per-message acks and delays, or a managed cloud queue, when you need task distribution rather than a replayable log.
Mini project
Run Kafka locally with Docker. Produce simulated order events keyed by order_id to a 6-partition topic, run two consumer groups (notifications and analytics), kill a consumer mid-stream and show the group rebalances without losing events.
Check yourself
You have 12 partitions and start 20 consumers in one group. How many are doing work, and why?
Free resources
- Apache Kafka documentation (Apache)
- Introduction to Kafka (Apache)
- Kafka design: the log at the core (Apache Kafka)