On this page
- One question per structure
- B-Tree: sorted pages you update in place
- LSM tree: write fast now, merge later
- Bloom filter: "definitely not" or "maybe"
- Trie and FST: share the prefixes (and suffixes) (members only)
- Inverted index: from words to documents (members only)
- Bitmaps and Roaring: sets as bits (members only)
- Geo indexes: geohash, quadtree, S2 and H3 (members only)
- HNSW: finding "similar" fast (members only)
- Picking one: the trade-offs side by side (members only)
- How to say it in the HLD interview (members only)
With about 10 bits per key, a Bloom filter can answer "is this key here?" and be wrong only about 1% of the time, without storing the keys at all. RocksDB keeps one next to every data file so it can skip disk reads it doesn't need. That's the pattern of this whole blog: a small, clever structure that answers one question fast by giving something else up.
Where this fits: Part of the Search & storage series. Every stop on a query's journey that answers "where is it?" quickly uses one of these structures. This blog gives you a picture of each and where it runs in real systems.
Before you start: Search, end to end, and Disk I/O and durability for why random disk reads hurt, which is what most of these structures avoid.
After reading this you will be able to
- Match a query shape (point, range, prefix, contains, nearby, similar) to the structure that serves it.
- Sketch a B-Tree, an LSM tree and a Bloom filter, and say what each costs.
- Explain how an inverted index, FST and Roaring bitmap make text search and filters fast.
- Compare geohash, quadtree, S2 and H3, and explain why Uber uses hexagons.
- Explain HNSW well enough to say why vector search is approximate.
We'll cover eight structures at the "picture plus where it's used" level, about one screen each. The papers linked below hold the implementation details.
01One question per structure
Think of a toolbox. A phone book is sorted, so you can flip to a name (B-Tree). An inbox you sort later takes mail fast (LSM tree). A bouncer can say "definitely not on the list" (Bloom filter). A dictionary has thumb tabs for each letter (trie). A book index maps words to pages (inverted index). A seating chart shows filled seats at a glance (bitmap). A map grid answers "what's near here?" (geo index), and a good shop assistant finds "something like this" (HNSW).
A few words first:
- Point lookup: find one exact key. Range scan: find every key between X and Y. Prefix: every key starting with some letters.
- False positive: the structure says "maybe yes" when the answer is no.
- Exact vs approximate: an exact structure never misses; an approximate one trades a little accuracy for speed or space.
| Structure | Question it answers | Exact? | Used in |
|---|---|---|---|
| B-Tree | This key, or keys from X to Y | Exact | Postgres, SQLite |
| LSM tree | Take a flood of writes | Exact | RocksDB, Bigtable |
| Bloom filter | Could this key be here? | Approximate (no false negatives) | RocksDB, Bigtable |
| Trie / FST | Keys with this prefix | Exact | Lucene terms index |
| Inverted index | Docs containing this word | Exact | Lucene, Postgres GIN |
| Bitmap / Roaring | Which items match this filter? | Exact | Lucene filter cache, Druid |
| Geo index | What's near this point? | Exact, by cell | Redis, S2, H3 |
| HNSW | What's most similar to this? | Approximate | pgvector, Elasticsearch |
02B-Tree: sorted pages you update in place
Answers: "find this key", or "every key between X and Y". A B-Tree is a wide, shallow tree of fixed-size pages. Inner pages hold separator keys that say which child to visit; leaves hold the sorted keys and pointers to rows. When a leaf fills up, it splits in two.
A B-Tree: a root page, inner pages and linked, sorted leaves
It's the default index type in Postgres, which also deduplicates repeated keys, and a whole SQLite database file is B-tree pages. The catch: updates rewrite pages in place, which means random writes.
03LSM tree: write fast now, merge later
Answers: "take a flood of writes without random I/O". Per the RocksDB Overview, writes go to a write-ahead log and an in-memory, sorted memtable. A full memtable is flushed to an immutable sorted file (an SST) at level 0. Background compaction merges files into bigger levels below.
An LSM tree: write to memory, flush immutable files, merge them down
The idea comes from the 1996 LSM-tree paper by O'Neil and colleagues. Google's Bigtable runs the same shape with a memtable, SSTables, and minor and merging compactions. The catch: a read may have to check several files, and compaction competes for disk. Bloom filters fix the first problem.
Seen this before?
Immutable files plus merging is everywhere. The B-Tree vs LSM trade-off for reads and writes is in Reads and writes, and Lucene segments merge the same way in How search engines work.
04Bloom filter: "definitely not" or "maybe"
Bloom filter Definition
A bit array plus a few hash functions that answers "could this key be in the set?" It can say "maybe" when the answer is no (a false positive), but it never says "no" when the answer is yes.
Burton Bloom described it in a 1970 paper about trading a small error rate for much less space. Here's how it works:
- To add a key, run it through k hash functions and set those k bits to 1.
- To check a key, look at the same k bits.
- If any bit is 0, the key was never added. If all are 1, it might have been.
Try this first
"cat" set bits 2, 5 and 9. Now "dog" hashes to bits 2, 5 and 7. Is "dog" in the set? (No. Bit 7 is still 0, and one zero is enough.)
A Bloom filter: one zero bit means definitely not
The RocksDB Bloom filter wiki gives real numbers. RocksDB keeps one filter per SST file:
| Bits per key | False-positive rate |
|---|---|
| 4.9 | about 10% |
| 9.9 | about 1% |
| 15.5 | about 0.1% |
Bigtable offers the same as an option per SSTable. The catch: you can't list or delete keys, and it only ever says "maybe".
Seen this before?
The Bloom filter on the LSM read path is in Reads and writes.
Members-only article
Keep reading with the Article Pass
You've read the free part. The other 7 sections of this guide, with every diagram and worked example, are for members. One Article Pass unlocks every members-only article on Engineering Atlas: the ones live today and every one I publish while your pass is active.
- Trie and FST: share the prefixes (and suffixes)
- Inverted index: from words to documents
- Bitmaps and Roaring: sets as bits
- Geo indexes: geohash, quadtree, S2 and H3
- HNSW: finding "similar" fast
- Picking one: the trade-offs side by side
- How to say it in the HLD interview
From ₹49 · one-time payment, no auto-renewal · separate from course passes
Interview practice