Learn Labs
4. Storage and Retrieval

4.1 The world's simplest database

Real databases add: handling concurrent writes, reclaiming disk space so the log doesn't grow forever, and handling partially written records when recovering from a crash.

#!/bin/bash
db_set () { echo "$1,$2" >> database; }
db_get () { grep "^$1," database | sed -e "s/^$1,//" | tail -n 1; }
$ db_set 12 '{"name":"London","attractions":["Big Ben","London Eye"]}'
$ db_set 42 '{"name":"San Francisco","attractions":["Golden Gate Bridge"]}'
$ db_set 42 '{"name":"San Francisco","attractions":["Exploratorium"]}'
$ db_get 42
{"name":"San Francisco","attractions":["Exploratorium"]}

$ cat database
12,{"name":"London",...}
42,{"name":"San Francisco","attractions":["Golden Gate Bridge"]}   ← old version kept
42,{"name":"San Francisco","attractions":["Exploratorium"]}        ← tail -n 1 wins

Every db_set appends. Updates don't overwrite; you find the latest value by looking at the LAST occurrence of a key (hence tail -n 1).

PerformanceWhy
db_setVery goodAppending to a file is generally very efficient — the simplest possible write operation
db_getTerrible — O(n)Scans the entire file every lookup. Double the records → double the time

Terminology note (important, used all book): log here does not mean application logs. It means an append-only sequence of records on disk. Not necessarily human-readable; may be binary and purely internal.

Real databases add: handling concurrent writes, reclaiming disk space so the log doesn't grow forever, and handling partially written records when recovering from a crash. The principle is the same.

The index trade-off — the sentence that governs the whole chapter

An index is an additional structure derived from the primary data. Adding/removing an index doesn't affect the contents of the database — only the performance of queries.

Well-chosen indexes speed up read queries, but every index consumes additional disk space and slows down writes, sometimes substantially.

That's why databases don't index everything by default — they require you to choose indexes using knowledge of the application's typical query patterns.


On this page