There's no need to explain to you what a DB index is and why it is needed, so I'll go straight to the point. The two main kinds of index implementations are B-Trees and LSM-Trees. Why not just use a HashMap? The answer to this is pretty simple: they only work if your keyset fits in memory (which it doesn't if you're doing any kind of serious work) and can't do range queries.
B-Trees
B-Trees are the most common data structure used for indexing in databases. Well, if we are to be specific, they are B+ trees, but I will continue calling them B-Trees. If you are familiar with how a B-Tree works, I'm sure you can imagine how they're used in databases. The easiest way to explain this is to show you an example.
In a B-Tree node, you have your values and between your values you have references to other nodes. So if you're looking for a value between 40 and 60, you will follow the reference in the root node that sits between these values. Then if you're looking for 35, you need to follow the reference sitting before 40 in that node and that will take you to a leaf node. The leaves contain the actual values and are generally connected to each other in a linked list.
So due to their nature, B-Trees enable very simple lookups for range queries. Besides that, you can also do stuff like prefix searches on strings ("LIKE 'something%'") because strings are lexicographically sorted by their prefix.
The values in a B-Tree don't necessarily have to be numeric, they just need to be comparable. So they can be strings, dates, pretty much anything. Something to keep in mind is that the size of the key can affect the shape of the tree. A large key will make the tree taller (we'll see how), which makes traversing the tree a bit slower.
Before going any further, I need to clarify something about how databases store data. Your hardware, your OS, and the DB all have a minimum addressable unit of storage. In the case of a DB, this unit of storage is called a page and is typically a multiple of the filesystem block size. That is 8 KB in PostgreSQL and SQL Server or 16 KB in MySQL's InnoDB. A page stores multiple rows (or, in an index, a B-Tree node). You can only fetch a row from disk by fetching the entire page it sits in.
B-Trees generally have a high branching factor, which means that each node can point to a lot of child nodes (depending on how many keys fit inside a single page). Going back to key sizes: since a page has a fixed size, larger keys mean fewer keys fit in a single page, lowering the branching factor and making the tree taller. Still, a branching factor of several hundreds means that you'll probably only need 3-4 layers of tree to store your entire DB. And because the root node and the upper layers are accessed all the time, the DB keeps them cached in RAM, meaning a lookup usually only takes 1-2 actual disk seeks to reach the leaf page.
What will you find at a leaf in a database? It depends. You may have two types of indexes:
- A clustered index (where the leaf contains the actual DB rows). This is good for really fast reads. So, in this case, you could say that the whole table is stored inside the index. Because the rows can only be physically sorted in one order on disk, you may only have one clustered index per table.
- A non-clustered index: where the leaf contains a reference to the rows. A reference to what depends on the DB engine. If the table has a clustered index, then the non-clustered index will contain a reference to the clustered keys (the primary keys). If there's no clustered index, then it contains a reference to the physical row location on disk.
Whether a table even uses a clustered index comes down to the DB engine. For instance, MySQL's InnoDB always stores the table as a clustered index on the primary key, while in PostgreSQL all indexes (even the primary key) are non-clustered indexes that point to rows in the table heap. An interesting detail about clustered indexes is that they are very quick to traverse. Since rows are stored physically next to each other based on the value of the index keys, you need to fetch way fewer pages (because a page will contain adjacent rows).
Updating a non-indexed value in a B-Tree leaf requires finding the page on disk and updating it in place (whereas updating an indexed key itself means deleting the old entry and inserting the new one at its sorted position). Inserting into a B-Tree can be a bit trickier. Normally, you just need to find the correct page and write the value into that page. But what do you do if your page is already full? What happens is that your page is split into two half-full pages and the parent node is updated to correctly point to both of them. If this happens at the root node, a new root is created and the tree becomes taller. Deleting an entry involves finding the entry on disk and removing it. If we keep deleting entries in pages, they may start to develop "gaps" (sections in the page that are empty). These gaps are just wasted space. This process is called B-Tree fragmentation and is generally handled by background processes that do some cleaning up.


During a transaction, you may need to write to a lot of different pages that may be far apart on disk. This pattern is called random I/O and is pretty expensive due to hardware limitations on both HDD and SSD. Besides that, during a page split, you need to update multiple pages one after another. What happens if your machine or DB process crashes in between those updates? These performance concerns, coupled with the durability concerns, lead to the need for a WAL (Write-Ahead Log). This WAL is just a sequential log (log as in sequential list of data) that is written to disk before any B-Tree modifications take place. So the DB first writes to the WAL, then makes the B-Tree modifications. If the process crashes, the modifications are safely persisted on disk in the WAL and we can recover gracefully. Another type of optimization is to write to the WAL, and only update the B-Tree in memory on a transaction commit. This avoids the random I/O overhead on commit and a background process will handle that later.
LSM-Trees
LSM-Trees (Log-Structured Merge-Trees) stem from a very simple idea: the simplest way of persisting stuff on disk is to just append it to a log.
This solves writes, but then how would we retrieve that data? As we established earlier, a simple HashMap won't cut it because it's not feasible to keep the whole database in memory and it would make range queries a nightmare.
So intuition tells us that we need something that keeps the data sorted. That sorted structure is an SSTable (Sorted Strings Table). It is a log of strings that is sorted and immutable, and structured in blocks. A block typically spans a few kilobytes. A table is stored alongside it, keeping the first key of each block, alongside the block's starting position (offset) in the file.
There's no pretty way to construct this kind of file while writes are coming in, so what DB engines do is to keep an in-memory ordered map, called a memtable (for instance a skip list or red-black tree). When the in-memory structure reaches a certain size, it gets flushed to disk as an SSTable. This new SSTable file is called a segment and is immutable. It is appended to a sequence of segments.
What happens if the machine crashes before the memtable is flushed to disk? Just like with B-Trees, LSM-Trees also use an append-only WAL on disk. Every write is first appended to the WAL before being added to the memtable, so if the DB crashes, the memtable can simply be rebuilt by replaying the log. Once a memtable is flushed to an SSTable, its corresponding WAL can be discarded.
Updating an entry in your DB just means adding the new value into your memtable. Once flushed to disk, you now have two copies of that key in your DB, but the more recent one is the one that will be naturally chosen. Eventually, during compaction, the older entry will be completely removed from the DB. Deleting an entry is similar, you just add a tombstone value that tells your DB engine this value is deleted.
From time to time, a compaction process happens, merging segments together and discarding overwritten or deleted values.
The advantage of LSM-Trees is that they greatly reduce random I/O during writes by only writing sequentially to disk.
One tradeoff to keep in mind for both structures is write amplification, where a single logical write to the DB results in multiple physical writes to disk over time. In B-Trees, this happens because modifying a single row requires writing to the WAL and rewriting an entire 8 KB or 16 KB page (or multiple pages during a split). In LSM-Trees, it happens because data is written first to the WAL, flushed once as an SSTable, and then repeatedly read and rewritten as segments get merged during background compaction.
If you want to read a key, you first check the memtable, then you check the latest segment, then the one before it and so on. To make this quicker, each segment typically also contains a Bloom Filter.
Performing a range query usually means reading from multiple segments at the same time in order.
This type of storage is popularized by systems like Cassandra, RocksDB, LevelDB, and ScyllaDB.
Head-to-Head: B-Trees vs. LSM-Trees
As a rule of thumb, LSM-Trees are typically faster for writes, while B-Trees are faster for reads.
Reads are slower on LSM-Trees because you have to check multiple data structures and SSTables at different stages of compaction, rather than walking down a single unified tree.
Here is the quick cheat sheet of how they stack up:
| Dimension | B-Trees | LSM-Trees |
|---|---|---|
| Core Philosophy | Update in place (mutable pages) | Append and merge (immutable segments) |
| Write I/O Pattern | Random I/O (plus sequential WAL) | Sequential I/O (WAL + SSTable flushes) |
| Read Performance | Fast & predictable (1–2 disk seeks via tree traversal) | Slower (may check memtable + multiple SSTables, mitigated by Bloom filters) |
| Write Performance | Slower on random keys (page splits, rewriting whole pages) | Very fast (buffered in memory, flushed sequentially) |
| Tail Latency (p99) | Predictable (work is done upfront) | Spiky under heavy load (background compaction competes with reads/writes for disk I/O and CPU) |
| Disk Space Usage | Higher (page fragmentation leaves unused space in half-full pages) | Lower (immutable, sequentially written files have no fragmentation and compress very well) |
| Key Uniqueness & Locking | Simple (each key lives in exactly one leaf page, making uniqueness checks and range locks cheap) | Harder (a key can exist across multiple segments, so checking uniqueness requires reading them first) |
| Popular Examples | PostgreSQL, MySQL (InnoDB), SQLite, Oracle | Cassandra, RocksDB, ScyllaDB, LevelDB |
My Thoughts
Why should you care about this? Well, indexes are leaky abstractions and knowing how they work under the hood can help you understand what data access patterns work and don't work for your app. Besides that, they play a big role in the decision regarding which DB engine to pick depending on your workload.
This article is partly based on the excellent chapter from "Designing Data-Intensive Applications" by Martin Kleppmann, so go check it out if you want to go into more detail.