LSM Tree

An SSTable is sorted and never changes after the engine writes it. Writes, in contrast, arrive one at a time in any key order. A log-structured merge tree (LSM tree) is the storage engine that turns this stream of writes into SSTables. It buffers writes in memory, writes each full buffer to disk as a new SSTable, and merges the SSTables in the background. Every disk write in this design is sequential. Patrick O'Neil and his co-authors introduced the LSM tree in 1996 (LSM-tree paper), and RocksDB, LevelDB, and Cassandra use variants of it.

The Write Path: WAL and Memtable

Before the engine answers a write, it performs two steps:

  1. It appends the record to the write-ahead log (WAL), an append-only file on disk. This is a sequential write.
  2. It inserts the key and value into the memtable, a sorted structure in memory. RocksDB uses a skip list by default (RocksDB MemTable wiki).

Because the memtable keeps keys in order, the engine can later write it out as a sorted file. The WAL protects against crashes. Memory is lost when the process dies, so the engine rebuilds the memtable on restart by replaying the WAL.

Write-ahead log (WAL). A WAL record holds the operation, the key, and the value, such as PUT 17 v2 or DELETE 42. The engine appends records in the order the writes arrive. If a write asks for a sync, the engine forces the record to disk before answering. Without a sync, a machine crash can lose the last few writes; RocksDB leaves sync off by default (RocksDB docs).

Flushing the Memtable

When the memtable reaches its size limit, the engine flushes it. The full memtable becomes read-only while a new empty memtable takes incoming writes. In the background, a thread writes the read-only memtable to disk as a new SSTable. Because the thread walks the memtable from the smallest key to the largest, it writes the file in one sequential pass. Once the SSTable is on disk, the engine no longer needs the WAL for that memtable and deletes it.

RocksDB's default memtable size is 64 MB (RocksDB tuning guide). At about 100 bytes per entry, one memtable absorbs up to about 64 × 1,048,576 ÷ 100 ≈ 670,000 writes. The flush stores these entries with a single sequential write of up to roughly 64 MB. The memtable's own overhead and block compression make the resulting file smaller. A B-tree receiving the same writes in random key order would update pages all over the index. In the worst case, where each write lands on a different page, that takes 670,000 page writes at scattered positions.

Updates and Deletes

Because SSTables never change, an update cannot overwrite the old value. Instead, the engine writes the key again with the new value. This new entry lands in the memtable and later flushes to a newer SSTable, while the old value stays in its older file.

A delete writes a tombstone, a marker indicating that the key is deleted. Because a read meets the tombstone first, the marker hides every older value of the key. In the sandbox below, delete 42 writes a tombstone while 42 = v1 still sits in an older file.

Old values and tombstones take up disk space until a merge removes them. That merge is called compaction, which comes after the read path.

Grasping the building blocks ("the lego pieces")

This part of the guide will focus on the various components that are often used to construct a system (the building blocks), and the design templates that provide a framework for structuring these blocks.

Core Building blocks

At the bare minimum you should know the core building blocks of system design

  • Scaling stateless services with load balancing
  • Scaling database reads with replication and caching
  • Scaling database writes with partition (aka sharding)
  • Scaling data flow with message queues

System Design Template

With these building blocks, you will be able to apply our template to solve many system design problems. We will dive into the details in the Design Template section. Here’s a sneak peak:

System Design Template

Additional Building Blocks

Additionally, you will want to understand these concepts

  • Processing large amount of data (aka “big data”) with batch and stream processing
    • Particularly useful for solving data-intensive problems such as designing an analytics app
  • Achieving consistency across services using distribution transaction or event sourcing
    • Particularly useful for solving problems that require strict transactions such as designing financial apps
  • Full text search: full-text index
  • Storing data for the long term: data warehousing

On top of these, there are ad hoc knowledge you would want to know tailored to certain problems. For example, geohashing for designing location-based services like Yelp or Uber, operational transform to solve problems like designing Google Doc. You can learn these these on a case-by-case basis. System design interviews are supposed to test your general design skills and not specific knowledge.

Working through problems and building solutions using the building blocks

Finally, we have a series of practical problems for you to work through. You can find the problem in /problems. This hands-on practice will not only help you apply the principles learned but will also enhance your understanding of how to use the building blocks to construct effective solutions. The list of questions grow. We are actively adding more questions to the list.

Pro Member
Pro Member Exclusive
Upgrade your account to continue
Benefits
check
Unlimited access to practice tool with AI grading
check
Unlimited access to expert-written solutions
check
Unlock 100+ lessons with full course access
check
Access to all future content while subscribed