◐ ai-teacher

⚡ 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

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:

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

The classic question

"How do you increase throughput of a Kafka consumer?"

Layered answer:

  1. Add consumers up to the partition count
  2. Beyond that, increase partitions (and accept the re-keying cost)
  3. Batch and parallelise the downstream write — usually the real bottleneck is the sink, not Kafka
  4. 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

EventAn immutable record that something happened, at a point in time.
TopicA named, durable stream of events.
PartitionAn ordered append-only shard of a topic; the unit of parallelism and ordering.
OffsetA consumer's position within a partition.
Consumer groupConsumers cooperating to read a topic; each partition assigned to exactly one member.
Consumer lagNewest offset minus committed offset — how far behind a group is.
RetentionHow long events are kept, regardless of whether anyone read them.
RebalanceReassignment of partitions when group membership changes.

In two minutes

Test yourself

1. A topic has 3 partitions. You run 5 consumers in one group. What happens?

  1. Throughput increases 5×
  2. 3 consumers get a partition each, 2 stay idle
  3. Each consumer reads all partitions
  4. 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…

  1. across the whole topic
  2. within a partition
  3. within a consumer group
  4. 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?

  1. It also stopped
  2. Nothing — offsets are per group, so it kept reading
  3. It received billing's messages too
  4. 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…

  1. duplicates
  2. silent data loss
  3. rebalance storms
  4. 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?

  1. Every consumer group has read it
  2. The producer sends a delete
  3. The consumer commits its offset
  4. 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?

  1. Start a new consumer group with auto.offset.reset=earliest (or reset it by timestamp to 3 days ago)
  2. Ask billing to pause so fraud can catch up
  3. Have the producer resend 3 days of events
  4. 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.

Learn it so it stays. Read it, test yourself, then recall it on two different days. The app schedules that for you, free.

Start this lesson →