How Kafka Handles Millions of Events: Architecture Explained
Akshay explains how Apache Kafka keeps up when event counts explode by putting a durable log between producers and consumers. The video covers partitions, brokers, consumer groups, offsets, rebalancing, replication and the trade-offs.
Imagine an application producing millions of events such as orders, clicks, payments and sensor readings. If every producer waits for every downstream service, a traffic spike can turn one slow consumer into a system-wide failure. In this video Akshay explains how Apache Kafka changes that problem by placing a durable event log in the middle, so producers and consumers can scale independently.
The problem with tightly coupled systems
The video starts without Kafka. An API receives an order and immediately calls inventory, fraud detection, analytics, email and billing. At low traffic this looks fine. At high traffic each dependency adds latency and another point of failure. If fraud detection slows down, the API starts backing up. Producers and consumers are tightly coupled, so one spike travels through the whole chain.
A durable log in the middle
With Kafka in the middle, producers append events to a topic and can move on once Kafka acknowledges the write. Consumers read those events at their own pace, so a temporary consumer slowdown no longer has to stop the producer. Kafka is not just passing messages along. It retains records for a configured period, which means consumers can fall behind and catch up later.
Partitions, keys and ordering
The first big scaling idea is partitioning. A topic is split into multiple partitions, and each partition is its own ordered log. Records with the same key can be routed to the same partition, which preserves order for that key, while different partitions are handled in parallel. More partitions create more lanes for throughput, though they also add coordination and operational cost. In the example the event key is the user ID, which gives related events a stable partitioning rule and is useful when ordering per user matters.
Spreading the load across brokers
A producer does not choose a broker for every event. It connects to the cluster, learns the metadata and sends each record to the broker that currently leads its partition. The example spreads six partitions across three brokers: broker one leads partitions zero and three, broker two leads one and four, and broker three leads two and five. Network traffic, disk writes and reads are distributed across machines instead of concentrating on one server, and Kafka scales horizontally by adding brokers and balancing partition replicas across them.
Consumer groups and offsets
On the read side, Kafka uses consumer groups. Within one group a partition is assigned to at most one consumer at a time. With six partitions, up to six consumers in a group can process in parallel, and a seventh sits idle until the assignment changes. Partition count therefore sets the ceiling for parallelism inside a group.
Every record has an offset, its position inside the partition. After processing, a consumer commits the next offset it should read, and if the process restarts it resumes from that position. Because retention keeps older records, a team can also rewind and replay events on purpose. The video stresses that Kafka does not make every application exactly once; the delivery guarantee depends on how producers, consumers, commits and downstream writes are configured. In the code shown, the consumer joins a group, subscribes, pulls records, processes them and then commits, with auto commit disabled so the application controls when progress is recorded.
Rebalancing and replication
When a consumer is killed, group coordination detects it and the group rebalances. The failed consumer's partitions go to surviving members, which resume from committed offsets. A rebalance causes a temporary coordination pause, so applications try to avoid unnecessary churn.
Kafka also protects partition data with replication. One replica is the leader and other replicas copy it on different brokers. If the leader's broker fails, an eligible in-sync replica can become the new leader. Actual availability behavior still depends on settings such as replication factor, acknowledgements and in-sync replica requirements.
The trade-offs
The video closes with the costs. Too few partitions cap parallelism. Too many increase metadata, file handles, recovery work and rebalancing complexity. Slow consumers accumulate lag, hot keys can overload one partition, and Kafka still needs capacity planning for disk, network, retention, replication and downstream systems. The architecture scales because work is partitioned, not because limits disappear.
Key takeaways
- A durable event log between producers and consumers lets each side scale independently and stops one slow consumer from backing up the producer.
- Topics are split into partitions, and each partition is its own ordered log.
- Records with the same key can go to the same partition, preserving order for that key.
- Partitions and their leaders are spread across brokers, so reads, writes and network traffic are distributed.
- In a consumer group, partition count sets the ceiling for parallelism, and extra consumers sit idle.
- Committed offsets let consumers resume after a restart, and retention allows replay.
- Rebalancing and replica failover keep work going when a consumer or broker fails, with a brief pause during rebalances.
- Partition counts, lag, hot keys and capacity planning are the main trade-offs.
Try it yourself
Sketch a pipeline of your own, such as orders feeding inventory, fraud checks and billing. Choose an event key, decide how many partitions you would need and how many consumers each group could use, and note where a hot key or a rebalance could cause trouble. Then compare your notes with the trade-offs the video lists.
Chapters
- 0:00The bottleneck of tightly coupled producer-consumer APIs
- 0:25Decoupling systems with Kafka's durable event log
- 0:50Topic partitioning, event keys, and ordering guarantees
- 1:15Distributing disk reads and writes across Kafka brokers
- 1:40Scaling reads with consumer groups and offset management
- 2:05Automatic group rebalancing and partition replication failovers
- 2:30Key trade-offs: Partition counts, hot keys, and capacity planning
Full transcript(auto-generated, with timestamps)
The bottleneck of tightly coupled producer-consumer APIs
[0:00]Hi, I'm Akshay. Imagine an app producing millions of events, orders, clicks, payments, sensor readings. If every producer waits for every downstream service, traffic spikes can turn one slow consumer into a system-wide failure. Kafka changes the shape of that problem. It puts a durable event log in the middle, then lets producers and consumers scale independently. So, how does that architecture keep up when the event count explodes? Start without Kafka. The API receives an order and
Decoupling systems with Kafka's durable event log
[0:25]Immediately calls inventory, fraud detection, analytics, email, and billing. At low traffic, it looks fine. At high traffic, each dependency adds latency and another failure point. If fraud detection slows down, the API starts backing up. Producers and consumers are tightly coupled, and one spike propagates through the whole chain. Now, put Kafka in the middle. Producers append events to a topic and can move on after Kafka acknowledges the right. Consumers read those events at
Topic partitioning, event keys, and ordering guarantees
[0:50]Their own pace. A temporary consumer slow down no longer has to stop the producer. Kafka is not just passing messages through. It retains records for a configured period, which means consumers can fall behind and catch up later. The first big scaling idea is partitioning. A Kafka topic is split into multiple partitions. Each partition is its own ordered log. Records with the same key can be routed to the same partition, preserving order for that key, while different partitions can be
Distributing disk reads and writes across Kafka brokers
[1:16]Handled in parallel. More partitions create more lanes for throughput, although they also add coordination and operational cost. A producer does not need to choose a broker for every event. It connects to the cluster, learns the metadata, and sends each record to the broker that currently leads its partition. Here, the event key is the user ID. That gives related events a stable partitioning rule, which is useful when ordering per user matters.
Scaling reads with consumer groups and offset management
[1:40]Partitions are spread across Kafka brokers. Broker one might lead partitions zero and three, broker two partitions one and four, and broker three partitions two and five. Now, network traffic, disk writes, and reads are distributed across machines instead of concentrating on one server. Kafka scales horizontally by adding brokers and balancing partition replicas across them. On the read side, Kafka uses consumer groups. Within one group, a partition is assigned to at most one consumer at a
Automatic group rebalancing and partition replication failovers
[2:06]Time. If this topic has six partitions, up to six consumers in the same group can actively process those partitions in parallel. Add a seventh consumer and it sits idle until the assignment changes. That is why partition count sets the ceiling for parallelism inside a consumer group. How does a consumer know where it is? With offsets, every record has a position inside its partition. After processing, a consumer can commit the next offset it should read. If the
Key trade-offs: Partition counts, hot keys, and capacity planning
[2:31]Process restarts, it resumes from the committed position. Because retention keeps older records, a team can also intentionally rewind and replay events. The exact delivery guarantee depends on how producers, consumers, commits, and downstream rights are configured. Kafka does not magically make every application exactly once. In code, the consumer joins a group, subscribes to the topic, pulls records, processes them, then commits progress. Here, auto commit is disabled, so the application controls when progress is recorded. In production, you would also design idempotency, retries, and error handling around this loop, because failures can happen between processing a record and recording its offset. Now, kill a consumer. Kafka's group coordination detects that the member is gone and the group rebalances. The failed consumer's partitions are reassigned to surviving members, which resume
From committed offsets. A rebalance causes a temporary coordination pause, so modern Kafka applications try to keep processing predictable and avoid unnecessary churn. But, the work itself is not permanently tied to one machine. Kafka also protects partition data with replication. One replica is the leader, and other replicas copy it on different brokers. Producers and consumers normally talk to the leader. If that broker fails, an eligible in-sync replica can become the new leader. Replication improves availability and durability, but the actual loss in availability behavior still depends on settings such as replication factor, acknowledgements, and in-sync replica requirements. So, the millions of event story is not one magic switch. Producers batch and append efficiently. Topics split traffic across partitions. Partition leaders are distributed across brokers. Consumer groups divide partitions across
Workers. Offsets let workers resume and replay. Replicas give partitions failover options. Together, those mechanisms let the pipeline spread storage, network traffic, and processing across many machines. There are tradeoffs. Too few partitions cap parallelism. Too many partitions increase metadata, file handles, recovery work, and rebalancing complexity. Slow consumers accumulate lag. Hot keys can overload one partition. And Kafka still needs capacity planning for disk, network, retention, replication, and downstream systems. The architecture scales because work is partitioned, not because limits disappear. The mental model is simple. Kafka turns one overloaded pipeline into many ordered lanes. Partitions create the lanes. Brokers host them. Consumer groups divide the work. Offsets track progress. And replicas keep copies available for failover. That's how Kafka can be designed to handle event streams at very large scale.
More from Humanitarians AI Fellows
3:34Paged Attention Explained: How OS Paging Fixes GPU Memory Waste in LLMs
1:39Brutalist AI Video Production & Content Pipeline Progress | Akshay Chavan
2:14Deterministic vs Probabilistic AI: When Guessing is Dangerous
4:29Simpson's Paradox: Why Aggregate Metrics Can Mislead You | Ushasvi
1:46How Do You Know AI Is Right? Moving Beyond the Demo
3:15