Where does a database actually put your rows?
FotoFlow has 15,000 users and a write-heavy table: likes and view events land constantly, each touching a different row. Reads are indexed and fast, but the database still crawls. The cause is below the SQL layer, in the storage engine: the part deciding what goes in memory, what on disk, and when.
Memory is fast and forgetful
A change in memory takes nanoseconds but vanishes on a crash. Disk survives a power cut, but confirming a write is truly on the device, an fsync, is thousands of times slower.
The naive answer is to change pages in memory, reply "saved", and copy pages to disk every few seconds. Then the power fails, and everything since the last copy is gone, including writes your users were told had succeeded. A half-written page is garbage: a torn page.
The write-ahead log
Pushing every changed page to disk before replying is safe but slow: a whole 8 KB page rewritten at a random position to save one 100-byte row. Instead, describe the change in a few bytes, append it to a log, and force that to disk before replying: the write-ahead log. Appending to one file is the fastest thing a disk can do; the pages are updated in memory and copied out later. After a crash the engine replays the log and rebuilds the state it promised. A write-ahead log makes saved mean saved: append the change, fsync, then reply.
Forcing the log to disk once per transaction becomes the limit at thousands of commits a second, so databases offer a dial: sync on a timer instead, multiplying throughput and accepting that a crash loses the last moments of writes. Group commit softens the cost, letting transactions that arrive together share one fsync. How often your database fsyncs is a durability dial; know where yours is set. Redis in append-only mode syncs once a second by default; PostgreSQL synchronous_commit = off makes the same trade. A periodic checkpoint copies changed pages to the data file and throws old log away, so recovery replays seconds, not days.
Two engines: update in place, or only append
The classic design, inside PostgreSQL, MySQL and Oracle, keeps data in fixed-size pages found through B-trees and modifies each page in place. Any row is a few page reads away. But on a firehose of small writes, every write eventually costs a scattered page write. Storing 100 bytes can cost a whole 8 KB page write; that is write amplification.
The LSM-tree drops the idea of one home per row. A write goes into a sorted structure in memory, the memtable; when it fills, it flushes in one sequential write as an immutable SSTable. Updates are newer entries, deletes are tombstones, nothing on disk is modified: the engine behind Cassandra, RocksDB and HBase. Reads must check several files newest to oldest, rescued by a Bloom filter per file that answers "definitely not here" without touching disk. A background process, compaction, merges files and keeps the count low; if writes outrun it, files pile up and the disk fills.
B-trees pay at write time to keep reads simple; LSM-trees keep writes simple and pay at read time.
Rows or columns
Both designs keep each row together, ideal for fetching whole records. But SELECT SUM(price) FROM orders across two billion rows forces a row store to read all 40 columns of every row to add up one. A column store keeps each column in its own file, reads a fortieth of the data, and compresses well. That is how BigQuery, Snowflake and ClickHouse scan billions of rows in seconds. Row stores serve the app; column stores serve analytics.
What this does not solve
A storage engine keeps one machine's data fast and durable. It says nothing about the machine itself dying, or scaling reads beyond one box. That is replication, the next post.
Your database's durability dial: is it set to survive a power cut, or to survive a benchmark?
Your heaviest table: would it be happier if updates never happened in place?
One engine per database, chosen for the workload it serves.
Next in the series: Two copies of the same data: safety or trouble?
Sources
Wikipedia: Log-structured merge-tree
Designing Data-Intensive Applications
Redis Docs
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.