⚡ Streaming & Kafka · Chapter 3
Events and the Log — the Kafka mental model
Kafka is not a queue. It is an append-only log that many readers can read at their own speed.
Study this lesson free in the app →
The basics
An event is a record of something that happened — "rider R42 requested a cab at 18:02 in Koramangala" — and a log is an append-only list of events kept in the order they arrived. Kafka is a distributed log.
The problem it solves: a cab app has one stream of ride events and many teams that need it — billing, fraud, surge pricing, the data lake. Wire each team directly to the app and you get N×M integrations, and one slow team backs up the app. A traditional queue doesn't fix it either: once one reader takes a message, it's gone for everyone else.
The mental model: a newspaper archive. New editions are added at the end; old ones are never edited. Any number of readers can visit, each with their own bookmark. A reader who was on leave for a week simply resumes from their bookmark — and a new reader can start from the first edition still on the shelf.
Words you must know 5
- Topic
- a named stream of events, like
ride_events. - Partition
- one ordered, append-only slice of a topic; order is guaranteed only within it.
- Offset
- an event's position in a partition; each reader's bookmark.
- Consumer group
- a team of readers splitting the partitions; different groups read independently.
- Retention
- how long events are kept (e.g. 7 days), regardless of whether anyone read them.
Explained
A queue loses a message once it is consumed.
A log keeps it, and every reader remembers their own position.
That one difference is why streaming platforms can replay yesterday, feed six teams from one topic, and survive a broken consumer.
The book series and the reading clubs
A topic is a book series — say, ride_events.
Each partition is one volume of the series. Volumes can be read in parallel, but within a volume the pages are strictly in order.
A producer writes new pages at the end. Nobody edits earlier pages.
A consumer group is a reading club. The club splits volumes among its members so it finishes faster — each volume read by exactly one member. A different club can read the whole series independently, at its own pace, without disturbing the first.
Every reader keeps a bookmark — the offset. Lose your bookmark and you either re-read (duplicates) or skip ahead (data loss).
The moving parts
- Event — an immutable fact that happened, with an id and a time
- Topic — a named stream of events
- Partition — an ordered, append-only sequence; the unit of parallelism and the unit of ordering
- Offset — the position of an event inside a partition
- Producer — writes; picks the partition via a key
- Consumer group — a set of consumers sharing the partitions of a topic; each partition goes to exactly one member
- Retention — how long events stay (time or size based), independent of whether they were read
- Broker — a server holding partitions; partitions are replicated across brokers for durability
Ordering guarantee: within a partition, never across a topic. Remember that sentence exactly.
One topic, two clubs
topic: ride_events
P0 ──▶ e1 e2 e3 e4 ...
P1 ──▶ e5 e6 e7 ...
P2 ──▶ e8 e9 ...
group: billing group: analytics
c1 → P0, P1 c1 → P0
c2 → P2 c2 → P1, P2
(offset 402) (offset 87 — lagging, and that's fine)Add a third consumer to billing and Kafka rebalances partitions across the three. Add a fourth with only three partitions and one consumer sits idle — partitions cap your parallelism.
Why high-volume platforms put Kafka in the middle
Without a log, every producer must know every consumer: N×M integrations, and a slow consumer backs up the app.
With a log, producers write once and walk away. Consumers are decoupled in time — this buys you:
- Buffering — a traffic spike or a warehouse outage does not reach the app
- Replay — deploy a fixed consumer and re-read from an old offset
- Fan-out — fraud, pricing, ops dashboards and the lake all read the same events
- Back-pressure isolation — a slow consumer only grows its own lag
The number you watch in production is consumer lag: how far behind the newest offset a group is. Rising lag is the earliest warning that something downstream is dying.
Topic = series. Partition = volume. Offset = bookmark. Consumer group = reading club.
Order is guaranteed inside a volume — never across the series.
Gotchas that fail interviews
- Saying "Kafka guarantees ordering" — only per partition
- Adding consumers beyond the partition count and expecting more throughput
- Assuming a consumed message is deleted — retention decides that, not consumption
- Committing offsets before the work succeeds — a crash then silently loses events
- Ignoring rebalance storms — every join/leave pauses consumption briefly
The classic question
"How do you increase throughput of a Kafka consumer?"
Layered answer:
- Add consumers up to the partition count
- Beyond that, increase partitions (and accept the re-keying cost)
- Batch and parallelise the downstream write — usually the real bottleneck is the sink, not Kafka
- Check whether processing is synchronous per message when it could be async or bulk
Then add the honest caveat: "more partitions means more open file handles, more rebalance cost, and ordering only within each partition — so I'd confirm the ordering requirement before scaling that way."
Words to know
| Event | An immutable record that something happened, at a point in time. |
|---|---|
| Topic | A named, durable stream of events. |
| Partition | An ordered append-only shard of a topic; the unit of parallelism and ordering. |
| Offset | A consumer's position within a partition. |
| Consumer group | Consumers cooperating to read a topic; each partition assigned to exactly one member. |
| Consumer lag | Newest offset minus committed offset — how far behind a group is. |
| Retention | How long events are kept, regardless of whether anyone read them. |
| Rebalance | Reassignment of partitions when group membership changes. |
In two minutes
- An event is an immutable fact; a log appends events in order and never edits them.
- Kafka is a log, not a queue: reading doesn't delete — retention (time or size) does.
- Topics split into partitions; ordering is guaranteed within a partition, never across the topic.
- Each consumer group keeps its own offsets, so billing, fraud and the lake read the same events independently.
- Producers write once and are decoupled in time from consumers: buffering, replay and fan-out come free.
- Within a group, each partition goes to one consumer; partitions cap parallelism.
- Consumer lag is the production health signal; commit offsets after the work, not before.
Test yourself
1. A topic has 3 partitions. You run 5 consumers in one group. What happens?
- Throughput increases 5×
- 3 consumers get a partition each, 2 stay idle
- Each consumer reads all partitions
- Kafka rejects the extra consumers
Show the answer
3 consumers get a partition each, 2 stay idle — A partition is assigned to exactly one member of a group. Partition count is the hard ceiling on group parallelism.
2. Kafka guarantees ordering…
- across the whole topic
- within a partition
- within a consumer group
- only with a single broker
Show the answer
within a partition — Per-partition ordering only. If two events must be ordered relative to each other, they must share a partition key.
3. Your billing consumer crashed 2 hours ago. What did the analytics consumer group experience?
- It also stopped
- Nothing — offsets are per group, so it kept reading
- It received billing's messages too
- Events were dropped for both
Show the answer
Nothing — offsets are per group, so it kept reading — Groups track offsets independently. Fan-out with time-decoupling is precisely why a log beats point-to-point integrations.
4. Committing the offset BEFORE processing succeeds risks…
- duplicates
- silent data loss
- rebalance storms
- higher lag
Show the answer
silent data loss — Commit-then-crash means the event is never reprocessed — it is gone. Commit after success instead, and accept duplicates (which idempotency handles).
5. What decides when Kafka deletes an event?
- Every consumer group has read it
- The producer sends a delete
- The consumer commits its offset
- The topic's retention policy — time (e.g. 7 days) or size
Show the answer
The topic's retention policy — time (e.g. 7 days) or size — Kafka doesn't track who has read what for deletion purposes. Segments older than retention.ms (or beyond retention.bytes) are removed whether or not anyone read them. (Compacted topics instead keep the latest value per key.)
6. A new fraud team wants to process ride events from the last 3 days. Retention is 7 days. What's the least disruptive approach?
- Start a new consumer group with auto.offset.reset=earliest (or reset it by timestamp to 3 days ago)
- Ask billing to pause so fraud can catch up
- Have the producer resend 3 days of events
- Copy the topic first
Show the answer
Start a new consumer group with auto.offset.reset=earliest (or reset it by timestamp to 3 days ago) — A new group has its own offsets, so reading history affects no one else. The data is already on disk; producing it again would create duplicates for every existing consumer.
There are 8 questions, 3 puzzles and 5 recall cards for this lesson in the app — the recall cards come back on later days, just before you would forget.
Start this lesson →