Bugpit

Lessons › Core concepts

Built for writes

Delivery vans send their location every few seconds: 20,000 req/s, and 90% are new readings. Your Postgres primary is the biggest you can buy, and it's drowning.

Play this level

Free with an account. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

Most relational databases store data in a B-tree. Reads are fast, and every write updates pages in place across the disk, which gets expensive when writes never stop.

An LSM tree, used by Cassandra, RocksDB and time-series stores, writes to memory first and then saves sorted files in big sequential runs. Writes become cheap, and background compaction merges the files later.

The trade is on reads. A read may check several files, so LSM stores use bloom filters to skip files that can't hold the key. Pick the engine for the work: mostly reads, a B-tree; a flood of writes, an LSM tree.

What you have to achieve

20,000 requests a second at the busiest hour, 90% of them writes.

What you learn

You stopped forcing a read-optimised engine to take a flood of writes. A store built on an LSM tree takes them as quick sequential runs, so the same work needs far fewer machines.

Check yourself

Why do steady writes hurt a B-tree database?

Show the answer

Each write updates pages in place, all over the disk. Updating pages in place means many small scattered writes. That's fine for mostly reads, and costly when writes never stop.

How does an LSM tree make writes cheap?

Show the answer

It collects them in memory, then saves sorted files in big runs. Big sequential writes are much faster than many small random ones. Compaction tidies the files up later, in the background.

What does a bloom filter do in an LSM store?

Show the answer

Tells a read which files can't hold the key, so it skips them. A read might otherwise open every file. The filter answers "definitely not here" cheaply, so most files are never read.

Try it yourself

More in Core concepts

← Pick the right key Who's in charge? →