Home›Learn›Start 2: 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.

Why it exists. Without it, every query reads the whole table, and each new row makes that slower.

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.

Why it exists. When one fact is copied into many rows, an update that misses one copy leaves the data disagreeing with itself.

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.

Why it exists. Normalized data lives in separate tables. A join puts it back together to answer one question.

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.

Why it exists. Real work takes several steps, like taking money from one account and adding it to another. A crash between them must not leave half of it done.

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.

Why it exists. These four promises are why you can trust a database with money and orders, and not just a folder of files.

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.

Why it exists. In any busy app, two transactions will sometimes lock the same rows in opposite order. The database has to break the circle, or both would wait forever.

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.

Why it exists. A huge table is slow to scan, to index and to clean up. Parts let a query, or a cleanup job, touch only what it needs.

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.

Why it exists. One server has a ceiling: disk, memory and writes per second. Sharding is how you grow past it.

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.

Why it exists. One server is one failure away from an outage, and one disk away from losing data. A copy is the way back.

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.

Why it exists. Most apps read far more than they write. Copies can share the reads, so the primary keeps up with the writes.

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.

Next · chapter 3How a query travelsFrom your app to the disk and back← Chapter 1: The basics
One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 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.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect