Course map

Topic 111 / Core

Kafka architecture: logs, retention, replay

Chapter 7.2 · Broker Architectures

What and why

Topics 106–110 described queue mechanics in the abstract; Kafka is the concrete example of a different design choice — a log instead of a disappearing queue — that changes what's possible. A Kafka-style broker structures a topic as one or more append-only, replicated partitions (logs); producers append, consumers read at their own offset, and messages are not removed on consumption — they age out only by a retention policy, which makes replay a first-class, ordinary operation rather than a special feature. Builds on: → 108.

Diagram

partition (log):  [0][1][2][3][4][5][6][7] ...  (append-only, offsets increase)
consumer group A:         ^ offset 3   (reads independently)
consumer group B:   ^ offset 1          (reads the same log, its own pace)
                     └── retention window: oldest offsets age out past here ──┘

How it works

  1. A topic is divided into partitions, each an ordered, append-only log identified by increasing offsets.
  2. Producers append to the partition selected by key (or round-robin without one); replication (→ 72) copies each partition across brokers for durability.
  3. Consumers, within a group (→ 108), each track their own offset into the partitions they own; multiple groups can read the same log independently, at different speeds, without affecting each other — the same log doubles as both a work queue and a pub/sub stream (→ 109).
  4. Messages are retained for a configured window (time- or size-based) regardless of whether every consumer has read them — consumption never deletes data.
  5. Because the data stays and an offset is just a position marker, replay is simply resetting a consumer group's offset to an earlier point and re-reading — used to reprocess after a bug fix or to backfill a new consumer (Project 19).

Practical example

(illustrative). A topic retains 7 days at 1,000 events/sec, averaging 1 KB (1,000 bytes) per event. That's 1,000 × 1,000 bytes = 1,000,000 bytes/sec = 1 MB/sec; over a day, 1 MB/sec × 86,400 sec = 86,400 MB ≈ 86.4 GB/day; over 7 days, 86.4 × 7 ≈ 604.8 GB retained before the oldest segment ages out — the concrete storage cost of "replay for a week" (→ 102).

When to use it

  • Use when high-throughput streaming, multiple independent consumers of the same history, or replay/reprocessing are needed.
  • Avoid when rich per-message routing or a simple, low-overhead task queue is the actual need — that is RabbitMQ's niche (→ 112).
  • Failure scenarios: under-provisioned retention loses history right when a bug is found and replay is needed; a consumer group that falls behind the retention window has turned lag (→ 108) into permanent data loss, not just delay.
  • Simpler alternative: at modest scale, a database table with a sequential ID and a "last processed ID" column per reader gives ordered replay with no broker at all.

Practice

(mechanism — trace and rebuild by hand; 20–30 min, lab Kafka). Create a 3-partition topic, produce 30 keyed messages, consume them, then reset the consumer group's offset to 0 and re-consume. Confirm you get the identical 30 messages again, in the same per-partition order. Done when a full replay reproduces the exact same per-partition sequence.

Shared lab setup

Key takeaway

A log-structured broker keeps data after consumption, turning replay from a special feature into the default behavior. Done when you can: explain why independent consumer groups don't interfere with each other on the same log, and compute storage cost from retention and throughput. Used next in → 119 (CDC output commonly lands in a log like this), → 124 (stream processing replays this log); Project 19.

References

Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide