Module 04 · From Scratch · Kafka · with code: Go · about 25 min to read
Continue where you left off ×
Kafka 03: Segments and the Offset Index
Segments and Offset Index · Open board
It draws the log as a shelf of bound volumes instead of one endless scroll, and that picture is this whole lesson. The project this stage belongs to is Build Your Own Kafka, From Scratch . It follows Kafka 02: The Log and hands off to Kafka 04: Partitions .
Before you start. This series builds a small copy of Kafka, the program big companies use to pass events (every click, every order) from the programs that make them to the programs that need them. Other programs send it a stream of records and read them back later, in order. The last stage turned that stream into one file that only grows, where a record’s distance from the start of the file, its offset, doubles as its address.
The idea in 30 seconds
A growing file can only be trimmed from the end, never the middle, so Kafka cuts its log into many smaller files instead of one.
Each file is a segment, named by the offset of its first record, its base offset, which keeps the directory sorted for free.
Deleting old history is now deleting a whole file: nothing you keep is read or rewritten.
Each segment carries its own map, an index, and Kafka’s is sparse: one mark roughly every 4 kilobytes, not one per record.
The map can be rough because its records are already sorted and a whole segment goes stale together, all at once, unlike Rosenblum and Ousterhout’s filesystem, whose chunks needed a cleaner because a write anywhere could kill a page anywhere.
Read the story
Lesson 26 of 117 in From Scratch
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 1991: a filesystem that could not stop growing Kafka hits the same wall The fix: bound volumes, not one endless scroll Naming the volume by its first page A map that leaves out most of the room Reading one record: three narrowing steps Why Kafka’s map can afford to be sloppy What this stage still leaves behind Terms it explains: segment, base offset, binary search, index, sparse, keeping, The name survives everything, or, not.
Review From Scratch
Mark as read Saved in this browser, and to your account when you sign in.