Back to blog

Where does a database actually put your rows?

Thắng Đoàn
Thắng Đoàn

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

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.