Write amplification
Step 1 · The pattern
One row, more than one write
The app saves one order. The database writes the row to the table and an entry to the primary key index. That is two writes on disk for one from the app, and the disk has room to spare.
- row write
- index write
- saved
In short
Write amplification: how it works and how it breaks
Write amplification is the ratio between what a database writes to storage and what the application asked it to write. It is never one. A single row change is recorded in the write-ahead log, written to the table and written again to every index on that table. In PostgreSQL an update creates a new version of the row, and unless a special shortcut applies, every index gets an entry for it, even indexes on columns that did not change. So each index that speeds up a read taxes every write. The cost stays hidden while the disk has headroom. Then it appears as slow commits, a growing log and replicas that fall behind, with no change in traffic. Log-structured engines pay the same tax in another form: compaction rewrites the same data several times as it merges files. Count the indexes on busy tables and drop those nothing reads.
What this simulation simplifies
- Each dot is one page write. A real database changes pages in memory first and flushes them later in batches, so disk writes are smoothed.
- The write-ahead log is left off the map. Every table and index change shown here is also recorded there first.
How this comes up in interviews
"Writes to a table have become slow and its disk is busy, but the request rate has not changed. What would you look at?"
Then they ask
- Why does an update to one unindexed column still write to the table's indexes?
- How would you decide which indexes are safe to drop?
- How does write amplification differ between a B-tree engine and an LSM tree?
A strong answer covers
- Write amplification is storage writes divided by what the application asked to write, and it is always above one.
- A row change is written to the log, to the table and to every index on that table.
- Each index speeds up some reads and adds a write to every insert, so unused indexes are pure cost.
- The symptoms are slow commits, a fast-growing log and replica lag, with flat traffic.
- LSM engines move the cost to compaction, which rewrites the same data as it merges files.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Uber Engineering: Why Uber Engineering switched from Postgres to MySQL ↗ · PostgreSQL documentation: heap-only tuples (HOT) ↗ · PostgreSQL documentation: WAL configuration ↗ · PostgreSQL documentation: the cumulative statistics system ↗ · RocksDB wiki: tuning guide (amplification factors) ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.