Unit 1 of 15 · about 4 hours · explain, implement

Append-only segmented logs, offsets, sparse indexes, retention and compaction

Every later contract in this section, from consumer positions to checkpoints, is an offset into a durable log. If the log cannot bound its storage or rebuild its index after a crash, nothing above it holds.

You'll be able to

  • Explain why a log assigns monotonically increasing offsets per partition and what a consumer offset marks.
  • Explain segments, sparse indexes, time and size retention, and log compaction as latest-value-per-key retention.
  • Implement an append-only segmented log with a sparse index, retention and recovery that replays from the last valid record.

Covers

Append-only segmented logOffsets and sparse indexesRetention versus compaction
1

Read

In this order, only the parts listed

0 of 4 done

  1. The log as the unifying abstractionThe Log: What Every Software Engineer Should Know About Real-time Data's Unifying Abstraction (Kreps, 2013)

    Part One: the log as an append-only totally ordered sequence; the state-machine replication principle; tables and changelogs as dual views of the log

  2. Log-based message brokers, offsets and retentionDDIA 2e - Chapters 11 to 13 Batch and Stream Processing

    Chapter 12, Log-Based Message Brokers: per-partition offsets (pp. 495 to 496); consumer offsets (p. 498); segments, retention and the circular-buffer bound with the 22-hour calculation (pp. 498 to 499)

  3. Log compactionDDIA 2e - Chapters 11 to 13 Batch and Stream Processing

    Chapter 12, Log compaction (pp. 505 to 506): keep the latest value per key, tombstones for deletes, rebuild derived state with a scan from offset 0

  4. Kafka design ancestor: segments, offsets, pull consumersKafka: a Distributed Messaging System for Log Processing (Kreps, Narkhede and Rao, 2011)

    The log-structured design (segments, offsets, stateless broker, pull consumers) and the final section (replication as future work)

    Take durability and replication from the current Kafka 4.3 docs, not this paper. Its producer is fire-and-forget with no delivery guarantee.

2

Focus on

  • An offset identifies a position in one partition only. It never restarts and is never reused within that partition.
  • A segment bounds the unit of deletion, and a sparse index maps sampled offsets to file positions, so lookup stays fast while index memory stays bounded.
  • Time and size retention deletes whole segments, so a consumer that falls behind the oldest retained segment misses messages. Compaction instead keeps the latest value per key plus tombstones, so a scan from offset 0 rebuilds current state.
  • The book gives a concrete bound: one 20 TB disk written at 250 MB/s fills in about 22 hours, so even at full write bandwidth a single disk holds about a day of messages. Real deployments write well below full bandwidth, so retention reaches days or weeks.
Common mistakes · 3
  • Treating offsets as global across partitions. Ordering and positions hold within one partition only.
  • Expecting compaction to shrink append-only event streams. Without repeated keys nothing is deduplicated.
  • Assuming a slow consumer can always catch up. Retention deletes segments, so a lagging offset can point at deleted data.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive the 20 TB at 250 MB/s bound (about 22 hours) and sketch the segment layout, sparse index entries, and the scan-from-0 rebuild of a compacted topic with one tombstoned key.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A topic has 3 partitions and a consumer reads offsets 0 to 99 on each. How many totally ordered sequences is it reading?Answer

    Three, one per partition. There is no ordering guarantee across partitions.

  2. 2.A drive holds 20 TB and ingest runs at 250 MB/s. How long before the oldest segment must be deleted?Answer

    About 22 hours: seconds, which is about 22 hours. Deployments that write below full bandwidth retain days or weeks.

  3. 3.A compacted topic holds keys mew, purr and scratch, then a tombstone for purr. What does a scan from offset 0 return?Answer

    The latest value for mew and scratch, and nothing for purr. The tombstone removes purr during compaction.

  4. 4.Why does the Kafka 2011 paper not settle your durability design?Answer

    Replication is future work in that paper and its producer is fire-and-forget, so broker loss behavior must come from the current 4.3 docs and your own acknowledgement contract.

5

Practice

  • Gossip Glomers 5a: single-node Kafka-style log

    Implement send, poll, commit_offsets and list_committed_offsets; run the Evaluation section's kafka workload on one node with the flags given on the page

    The checker catches lost writes and nonmonotonic or skipped offsets. It has no recency requirement, so an acknowledged send need not appear in a poll right away; your own log should still serve it on the next poll.

You're done when

