Home›Learn›Ten concepts
Course · FoundationsLesson 2 of 330 read

10 database concepts, explained in a library

What do the words on this site actually mean?

One picture and one line each. No prior knowledge. Five minutes.

A database is a library. Tables are shelves. Rows are books. The card catalog is the index, the loan desk is where transactions happen, and the branch libraries are the shards.

Learn the library version of each concept, and the database version is the same job with files instead of paper. When a term below feels abstract, picture the shelf.

Structure

1.Index

WHERE title = 'Dune': 1 lookup, 4 buffers, 0.05 ms. Without the catalog: a scan of every shelf, 3 to 8 seconds.

An index is the card catalog. It trades a little disk and a little write cost for lookups that stay fast as the shelves fill.

Structure

2.Normalization

HP1 · Rowling, UKHP2 · Rowling, UKHP3 · Rowling, UK
The author is written on every book. Fix a spelling, fix it three times.
→books: HP1 · a1authors: a1 · Rowling, UK
Now the name lives in one place.

One fact, one place. The author's name lives on a single author card, so a correction has exactly one place to land.

Structure

3.JOIN

members: 1 · Ashaloans: 1 · Dune→Asha · Dune
Two lists, matched on the shared key member_id. The result is one row per match.

A join matches two tables on a shared key. The key is what makes it cheap. Without a key, it reads both tables end to end.

Transactions

4.Transaction

Book 1Book 2Book 3→One checkout
Borrow 3 books. Either all three leave with you, or none do.

A transaction groups steps so they commit together or roll back together. There is no half-borrowed state.

Transactions

5.ACID

AAtomic
all or nothing
CConsistent
rules always hold
IIsolated
no interference
DDurable
survives a crash
The four promises a transaction makes. Break one, and the other three stop meaning anything.

Four promises, one line each. Durable is the promise the log pays for: the record hits disk before the commit returns.

Transactions

6.Deadlock

T1 holds Book 1wants Book 2 →T2 holds Book 2
T2 holds Book 2wants Book 1 →T1 holds Book 1
Each waits on the other. Neither can move. Waiting...

A deadlock is a circle of waiting. Nobody can move, so the database picks one transaction, rolls it back, and lets the other finish.

Scaling

7.Partitioning

FictionScienceHistory
One table, three shelves, same server. WHERE genre = 'Science' reads one shelf, not three.

Partitioning splits one table into parts on the same server. A query that names the part reads only that part.

Scaling

8.Sharding

Branch 1 · A-HBranch 2 · I-PBranch 3 · Q-Z
Dune lives at Branch 1. Nothing else is searched, and nothing else needs to be.

Sharding is partitioning across machines. It buys room to grow, and the price is that a query spanning branches is a query across the network.

Scaling

9.Replication

Library A · primarycopies →Library B · copy
Library A goes down. Library B keeps lending, once it is promoted and caught up, because it holds the same books.

A replica replays the primary's log, so it holds the same books. When the primary fails, the copy is what keeps the doors open, once something promotes it to primary. The copy can trail the original, and something still has to promote it before it can take writes; the next concept covers the lag.

Scaling

10.Read Replica

Writer → PrimaryReaders → Reading room copy
The copy can trail the original by seconds. That lag is the price of the split.

Writes go to the primary, reads go to the copies. The split only works if your readers can live with a copy a few seconds behind.

That is the vocabulary. Next: Start here, one insert end to end, then the three rules that run every database on this site.

Your turn

The primary is slow because of writes. Someone suggests adding two read replicas. In your own words, why will that not help, and what would?

Write your answer down first. Then open mine.

Show my answer

Why not: every replica has to apply every write the primary makes. Replicas take reads off the primary; they do nothing for writes. They also add lag, so anything that reads its own writes from a replica may not see them.

What helps: making each write cheaper (fewer indexes, smaller rows, batching), moving writes that do not need to be there (sessions, counters) to a store built for them, and only then splitting the writes across several primaries.

Part 1 of 4 · Learn · 23 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.