Card 05/ 11
Databases: Where State Lives
Every other part can be thrown away and rebuilt; the database cannot.
The database is the part of a web system that keeps data for good: accounts, orders, posts, and the state the application servers have moved out of their own memory. It is the only part whose loss means lost data, which is why it gets the backups, the copies and the most careful changes.
Why it is different from every other part. Turn off a CDN edge and it refills from the origin. Turn off an application server and another takes its place. Empty a cache and it fills again from the database, more slowly. Lose the database without a copy and the orders, the accounts and the history are gone, and no new machine brings them back. Every other part holds a copy or holds nothing; the database holds the original.
Where it sits. The database sits at the far end of a request's path, behind the application servers. Readers never talk to it directly. The application servers send it queries, and a cache in front of it answers the reads that repeat, so the database sees fewer of them.
What protecting it involves. A backup is a copy taken at a point in time, so a mistake can be undone by going back to it. A replica is a second database kept up to date with the first, so a failure of one machine loses nothing and reads can be spread across both. A schema change, altering the shape of a table, is planned and rehearsed, because a mistake there changes every row.
Where you meet it. PostgreSQL and MySQL are relational databases: they store rows in tables with a fixed shape. Amazon DynamoDB and MongoDB are NoSQL databases, which trade some of that structure for other strengths. The choice between the two kinds has its own card; here, what matters is that both hold the original.
What it is not. A database is not the right home for everything. Large files such as images and video belong in object storage, with only their address in a row, and short-lived values that are cheap to rebuild belong in a cache.
So spend your care where the original data lives: every other part can be rebuilt from the database, and the database can be rebuilt only from its backups.
← → move through sections and cards