You can derive the retention bound, explain what compaction keeps, and your 5a node passes the single-node run with its history retained.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 15 more in the section

Also in this section

  • The Dataflow Model (Akidau et al., 2015)Google (author copy) · required

    Read the model section: event time versus processing time, windowing, triggers and accumulation. Implement one trigger and one accumulation mode, not the catalog.

  • MillWheel: Fault-Tolerant Stream Processing at Internet Scale (Akidau et al., 2013)PVLDB · support

    Read low watermarks, persistent state, strong productions and exactly-once delivery by deduplication. The deduplication holds only within its retention bound.

  • Lightweight Asynchronous Snapshots for Distributed Dataflows (Carbone et al., 2015)arXiv · required

    Read the asynchronous barrier snapshotting algorithm for acyclic graphs and its recovery; skip the cyclic extension. Barriers assume FIFO channels.

  • Distributed Snapshots: Determining Global States of Distributed Systems (Chandy and Lamport, 1985)Leslie Lamport · support

    Read the marker-sending and marker-receiving rules and why the recorded state is consistent. The algorithm needs FIFO channels.

  • Apache Kafka 4.3 - Design, Log, Rebalance and Transaction ProtocolsApache Kafka · required

    Read the 4.3 design page (replication and in-sync replicas, acknowledgements and min.insync.replicas, delivery semantics, idempotence and transactions, the consumer, log compaction), the log implementation page, and the consumer rebalance protocol and transaction protocol pages (https://kafka.apache.org/43/operations/). Kafka 4.0 removed ZooKeeper. Run experiments in the apache/kafka:4.3.1 image and compare each contract with your mini log.

  • Gossip Glomers - Kafka-Style LogFly.io and Jepsen · required

    Each challenge runs locally under Maelstrom (v0.2.4; the pages were tested on 0.2.3) with its checker and, where the page says so, the partition nemesis; keep the run directory as the history. Do 5a to 5c: send, poll, commit_offsets and list_committed_offsets on one node, then two nodes over lin-kv, then with fewer messages. The checker catches lost writes and nonmonotonic or skipped offsets; it does not check recency or exactly-once effects.

  • AWS Builders' Library - Avoiding Insurmountable Queue BacklogsAmazon Web Services · support

    Read it for how backlogs grow and why they take long to drain, and for backpressure, prioritization, sidelining and constant work. Compare its recovery strategies with the bounded backlog and lag measurements of your consumer.

  • Apache Flink 2.3 - Event Time, State and CheckpointingApache Flink · required

    Read the 2.3 concepts pages on time (event time, watermarks, lateness, windowing) and stateful stream processing (keyed state, checkpoint barriers, recovery, unaligned checkpoints, exactly-once versus at-least-once), then the watermark-generation guide on idle sources. Use them to build one bounded local processor; exactly-once covers state and participating sinks only.

  • Confluent Schema Registry - Schema Evolution and CompatibilityConfluent · support

    Use only the compatibility-type definitions (backward, forward, full and their transitive forms) as the vocabulary for your schema checks, with the Avro schema resolution rules as the mechanism.

  • HLD Handbook - Queues, Streaming, Stream vs Batch and CDCHLD Handbook · required

    Read only Part 2 message queues and pub-sub plus Part 4 stream versus batch and change data capture. Use Part 5 event-driven architecture after the underlying log and replay contract is clear.

  • MIT 6.5840 - Lab 1 MapReduceMIT PDOS · support

    Complete or review the coordinator and worker lab to connect deterministic batch dataflow, task retries and output publication. Do not confuse its shared-filesystem assumptions with the durable log contract.

  • Replicated Log and Stream Processor - Full Data PathRoadmap-Final · support

    Complete partitioned log, producer identity, consumer ownership, offsets, backpressure, event-time state, checkpoint, replay and sink boundary. Run the required producer, consumer, storage and late-data fault campaign.

  • Apache Iceberg - Table SpecificationApache Iceberg · reference

    Read the snapshot, manifest list and manifest sections for the read path, and the table metadata section for the atomic metadata-file swap that commits a snapshot.

  • Delta Lake Protocol (pinned commit)Delta Lake · reference

    Read the transaction log replay and checkpoint sections for how readers reconstruct table state, and the concurrency section for the optimistic commit rule.

  • Apache Avro 1.12.0 Specification - Schema ResolutionApache Avro · reference

    Read Schema Resolution only: records match by field name, writer-only fields are ignored, reader-only fields are filled from defaults, plus the promotion rules.