Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Portfolio Projects the lesson

Build Your Own Database a show in 8 chapters

Goal: a store where every write it says yes to survives a crash, in about 240 lines of Go

1 of 8 Your guess

Before we start

You save something in an app, the power cuts out, and when it comes back the thing is still there. Something kept it safe through the crash. That something is a database.

At its smallest, which picture is closest to what a database is?

Pick the closest. You find out which fits at the end.

where System R's 3 years went:SQL, a language typed like Englishanswering questions fasttwo people changing one row at once1974197519761977first real customer: Pratt & Whitneystill there after a crash← this project

1977 − 1974 = 3 years to the first real customer

2 of 8 The problem

1974: three years before the first customer

In 1974 a team at IBM's research lab in San Jose began System R, a database you could ask questions in a new language, SQL, typed almost like English. It reached its first real customer, the aircraft-engine maker Pratt & Whitney, in 1977.

Most of those years went into the language and into answering fast. The rest went into two people changing the same row at once without one wiping out the other.

Almost none of it went into the one thing that makes a database more than a file: what you wrote is still there after a crash mid-write. That part is small.

Step 1 of 2

db.Put("user:1", []byte("Alice"))

memoryuser:1→Alicecart:9→socksa Go map: key → valuedisknothingwritten

3 of 8 The idea

A file that only grows

Part 1 is three calls: Put stores a value under a key, Get reads it back, Delete forgets it. Behind them sits a table in memory, a Go map from key to value.

It is fast and it works, until the program stops.

Two keys in memory, nothing on disk.

Part 1 proof: the stage 1 test (Put, Get, an overwrite, Delete, a key never set) passes on a plain Go map.

Step 1 of 7

if h.Sum32() != crc { break }

the file, on diskoffsetindex,in memory0user:1 = Alice23 B23cart:9 = socks23 B46user:1 = Alicia24 Bemptywiped bythe crashfile: 70 bytes

4 of 8 Try it

Pull the plug

The program crashes. The index in memory is gone. The 70-byte file is all still there.

So on startup the store reads the file from its very first byte, one record at a time, and writes the index again from what it finds.

Press Play to read the file from byte 0.

Step 1 of 4

db.Delete("cart:9")

4crc46000keylength4ffffffffvaluelength6cart:9keyone box = one byte

4 + 4 + 4 + 6 = 18 bytes, no value bytes at all

5 of 8 The fix

Delete by adding

Delete cart:9. You can't reach into the file and erase bytes: the whole point was never touching old ones. So a delete adds one more record.

Its value length is the largest number 4 bytes can hold, ff ff ff ff, which no real value uses. It means "this key is gone". That marker is a tombstone.

Step 1 of 5

old file: 88 bytesuser:1 = Alicedeadcart:9 = socksdeaduser:1 = Alicialivecart:9 tombstonedeaddead: an older copy, or a deletealready applied to the index

dead: 23 + 23 + 18 = 64 of 88 bytes

6 of 8 The cost

Shrink it, then time it

Every overwrite and every delete leaves old bytes in the file doing nothing. Of these four records only one is still read: user:1 = Alicia.

Given long enough, most of the file is dead weight.

Step 1 of 5

7 of 8 Your guess

Your picture now

Goal met: five parts, 242 lines of Go, and every write the store says yes to comes back after a crash. Which picture is closest now?

Pick one.

Build your own databaseNever touch an old byte. Only ever add a new one, and remember where it went.1Put, Get, Delete on a mapfast, and gone on a restart2Append every write to a logindex: key → offset · 23 bytes a record3Replay from byte 0 on startstop at the first bad crc, cut the rest4Delete = append a tombstone18 bytes · 4 writes = 88 bytes5Compact: keep the newest live copy88 → 24 bytes, then rename267/sfsync after every write, 3.75 ms each~53,000/s200 writes share one fsyncchalkbrain.com
Build your own databaseNever touch an old byte. Only everadd a new one, and remember where.1Put, Get, Delete on a mapfast, and gone on a restart2Append every write to a logindex: key → offset · 23 bytes a record3Replay from byte 0 on startstop at the first bad crc, cut the rest4Delete = append a tombstone18 bytes · 4 writes = 88 bytes5Compact: keep the newest live copy88 → 24 bytes, then rename267/sfsync after every write~53,000/s200 share one fsyncchalkbrain.com

8 of 8 Keep this

Keep this

Never touch an old byte, only add a new one. A small index remembers where each key's newest copy landed, and a restart reads the file again from byte 0.

The full lesson has the five stage test files, the byte dump, the fsync timer and how to put it on GitHub with a passing test badge.

Read the full lesson