Guide

Choosing a storage engine

Rule of thumb: LSM suits heavy writes, B-trees suit reads. Test with your own workload; some engines mix both.

Reads and range queries

A B-tree reads one page per level, so reads are fast and steady. An LSM read may check several files, but Bloom filters skip most of them. Range queries are easy on a B-tree. On LSM they must read every file and merge the results, and Bloom filters can't help. If writes outpace flushing, the memtable fills and engines like RocksDB pause reads and writes until it is flushed.

Sequential vs random writes

B-tree writes land on whatever page holds the key, so they are scattered. LSM engines write whole files at once. Spinning disks are far slower at scattered writes, because the head must move. SSDs write in small pages but erase only in big blocks, so before erasing they copy valid pages away. Scattered writes cause more copying and wear.

Write amplification

Write amplification is bytes written to disk divided by bytes a plain log would need. LSM writes a value to the log, at flush, and at every compaction. A B-tree writes the WAL and the page, sometimes a whole page. LSM is usually lower. Benchmark long enough for compaction to start.

Disk space, deletes, snapshots

Deleted keys leave empty pages in a B-tree that need a cleanup process. LSM files have no half-empty pages and compress better. But a deleted value can linger in old files for a long time, which matters for privacy laws. An LSM snapshot just keeps the current files from being deleted.

In-memory and embedded databases

An in-memory database keeps all data in RAM and can stay durable with a log, snapshots, or replicas. It is fast mainly because it skips encoding data for disk. RAM also makes rich structures, like sets and priority queues, easy to offer. An embedded database runs as a library inside your app.

Remember

  • Rules of thumb, not laws: test your workload.
  • Big sequential writes beat scattered ones.
  • Deletes linger in LSM; snapshots are cheap.
  • In-memory speed comes from skipping disk formats.