Know your write amplification
One logical write turns into many physical writes: WAL plus page, every index, every replica, and LSM compaction rewriting the same data several times.
Say it like this
Each chat message is one logical write but about forty physical ones once I count the WAL, three indexes, three replicas and LSM compaction, so I drop the indexes the hot path does not use and batch the rest.
Basic solution
Count the writes a row costs (indexes × replicas × compaction levels), drop indexes you do not query, batch small writes, and pick an engine whose amplification fits the workload.
One logical write becomes many physical ones
Count the streams: WAL, table, each index, each replica, and compaction rewrites on an LSM. The multiplier is the product, not the sum, which is why one extra index on a replicated LSM table is expensive.
How it works
- Count what one logical write touches on the primary: an entry in the write-ahead log, the table page that holds the row, and one page per secondary index that includes the row. A row with three indexes is at least five physical writes.
- Multiply by the number of replicas, because each replica receives the same change and repeats the same writes on its own disk. Three copies triple the total.
- On an LSM engine, add compaction: a row written once to level 0 is rewritten every time its SSTable is merged into the next level down. Levelled compaction rewrites a byte roughly 10 times per level across four or five levels; tiered compaction rewrites far less but leaves more files for reads to check.
- Add the engine's own overhead: B-tree page splits, full-page writes (Postgres logs the whole page the first time it changes after a checkpoint), and the filesystem journal.
- Compare the total bytes per second against what the device can take: SSDs have a budget of drive writes per day and a throughput ceiling, and when amplified writes approach that ceiling, latency climbs long before CPU does.
Levers, in order of cheapness
- Remove indexes nobody queries; each one is a full extra write stream.
- Batch many small writes into one transaction or one SSTable flush.
- Choose the compaction strategy for the workload, or a B-tree engine when writes are updates to existing rows.
Worked example
Chat app message store
- 1M messages per second at peak, 300 bytes each: 300 MB/s of logical writes.
- Wide-column store, replication factor 3, three secondary indexes (by sender, by thread timestamp, by media flag), levelled compaction across 4 levels with a fan-out of 10.
- Per replica: 1 (memtable flush) + 3 (indexes) = 4 writes; compaction rewrites each of those about 10 times per level × 4 levels = 40×. Rough total: 4 × 40 = 160× the logical bytes per replica.
- Across 3 replicas: 480× → 300 MB/s becomes 144 GB/s of disk writes. No cluster of sensible size survives that.
- After fixes: drop two indexes the app never queries (the thread read is by partition key, not index) → 2 streams; switch to tiered compaction with a 3–4× rewrite factor → 2 × 4 = 8× per replica, 24× total, about 7 GB/s across the cluster. Over 60 nodes that is 120 MB/s each, within an NVMe budget with headroom.
- The read side pays for tiered compaction with more SSTables to check, which Bloom filters mostly absorb; say that out loud as the trade.
Variants and when each wins
B-tree engine (InnoDB, Postgres heap + B-tree)
- Rows are updated in place, so amplification comes from random page writes, index maintenance and full-page writes after checkpoints. It wins when writes are updates to hot rows that fit in memory.
LSM with levelled compaction (RocksDB default, Cassandra LCS)
- Each level holds non-overlapping sorted files about ten times the size of the level above, so reads check one file per level. It wins for read-heavy tables that still take steady writes, at the price of the highest write amplification, often 10 to 30×.
LSM with tiered or size-tiered compaction (Cassandra STCS, RocksDB universal)
- Files of similar size are merged together when enough accumulate, so each byte is rewritten only 3 to 5 times. It wins for append-heavy time series and logs, at the cost of more files per read and temporary space doubling during merges.
Append-only log with periodic snapshots (Kafka, event logs)
- Records are only ever appended, with no updates and no secondary indexes. It wins when access is sequential, because amplification is just the replication factor.
Measure first: disk bytes written divided by logical bytes written. Then choose the engine or compaction for the dominant pattern instead of the default.
Failure modes
SSD wear-out
- Drives fail early, or cloud volumes hit their write quota. It happens because amplification pushed physical writes far above the device's drive-writes-per-day budget. Compute the multiplier, drop unused indexes, and move to tiered compaction or larger memtables.
Write stalls on an LSM
- p99 write latency spikes to seconds every few minutes. It happens because compaction cannot keep up, level 0 fills with unmerged files, and the engine throttles writers until it catches up. Add compaction threads, enlarge the memtable, use fewer levels, or reduce the write stream.
Replica lag under write bursts
- Replicas fall behind during the evening peak and read-your-writes breaks. It happens because every replica must perform the same amplified work as the primary. Cut amplification on every node rather than scaling only the primary, and batch writes so the WAL stays sequential.
Index added for one report
- Write throughput drops 25% after a deploy. It happens because someone added a secondary index to a hot table, and every insert now maintains it. Serve the report from a replica or the analytics store, and keep indexes on hot tables to the ones the hot path uses.
Full-page writes after checkpoints
- Postgres shows periodic bursts of write traffic. It happens because the first change to each page after a checkpoint writes the whole 8 KB page to the WAL, not just the changed bytes. Lengthen the checkpoint interval, allow a larger WAL, or compress full-page images.
Interviewer follow-ups
Answer out loud before you open each one.
How would you measure write amplification on a running system?
Divide bytes written at the disk (iostat or the engine’s own counters, e.g. RocksDB compaction statistics) by bytes of logical writes the application sent over the same window. A number over 10 on a write-heavy table is the signal to act.
Why do LSM trees have high write amplification if they were designed for writes?
They turn random writes into sequential ones, which is what makes the initial write fast, but they pay for it later by rewriting data during compaction. The design trades latency now for background I/O later; the total bytes written still grow with each level.
Does batching reduce amplification or just throughput cost?
Both. A batch of 1,000 rows produces one WAL sync and one memtable flush instead of 1,000, and on a B-tree the index pages touched are shared across the batch, so fewer pages are dirtied per row.
What is the relationship between write and read amplification?
They trade against each other through space. Levelled compaction keeps data in few sorted runs, so reads touch few places but every write is rewritten many times; tiered compaction writes each byte fewer times but reads must check more runs. You choose by which side is the bottleneck.
Connections
- Built on
- B-tree · LSM tree
- Pairs with
- Write-optimised vs read-optimised storage · Index the columns you filter on · Batch small operations · Know your read amplification
- Pulls against
- Precompute joins
- Used in
- Chat App (WhatsApp / Messenger) · Ride Matching (Uber / Lyft)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Chat service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |