SkillSprout
Chapter 15 · Lesson 3
Intermediate
4 min read

LSM-Trees, Hash Indexes and Full-Text Search

Overview

B-trees are not the only way to index. Some workloads are mostly writes, some are pure key lookups, and some are text searches. Each has an index structure built for it.

This lesson gives a quick tour of the three alternatives and when to pick each.

Detailed Notes

What are these other indexes

  • LSM-tree: a write-friendly structure that buffers writes in memory and saves them to disk as sorted files.
  • Hash index: an index that jumps straight to a key using a hash function.
  • Inverted index: an index from each word to the documents that contain it, used for text search.

The problem it solves

A B-tree updates pages in place on disk. That is random disk work, and it becomes the bottleneck when you write huge volumes of data (logs, sensor readings, messages). Other workloads need something different: only exact key lookups, or searching words inside text, which a B-tree cannot do well.

How it works

LSM-tree (Log-Structured Merge-tree)

  1. Every write is appended to a log file for safety.
  2. The write is also put into a sorted in-memory table called the memtable.
  3. When the memtable is full, it is saved to disk as a sorted, never-modified file called an SSTable.
  4. Reads check the memtable first, then the SSTables from newest to oldest.
  5. In the background, compaction merges SSTables and throws away old or deleted values so the file count stays small.

Writes are fast because they are sequential appends. Reads may check several files, so they can be slower, and compaction rewrites data in the background. Used by Cassandra, RocksDB, LevelDB and HBase.

Hash index. The key is hashed to find a bucket directly, so exact lookups are about as fast as possible. But hashing destroys order, so there are no range queries or sorting. Good for key-value stores and in-memory lookups.

Inverted index. A document naturally "contains" its words. An inverted index flips this: for each word it stores the list of documents containing it. Searching "red shoes" means getting the list for "red" and the list for "shoes" and intersecting them. Search tools first clean the text (lowercase it, split into words, drop endings) and then rank results by relevance. A plain LIKE '%shoes%' cannot use a B-tree and scans everything, which is why real search uses inverted indexes (Elasticsearch, OpenSearch, Lucene).

B-treeLSM-treeHashInverted
Best forBalanced reads and rangesHeavy writesExact key lookupSearching words
Range queriesYesYesNoNo
Write speedModerateVery fastFastSlower to update

A simple example

A B-tree is a neatly organised filing cabinet: easy to find things, but filing each new paper takes care. An LSM-tree is a "to file" tray: you drop papers in quickly, and every so often someone sorts the tray into the cabinet. An inverted index is the glossary at the back of a book.

System design example

A social app stores user profiles and posts in a relational database (B-tree indexes). It stores billions of activity events in a Cassandra-style store (LSM) because writes are constant. Its search box runs on Elasticsearch (inverted index). The database stays the source of truth, and changes are copied into the search index shortly after, so search results may lag a few seconds.

Summary

  • LSM-trees favour heavy writes by appending sequentially and merging later.
  • Hash indexes give very fast exact lookups but no ranges.
  • Inverted indexes map words to documents and power text search.
  • B-trees remain the balanced default for relational data.
  • Pick the structure that matches the workload.

Architecture Diagram

Database to search index pipeline
Architecture
Primary DBsource of truth
Indexer
Search indexinverted index
Search users

The database stays the source of truth; the search index serves queries.

  1. 1Content is saved in the primary database.
  2. 2Changes are sent to an indexer that splits the text into words.
  3. 3Words are written into the search index.
  4. 4User queries read the search index and results are ranked.

Common Interview Questions

Chapter quiz

Indexes · 5 questions

  1. 1.What does a full table scan do?

  2. 2.An index on (user_id, created_at) will NOT help which query?

  3. 3.What is the main cost of adding more indexes?

  4. 4.Which structure is best for a very write-heavy workload?

  5. 5.Which index powers searching for words inside text?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course