Why you are doing this at all
If you ask which storage engine suits a write-heavy system, you usually hear a slogan: log-structured merge trees, or LSM trees, for writes, and B-trees for reads. It's half right, and the wrong half is the one that picks the engine.
Take one Postgres index and insert 100-byte entries into it. If the keys arrive in order, like timestamps, each insert writes at most about 3 bytes to disk for every byte of data. If they arrive at random, like user IDs, into a large index, each insert writes about 145 bytes per byte. An LSM tree writes each byte about 34 times. So the same B-tree is ten times cheaper than the LSM tree on one workload and four times dearer on another. The toolkit below works out all three numbers.
So "write-heavy" doesn't pick the engine. Both engines keep keys sorted on disk, because sorted order makes lookups and range reads fast. They differ in when they do the sorting.
A B-tree does the work of keeping keys sorted at the moment you write, one page at a time; an LSM tree puts that work off and does it later, in bulk, when it compacts.
A B-tree finds the page where the new key belongs and changes it. When that page goes back to disk, the whole page is written, however small the change was. An LSM tree adds the write to a sorted buffer in memory. Later it writes thousands of buffered writes to disk as one sorted file, and a background job called compaction merges those files to keep the whole data set in order.
The question that decides between them is how many writes share each write to disk. In a B-tree, that number depends on your key order, your write rate and the size of the index. In an LSM tree it's always large, because the engine batches writes before they reach disk.
Putting the work off doesn't remove it. Compaction still has to do it, so compaction is load you budget for. Until it has, a read may have to look in several files. And a delete has to be written down as a marker, because the data it removes might be in any of those files.
When the engine isn't your decision
Most of the time you choose a database and the engine comes with it. Choosing a database covers that choice. Its default, one Postgres, holds until a number forces you off it. The engine becomes your decision in three cases.
- Random keys go into a large index at a high write rate.
- Rows are deleted or expire at a high rate.
- A write-heavy data store also has to serve range reads.
The engine also isn't how a system spreads writes over machines. Cassandra takes more writes as you add nodes because it partitions the data, as partitioning and sharding describes. The engine decides what each node can take.
The two engines, and the three costs
How a B-tree writes
A B-tree stores keys in fixed-size pages: 8 KB in Postgres and 16 KB in MySQL's InnoDB. Leaf pages hold the keys in order, and the pages above them say which leaf covers which range of keys. Indexing and query performance works out why a tree over 50 million rows is only 3 levels deep.
An insert walks down to the right leaf and adds the key to that page in memory. Then two writes reach the disk.
- A record of the change is appended to the write-ahead log (WAL). The engine writes this log before it changes any page, so it can replay the change after a crash.
- Later, the changed page is written back to its place in the data file. That happens when the engine needs the memory, or at the next checkpoint, when it writes every changed page to disk. Postgres runs a checkpoint at least every 5 minutes by default.
The page goes back whole, so a 100-byte change costs an 8 KB write. Postgres adds a second copy. The first time a page changes after a checkpoint, it also writes an image of the page into the WAL, leaving out the page's empty middle. A page half-written during a crash can't be rebuilt from the change record alone. InnoDB guards against the same failure with its doublewrite buffer, which writes every page to a separate area first and then to its own place in the data file.
When a leaf is full, the next insert splits it. Postgres moves about half of the page's bytes to a new page and adds a pointer to that page in the parent. If the parent is full too, it splits as well.
Key order decides how full the pages end up. Increasing keys, like a timestamp or an identity column, always land on the rightmost leaf. Postgres fills that leaf to its fillfactor, 90 percent by default, before starting a new one. InnoDB fills pages to about 15/16 when keys arrive in order. Random keys land anywhere and split pages in the middle of the tree, and InnoDB's documentation says those pages end up between 1/2 and 15/16 full.
So what does one insert cost? It depends on how many other inserts change the same page before it goes back to disk.
The question is how much disk traffic one small insert
causes, with the keys in random order and then in
sequence. Postgres defaults: 8 KB pages and full page
images. A checkpoint every 5 minutes assumes
max_wal_size is raised; at its 1 GB default, heavy
writes force checkpoints much more often.
Random keys into a large index
each leaf gets one insert or fewer per checkpoint,
so the insert pays for its page alone
change record in the WAL ~100 B
full page image in the WAL; Postgres leaves out
the empty middle, so a ~70% full leaf ≈ 5.7 KB
the page itself, written back whole 8 KB
page splits, spread over the ~40 inserts
between them ≈ 0.5 KB
─────────────────────────────────────────────────────
per 100 B inserted ≈ 14.5 KB
bytes written per byte inserted ≈ 145x
Sequential keys, every insert on the rightmost leaf
entries per page at 90% 8,192 × 0.9 / 100 ≈ 73
change record in the WAL ~100 B
the page, shared by 73 inserts 8,192 / 73 ≈ 112 B
full page image: none for a page born in a
split, at most one per page otherwise ≤ 112 B
─────────────────────────────────────────────────────
per 100 B inserted ≈ 210 to 324 B
bytes written per byte inserted at most ≈ 3x
Same engine, same entry size, about a 50x gap. The
order of the keys decides what a B-tree write costs.The random case holds while each leaf gets fewer than one insert per checkpoint: divide the inserts per checkpoint by the leaf pages. An index bigger than memory is worse again, because each insert first reads its leaf from disk. And every index on a table pays separately.
A B-tree's write cost is the size of a page divided by how many writes share it. Sequential keys share a page among dozens of writes. Random keys into a big index share it among almost none.
How an LSM tree writes
An LSM tree never changes a file once it's written. A write takes two steps, and neither one reads anything from disk. The engine appends the write to its WAL, which Cassandra calls the commit log. Then it inserts the write into the memtable, a sorted structure in memory.
When the memtable reaches its size limit, the engine freezes it and writes it out as an SSTable, short for sorted string table. That's a file of keys in order, split into blocks, with an index of the blocks. It usually also carries a Bloom filter, a small summary that can say a key is not in the file. Cassandra's documentation says an SSTable is never written to again after the flush.
Every flush adds another file, and the files' key ranges overlap, so a read would have to check all of them. Compaction fixes that by merging files into levels.
- Level 0 holds fresh flushes, and its files overlap.
- Each level below is one sorted run: a set of files whose key ranges don't overlap. Each level is about 10 times the size of the one above it. LevelDB, the engine RocksDB was built from, sizes them at 10 MB, 100 MB, 1 GB and so on.
- The last level holds most of the data. With the level sizing RocksDB now recommends, it's about 90 percent.
The idea comes from a 1996 paper by Patrick O'Neil and colleagues. They wanted an index on the history table of TPC-A, a banking benchmark, taking 1,000 inserts a second for random account IDs. As a B-tree, each insert read a leaf and wrote it back, and that one index doubled the disks the system needed. The LSM tree merges entries from memory into the disk part in large multi-page blocks, so each page write carries many entries. The paper put the saving in insert I/O at nearly two orders of magnitude, for workloads where inserts are more common than reads.
Disks have changed since then. On a spinning disk, the cost of a random write was the time the disk arm spent seeking. On flash, a random write is quick, and the cost moved to how many bytes you write and how much space you use. Facebook gave both as its reasons for moving the MySQL database behind its social graph off InnoDB and onto RocksDB, in an engine called MyRocks. It reports that bytes written to flash fell by 75 percent.
Read, write and space amplification
Three ratios describe what an engine costs. RocksDB's tuning guide defines them.
- Write amplification is bytes written to storage divided by bytes written to the database. If you write 10 MB a second and the disk sees 30 MB, it's 3.
- Read amplification is the number of disk reads one query needs.
- Space amplification is the size of the files on disk divided by the size of the data.
The tuning guide also works through a levelled LSM tree to find its write amplification. The method is to follow one byte down the levels and count how many times it's rewritten.
RocksDB's tuning guide example: a 500 GB database,
level 0 and level 1 at 512 MB each, and every level
10 times the size of the one above. Follow one byte.
appended to the WAL 1
flushed from the memtable into level 0 1
level 0 into level 1, the same size 2
level 1 into level 2, merged with ~10x
as many bytes already there 10
level 2 into level 3 10
level 3 into level 4 10
─────────────────────────────────────────────────
write amplification ≈ 34x
(the guide's 33, plus 1 for the WAL)
Space on disk against data
(0.5 + 0.5 + 5 + 51 + 512 GB) / 500 GB = 1.14
Every byte is written about 34 times. That's ten times
the B-tree's 3x on sequential keys and about a quarter
of its 145x on random ones. And the LSM tree writes long
sequential files, where the random B-tree writes one
scattered page per insert.Each level costs about 10 because of how a byte moves down. Compaction picks a file in one level and merges it with every file it overlaps in the next level. The next level holds ten times as much data over the same keys, so any key range has about ten times as many bytes there. All of them are read and rewritten.
You can't keep all three ratios low at once. A 2016 paper by Manos Athanassoulis and colleagues calls this the RUM conjecture, for read, update and memory. It says an access method that sets an upper bound on two of the three costs also sets a lower bound on the third. The paper puts B-trees among the designs built for reads and LSM trees among those built for writes. Inside an LSM tree, the compaction strategy moves the trade. RocksDB's wiki says levelled compaction keeps space low at the cost of read and write amplification, and size-tiered keeps write amplification low at the cost of read and space.
Compaction: levelled, size-tiered and time-window
Compaction is where an LSM tree does the work it put off. There are three common ways to schedule it.
- Levelled. Each level is one sorted run, about 10 times the size of the one above, and a file moves down by merging with the files it overlaps.
- Size-tiered. Files of similar size are merged into one bigger file, and no level is a single sorted run.
- Time-window. Files are grouped by when their data was written, and a window's file is deleted whole once everything in it has expired.
Cassandra's documentation now recommends a fourth, unified compaction (UCS), for new tables, and says it suits both time series and tables with many updates and deletes.
Levelled compaction is the layout behind the 34x above. In Cassandra it uses 160 MB files and levels ten times apart, so a read checks at most one file per level. ScyllaDB's documentation says at most 10 percent of space goes to obsolete rows, and that it does about twice as much I/O on writes. Cassandra's documentation recommends it for read-heavy tables and tables with many updates and deletes, not for write-heavy ones.
Size-tiered compaction is Cassandra's default. It waits for 4 files of similar size and merges them into one. A byte is rewritten once each time its file joins a bigger merge, not about ten times per level, so writes are cheaper; RocksDB built its version, universal compaction, for that. The costs are reads, because a key updated often has versions in many files, and space, because the old files stay on disk until a big merge finishes.
Time-window compaction is built for data written once with a TTL, a time to live after which it expires. While a window is open, its files merge size-tiered style. When it closes, its files merge into one that is never compacted again. Once everything in it has expired and the tombstone grace period, covered under deletes, has passed, Cassandra deletes the whole file. It suggests 20 to 30 windows, such as 3-day windows for a 90-day TTL. Data has to arrive in time order: a write with an old timestamp lands in the current window's file.
Whichever strategy you pick, compaction runs in the background and uses the same disks and CPUs as your queries. Cassandra caps it at 64 MiB a second per node by default. If writes arrive faster than compaction can merge them, files pile up in level 0 and every read has more files to check. RocksDB then protects itself. When level 0 holds too many files, or the bytes waiting for compaction pass a limit, it slows every write. Past a second limit, it stops writes until compaction catches up.
The arithmetic that prevents this fits on one line. RocksDB's guide gives an example: at a write amplification of 50, a disk that does 500 MB a second sustains 10 MB a second of writes. So multiply your write rate by the write amplification, and compare that with the disk bandwidth your reads leave free.
Reads: Bloom filters and range scans
A point read in an LSM tree checks the newest data first. It looks in the memtable, then each level 0 file from newest to oldest, then one file in each level below. It stops at the first copy of the key it finds, because newer versions always sit above older ones.
Without help, that's a disk read for every file. So each SSTable carries a Bloom filter, a small bit array kept in memory that answers either "definitely not in this file" or "maybe in this file". A "no" skips the file. RocksDB's filters use about 10 bits a key to be wrong 1 percent of the time. Bloom filters and probabilistic structures works through a key that doesn't exist, which is where the filters save the most.
A range read can't use a filter built on whole keys. Such a filter answers whether one exact key is in a file, and a range asks about every key between two bounds. So the engine opens a cursor in every sorted run (the memtable, each level 0 file and each level) and merges them in key order as it reads. RocksDB's guide puts the read amplification of a range scan at the number of level 0 files plus the number of non-empty levels. A B-tree finds the first key and walks along the linked leaves.
The question is what reads cost once the LSM has
deferred its sorting. The LSM has 4 files in level 0
and 4 levels below, so 8 sorted runs on disk. Assume
the LSM's filters and block indexes, and the B-tree's
upper levels, are in memory. Data blocks are not.
Point read, key present
B-tree one leaf read 1
LSM filter checks, all in memory 8
false yeses 7 runs × 1% 0.07
the block that holds the key 1
≈ 1.07
Range read: one user's last 50 rows of 100 B
B-tree descend once, read 1 leaf ~1
LSM whole-key filters can't help, so read
a block in every sorted run 8
with filters on the user's prefix or
partition, only the runs that hold it
If compaction falls behind and level 0 holds 20 files
LSM range read 20 + 4 ~24
A point read costs about the same in both. A range
read costs the LSM one block per sorted run, so the
share of your reads that are ranges decides how much
the deferred sorting costs you.The filters and block indexes only help while they're in memory. Facebook's MyRocks paper reports that its filters, at 10 bits a key, raised memory use well above InnoDB's. Skipping the filter on the last level, which holds 90 percent of the data, cut filter memory by 90 percent, and made lookups for missing keys slower.
Deletes and tombstones
A delete has to be recorded somewhere, and both engines remove the old data later. Both pay twice for a row deleted on its own.
In a B-tree engine, a delete marks the row dead and a background process removes it. In Postgres, an UPDATE or DELETE leaves the old row version in place while any transaction might still see it. VACUUM frees dead versions for reuse inside the table but doesn't return the space to the operating system; VACUUM FULL does, by rewriting the table under an exclusive lock. InnoDB removes deleted rows in a background purge, and its documentation warns that purge can fall behind when rows are inserted and deleted at about the same rate.
An LSM tree can't remove a key from a file it never changes, and older versions of the key may sit in any file below. So a delete writes a tombstone: a marker saying the key is gone, which hides every older version beneath it. The 1996 paper called these delete node entries. When compaction merges the tombstone with an older version of the key, it drops that version there. The tombstone itself has to stay until no deeper level could hold the key, often the bottom level, because an even older copy might sit there. Until then, every read that passes over it pays for it.
Cassandra adds a wait. A replica can miss a delete, so Cassandra keeps each tombstone for gc_grace_seconds, 10 days by default. After that, the first compaction that also includes every file holding older data for that partition drops it. Data written with a TTL becomes a tombstone when it expires and stays one for the same grace period. A read keeps every tombstone it passes in memory, so that it can tell the other replicas about the deleted rows. Cassandra logs a warning when one read passes 1,000 tombstones and fails the read at 100,000.
Discord described this in 2017. A channel whose millions of messages had been deleted took 20 seconds to load, because Cassandra had to scan millions of tombstones. Discord cut the tombstone lifespan from 10 days to 2, which its nightly repairs made safe, and changed its queries to skip empty buckets.
The delete that costs almost nothing removes a whole file or partition. The 1996 paper already proposed it: a rule such as "everything older than 20 days" makes the merge drop those entries as it passes them. Time-window compaction deletes expired files whole. A table partitioned by day drops a day at once, which is how the URL shortener removes old click events. In Postgres that's one DROP TABLE of the day's partition, with no vacuum afterwards. Kafka's compacted topics, in message queues vs event streams, use the same marker: a record with a null value is a tombstone for its key.
Which engines use which
| engine | structure | what to know |
|---|---|---|
| Postgres | B-tree indexes over a table of row versions | updates write new versions; vacuum removes old ones; 8 KB pages |
| MySQL InnoDB | the table is a B-tree on its primary key | rows live in the primary key's leaves; 16 KB pages; doublewrite buffer; purge |
| RocksDB | LSM library, embedded in other systems | built from LevelDB; levelled or universal compaction; MyRocks runs MySQL on it |
| Cassandra | LSM, wide-column | commit log, memtables, SSTables; size-tiered by default, unified compaction recommended for new tables |
| ScyllaDB | LSM, wide-column | written in C++, compatible with Cassandra's query language; size-tiered, levelled and time-window, plus an incremental one |
The method
1. Name the reads. Point reads or ranges, and how many reads there are for each write. The 1996 paper built the LSM tree for workloads with more writes than reads.
2. Find the key order of every index. Sequential keys (time, an identity column) or random ones (user IDs, random universally unique identifiers, or UUIDs). Each index has its own order.
3. Count writes per page write. Divide the inserts between checkpoints by the leaf pages of the index. Below one, every B-tree insert pays for a page and most of a page image.
4. Price the writes in bytes. For random keys, a B-tree writes a page plus most of a page image per insert, about 14.5 KB with 8 KB pages. For sequential keys it's at most about 3x. A levelled LSM tree costs about 34x, in long sequential writes. Size-tiered costs less.
5. Price the reads. Point reads cost about one disk read in either engine. A range read costs the LSM tree one block per sorted run, unless its filters cover the key's prefix or partition.
6. Price deletes and space. Rows deleted one at a time cost a write now and a cleanup later in both engines. Expiry by whole file or partition costs almost nothing. Size-tiered needs up to half the disk free.
7. Check compaction keeps up. Write rate × write amplification has to fit inside the disk bandwidth your reads leave free.
Worked examples, start to finish
Each of these four examples breaks a different obvious answer.
An audit log keyed by time, where the slogan picks the wrong engine
An audit log takes 4,000 events a second, 200 bytes each, and keeps them for 90 days. People read it by time range, such as everything between 14:00 and 14:05, and look up single events by their request ID. It's write-heavy, so the slogan says an LSM tree.
The keys say otherwise. In Postgres, partitioned by day, rows go on the end of today's partition, and the primary key (created_at, event_id) grows at its rightmost leaf. The question is whether anything else on the table is random, and the request ID is: it's a random UUID.
The question is what a B-tree writes at 4,000 events
a second, and what one random index adds. Postgres,
partitioned by day: 8 KB pages, and each index built
separately on each day's partition.
Rows, appended to today's partition
rows per page 8,192 / 200 ≈ 40
per insert 200 WAL + 205 page + ≤ 205 image ≈ 610 B
Primary key (created_at, event_id): sequential, ~30 B
per insert 30 WAL + 33 page + ≤ 33 image ≈ 100 B
─────────────────────────────────────────────────────
per event, at most ≈ 710 B
write amplification, at most 710 / 200 ≈ 3.5x
disk writes 4,000 × 710 B ≈ 2.8 MB/s
Index on request_id: a random UUID, ~30 B entries
today's index by evening 4,000 × 86,400 × 30 B ≈ 10 GB
leaf pages at ~70% full ≈ 1.8M
inserts per checkpoint 4,000 × 300 s = 1.2M
about 1.3 per page at midday, 0.7 by evening
per insert, averaged over the day ≈ 6.9 KB
disk writes 4,000 × 6.9 KB ≈ 28 MB/s
rising to ≈ 40 MB/s by evening
─────────────────────────────────────────────────────
The random index writes 10 to 15 times what the
table and its primary key write together.The random index is the problem, not the write rate. At 28 MB a second it writes 2.4 TB a day, more than half of a 4 TB drive, from one index. It also costs in two ways that aren't bytes. Postgres keeps a separate index on each partition, so a lookup by request ID has no day to aim at and probes all 90. And request_id can't be declared unique, because a unique constraint on a partitioned table has to include the partition key.
So make the request ID time-ordered. A version 7 UUID starts with a 48-bit timestamp in milliseconds, and RFC 9562, which defines it, notes the better index locality of time-ordered UUIDs. New IDs land at the right edge of today's index, and the application can read the day out of the ID and query that day's partition, and the next one near midnight.
The decision is Postgres, partitioned by day, with time-ordered request IDs. The write rate is below the 5,000 to 20,000 writes a second that one relational primary handles in choosing a database. Every index now writes at most about 3.5x, and day partitions turn the 90-day expiry into one dropped partition a day.
For a table with time-ordered keys like this one, an LSM tree buys nothing on writes. It would write 4,000 × 230 B × 34 ≈ 31 MB a second, about what the B-tree writes with random IDs and ten times what it writes without them.
Account history keyed by user, where every insert costs a page
A service records what each user does: 30,000 inserts a second on one node, 100 bytes each. The key is (user_id, created_at), so "this user's recent activity" is one short range. Reads are rare next to writes, 500 a second. Events are kept for 4 days, about 1 TB of entries.
Every insert goes to a random user's part of the index. The question is how often two inserts land on the same page before it's written back.
The question is what the writes cost in each engine, and whether the LSM tree's slower range reads matter at 500 reads a second. B-tree, Postgres defaults with max_wal_size raised leaf pages, at least 1 TB / 8 KB ≈ 122M inserts per checkpoint 30,000 × 300 s = 9M inserts per page per checkpoint 9M / 122M ≈ 0.07 → almost every insert pays for a page of its own per insert, from the toolkit ≈ 14.5 KB disk writes 30,000 × 14.5 KB ≈ 435 MB/s per day × 86,400 ≈ 38 TB WAL alone 30,000 × 5.8 KB ≈ 175 MB/s, so the 1 GB max_wal_size default would force a checkpoint about every 3 seconds Levelled LSM, ~34x from the toolkit disk writes 30,000 × 100 B × 34 ≈ 102 MB/s per day × 86,400 ≈ 8.8 TB Reads: one user's last 50 events, one short range B-tree ~1 leaf read 500/s × 1 ≈ 500/s LSM up to 8 runs 500/s × 8 ≈ 4,000/s ───────────────────────────────────────────────────── The B-tree writes 38 TB a day, enough to rewrite a 4 TB drive nine times. The LSM tree writes about a quarter as much and pays up to 8 times the read I/O, 4,000 block reads a second.
The B-tree loses on bytes written, which on flash costs bandwidth and, as RocksDB's guide notes, drive life. The LSM tree's extra reads are cheap because reads are rare, the condition the 1996 paper set out.
So where should 30,000 random inserts a second go, and what do you give up to move them?
Postgres, with an index on (user_id, created_at)
It's the default for good reasons. You keep SQL, transactions and every query you haven't thought of yet, and the team already runs it.
The arithmetic above breaks it. The index takes 9 million inserts between checkpoints across more than 120 million pages, so almost every insert pays for a page and its image: 435 MB a second and 38 TB a day. And at 30,000 writes a second it's above what one relational primary commits, so you'd be sharding it by user as well.
Move the table to an LSM store such as Cassandra
Partition by (user_id, day), cluster by created_at, and give rows a 4-day TTL. The day in the partition key stops any user's partition growing past the 100 MB that partitioning and sharding gives as Cassandra's advice. A read starts with today's partition and steps back a day at a time. Disk writes fall to about 102 MB a second even with levelled compaction, all in long sequential files, and Cassandra spreads the partitions over as many nodes as the rate needs.
This is what most teams would build, and it works. The cost is everything a relational database gave you: joins, multi-row transactions and ad-hoc queries. You also take on compaction tuning, tombstones and repair. The next tier asks whether you needed a different engine to get the batching.
Keep the B-tree, and make it batch: partition by hour
The B-tree pays because 9 million inserts between checkpoints spread over more than 120 million pages. The LSM tree avoids that by writing into something small first. A B-tree gets the same effect if the part being written is small, so partition the table by hour.
The current hour's index grows to 30,000 × 3,600 × 100 B ≈ 11 GB, which stays in memory, with about 1.9 million leaf pages by the end of the hour. By then each page takes about 5 inserts per checkpoint, and more earlier in the hour. Averaged over the hour, an insert costs about 2 KB, about 20x, against 145x for one big index and 34x for the levelled LSM tree. Earlier hours are never written again, and the 4-day expiry is one dropped partition an hour.
What you give up: a read for one user has no hour to aim at. It probes the hourly indexes newest first until it has 50 rows: one for a user active this hour, up to 96 for one idle for four days. That's the LSM tree's cost, one probe per sorted run, moved into SQL. The RUM paper lists partitioned B-trees next to LSM trees among the write-optimised designs. And the write rate is still above one primary, so you still shard by user, as in the naive tier.
Facebook took the other route: it kept MySQL and swapped InnoDB for MyRocks. It reports instances 62.3 percent smaller than compressed InnoDB, partly from also moving from Zlib to Zstandard compression. For a Postgres team, that route means moving to MySQL too.
Profiles rewritten all day, where the default compaction fills the disk
A Cassandra cluster stores user profiles, each as one serialized 1 KB blob, and the app rewrites the whole blob whenever a profile changes, including its last-seen time. Each node holds 50 GB of live profiles, 50 million of them, and takes about 6,000 rewrites a second. So each profile is rewritten about 10 times a day. Cassandra's default compaction is size-tiered, which favours writes, so the default looks right.
In an LSM tree, a rewrite adds a new version of the key and leaves the old one where it is. Size-tiered compaction merges files of similar size, so one profile's versions end up spread across every tier, until a merge happens to include all of them.
The question is how much disk and compaction each
strategy needs when every profile is rewritten about
10 times a day. Per node: 50 GB live, 6,000 rewrites
a second of 1 KB.
Disk
size-tiered: ScyllaDB says overwrite-heavy data can
need 5 times the space, counting the merge output
50 GB × 5 = 250 GB
levelled: at most ~10% obsolete rows, plus about
10 files of 160 MB for a merge in progress
50 GB × 1.1 + 1.6 GB ≈ 57 GB
Compaction writes
data written 6,000 × 1 KB = 6 MB/s
levelled, up to ~34x from the toolkit ≈ 200 MB/s
size-tiered, about half of that ≈ 100 MB/s
Cassandra's default compaction cap 64 MiB/s
─────────────────────────────────────────────────────
Size-tiered needs about 4 times the disk. Levelled
needs about twice the compaction writes, and both
are above the default cap.The decision is levelled compaction, with the compaction cap raised to about 200 MB a second. Step 7 of the method checks that, plus the reads compaction does, against what the disks have left after queries. If it fits, levelled is the cheaper side: its cost is bandwidth while compaction runs, and size-tiered's extra 190 GB of disk has to be provisioned all the time.
A work queue, where every delete is a write
A team builds a job queue in Cassandra. Each queue shard is one partition, clustered by the time a job was added. A shard takes 200 jobs a second of about 1 KB each. A worker deletes each job when it's done, and workers read from the start of the partition to find the next job.
The question is how long it takes before reads start to fail. Each finished job leaves a tombstone, and each tombstone stays for gc_grace_seconds, 10 days. tombstones per shard 200/s × 864,000 s = 172.8M A read from the start of the partition passes them all warning at 1,000 1,000 / 200/s = 5 s failure at 100,000 100,000 / 200/s = 500 s ───────────────────────────────────────────────────── About 8 minutes after launch, every read of the queue head fails. It doesn't recover while jobs keep finishing, because the shard always holds 10 days of tombstones, 172.8M of them.
In a relational table, every finished job is a dead row instead, which is the load where InnoDB's purge falls behind. The WhatsApp spool is a queue like this one, at 25 billion messages a day, and it can't delete either.
So how does a finished job stop costing anything, when every way of removing a row is itself a write?
Delete each job when it's done
It's what a queue does, and in a relational database at low volume it works. Here, the arithmetic above breaks it: reads of the queue head fail 8 minutes after launch and stay failing. Lowering gc_grace_seconds doesn't rescue it. Even at one hour, a shard holds 200 × 3,600 = 720,000 tombstones, seven times the failure threshold. And with explicit deletes, a shorter grace period means a node can be away for less time before deleted jobs come back and run twice.
Stop deleting: a TTL on each job, and a cursor
Write each job with a TTL and never delete it. Each worker stores a cursor, the position of the last job it finished, and reads from there. The read starts past every finished job, so it never walks over their tombstones, and nobody writes a delete.
This removes the read failures. The cost is that expired jobs still turn into tombstones and stay on disk for the 10-day grace period. So one shard's partition holds the TTL plus 10 days of jobs: with a 1-day TTL, up to 200 KB a second × 950,400 s ≈ 190 GB. Partitioning and sharding gives Cassandra's advice to keep a partition under 100 MB.
Lay the data out so whole files expire together
Stop removing rows one at a time, by any method. Add a time bucket to the partition key, (shard, minute), which keeps each partition near 200 KB × 60 s = 12 MB. Give every job in the table the same TTL, and use time-window compaction with 1-hour windows. Each hour's jobs are written into that hour's files and never touched again, and workers read only the current minute's partition, from their cursor.
Cassandra deletes a window's file without reading it, but only once everything in it has expired and the grace period has passed. At the default, that's the TTL plus 10 days on disk. Nothing in this table is ever deleted, only expired, and every replica stores the TTL with the row and expires it at the same moment. The grace period protects nothing here, so set gc_grace_seconds low on this table, for example an hour.
A finished job now costs no write and no read to remove. In Postgres the same move is a table partitioned by time, where cleanup is one dropped partition.
What you give up: every row needs the same TTL, or a long-lived row keeps its file alive. Writes have to arrive in time order, because an old timestamp or a read repair mixes windows and blocks the drop. A job unfinished when its minute ends needs a retry path. And the TTL sets the window size, since Cassandra suggests 20 to 30 windows: 1-hour windows suit about a day of jobs.
Where it goes wrong
These are ordered by how often they come up.
1. Choosing the engine by the slogan
"LSM trees for writes" picks an engine before anyone has asked about key order. Sequential keys make a B-tree write cost at most about 3x, and no LSM tree goes below about 2x. Random keys into a large index cost a B-tree about 145x. Find the key order of every index first.
2. Treating compaction as free
The work an LSM tree puts off comes back as background I/O on the disks your queries use. If write rate × write amplification doesn't fit in the spare bandwidth, reads slow down first and then writes are throttled.
3. Deleting rows one at a time
In both engines a deleted row is written once and cleaned up later, by vacuum, purge or compaction. Cassandra adds a 10-day wait and fails reads that pass 100,000 tombstones, so a queue that deletes its jobs fails within minutes. Design expiry as dropping a file or a partition.
4. Benchmarking an empty database
A new LSM tree has no lower levels, so compaction has little to rewrite, and a new B-tree fits in memory. Both look faster than they will in production. Load production-sized data and keep writing until compaction settles before you measure either.
5. Lowering gc_grace_seconds to get rid of tombstones
It's safe on a table whose rows only ever expire. With explicit deletes, it shortens how long a node can be away before deleted rows come back, which is why Discord paired its cut to 2 days with nightly repairs.
If you memorise nothing else
| fact | why it matters |
|---|---|
| A B-tree sorts on every write; an LSM tree sorts later, in compaction | the work is moved, not avoided |
| A B-tree writes a whole page, however small the change | 8 KB in Postgres, 16 KB in InnoDB |
| A random 100 B insert into a large Postgres index writes ~14.5 KB | about 145x: a page and its image |
| A sequential insert writes at most ~3x | dozens of inserts share each page |
| A levelled LSM tree writes each byte about 34 times | WAL, flush, then about 10 per level |
| Levelled space is about 1.1x; size-tiered can need 5x on overwrites | old versions sit in every tier |
| A point read is about one disk read in either engine | Bloom filters at ~10 bits a key skip most LSM files |
| A range read costs an LSM tree one block per sorted run | unless its filters cover the prefix or partition |
| A tombstone drops the old value at the first merge that meets it | the tombstone stays until nothing below could hold the key |
| Cassandra keeps tombstones and expired rows 10 days by default | and a whole-file drop waits for it too |
| Partitioning a B-tree by time gives it the LSM's batching | and the LSM's read cost, one probe per partition |
What is expected at each level
Mid-level
Know how each engine writes. A B-tree changes a page and splits it when it's full. An LSM tree appends to a log, buffers in a memtable, flushes sorted files and merges them with compaction. Know that Bloom filters let a point read skip files, and that an LSM delete is a tombstone.
You aren't expected to work out write amplification or choose a compaction strategy unprompted. If asked "what does compaction cost?", a good mid-level answer is "it rewrites data in the background, so it uses disk bandwidth the queries also need."
Senior
You lead this part of the discussion, and three things must come up without prompting.
The key order and the index size, before the engine. Sequential keys make a B-tree cheap. Random keys into a large index make every insert cost a page. This is the number that decides the engine, not "write-heavy".
Compaction as a budget. Give write rate times write amplification, and compare it with the disk. Say what fails first when it doesn't fit: reads slow down, then writes are throttled.
Deletes as writes. Name tombstones or vacuum, and design expiry as dropping files or partitions.
Two things are disqualifying: claiming an LSM tree is faster without mentioning compaction or range reads, and a Cassandra design that deletes rows one at a time on a hot path.
Staff+
Producing a correct design is still the bar. A staff answer stands out by choosing where the work goes, with numbers.
Price write amplification both ways, in bytes a day on flash: a page and its image per random B-tree insert, against about 34x in sequential files.
Choose the compaction strategy from the workload. Levelled for overwrites and range reads, size-tiered for write-heavy data with spare disk, time-window for data that expires. Know that Cassandra now recommends unified compaction for new tables.
Get the batching without moving everything. Partition a B-tree by time, or swap the engine under the same SQL as Facebook did with MyRocks. Say what each costs on reads.
Make expiry free. Lay data out by time so whole files or partitions expire together, with one TTL per table and a short grace period where nothing is deleted.
And say what you would not build. Each of these can cost a team a quarter.
One is a Cassandra cluster for 4,000 time-ordered writes a second, which one Postgres primary absorbs. Another is vacuum or purge tuning on a queue table that should have been partitioned by time. A third is Bloom filter tuning for reads that are mostly ranges across partitions.
Another is size-tiered compaction on an overwrite-heavy table, then a disk expansion project. And the last is a move off Postgres for write speed, when the index that hurt was one random index whose keys could have been time-ordered.
Sources
The engine figures come from these papers, manuals and source files. The bytes-per-insert, compaction and tombstone arithmetic is worked out in the article from the defaults they document.
- The Log-Structured Merge-Tree (LSM-Tree), O'Neil, Cheng, Gawlick and O'Neil, Acta Informatica 1996. The TPC-A history index, multi-page batching, delete node entries and dropping old entries during the merge.
- Designing Access Methods: The RUM Conjecture, Athanassoulis et al., EDBT 2016. The three overheads, and partitioned B-trees beside LSM trees as write-optimised.
- MyRocks: LSM-Tree Database Storage Engine Serving Facebook's Social Graph, Matsunobu, Dong and Lee, PVLDB 2020. InnoDB's whole-page writes, the 75 percent and 62.3 percent results, prefix filters, filter memory and tombstones.
- The RocksDB wiki: the tuning guide for the amplification definitions, the 33x example and the compaction budget, and levelled, universal and compaction styles, Bloom filters and write stalls. LevelDB's implementation notes for level sizes and dropping tombstones.
- The Apache Cassandra documentation on the storage engine, compaction and tombstones, the size-tiered, levelled and time-window strategies, and the configuration defaults. Cassandra's CompactionController for when an expired file is dropped. ScyllaDB's compaction strategies for the space figures.
- The PostgreSQL documentation on pages, B-tree splits, fillfactor, full page writes and max_wal_size, checkpoints, partitioning and vacuum, and xloginsert.c for page images without the empty middle. The MySQL documentation on InnoDB page fill, the clustered index, the doublewrite buffer and purge.
- RFC 9562, for version 7 UUIDs and their index locality.
- How Discord Stores Billions of Messages, Discord, 2017. The 20-second load over millions of tombstones, and the cut to 2 days.