How it works · key-value storage library

How LevelDB works

A store for keys and values that makes writing fast by never changing a file: it only adds, and tidies up later.

Think of it like this. A desk with an inbox. New papers go on the pile at once. When the pile is tall it is sorted into a folder, and now and then folders are merged into bigger ones.

  1. The log Every write is added to the end of a file first, so nothing is lost in a crash. db/log_writer.cc
  2. The memtable The newest writes, kept sorted in memory. db/memtable.cc
  3. Table files Sorted files on disk that are written once and never changed. table/table_builder.cc
  4. Levels Keeps track of which files exist, level by level, each level about ten times bigger. db/version_set.cc
  5. Compaction In the background, merges files down a level and drops what was overwritten or deleted. db/db_impl.cc

Follow one thing through: db->Put("name", "Ada"), then db->Get("name")

  1. Writes waiting at the same moment are grouped into one batch.db/db_impl.cc
  2. The batch is added to the end of the log file.db/log_writer.cc
  3. Then it is put into the memtable. The write is done.db/memtable.cc
  4. When the memtable is full it is frozen, a fresh one takes over, and the frozen one is written out as a table file.db/db_impl.cc
  5. A read looks in the memtable, then the frozen one, then the files, newest first. The first answer wins.db/db_impl.cc
  6. Inside a file, an index says which block could hold the key, so only that block is read.table/table.cc

The clever bit: A sorted list that needs no rebalancing

The memtable is a skip list: a sorted linked list where some nodes also have links that jump further ahead. How tall a node is gets decided by chance, one in four for each extra level, up to twelve. That is enough to make a search skip most of the list, like a tree would, with none of a tree’s rebalancing. It also means readers never have to wait for a writer.

db/skiplist.h

Words you’ll meet

memtable
The newest data, sorted, in memory.
SSTable
A sorted file of keys and values that is never changed after it is written.
compaction
Merging files and throwing away old versions of keys.
write-ahead log
A file every write goes to first, to replay after a crash.

Good questions to ask next

Open LevelDB in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.

Take LevelDB apart yourself.

Free and open source. Nothing to install, no account.

Ideas inside it

More projects, taken apart

How React worksYou describe what the screen should look like. React works out the smallest set of changes to make it so.How Express worksA request comes in and passes through a list of functions, in order, until one of them answers.How Flask worksYou write a function for each address. Flask finds the right one for a request, calls it, and turns what it…How Redis worksOne big dictionary held in memory, answered by a single loop that takes one command at a time.How nanoGPT worksText becomes numbers, a model learns to guess the next number, and guessing again and again writes new text.How micrograd worksEvery number remembers how it was made, so the answer can be traced back to say how much each input mattered.How SQLite worksYour SQL is compiled into a small program, and a tiny virtual machine runs it against one file on disk.How FastAPI worksYou write a function with type hints. FastAPI reads the hints to check the request, convert the values and…How Vue worksVue notices which data each part of the screen reads, so when that data changes it knows exactly what to…How Requests worksOne friendly line, requests.get(url), on top of the fiddly work of opening connections and speaking HTTP.How Redux worksAll of an app’s state lives in one place, and the only way to change it is to describe what happened.How Git worksEvery version of every file is stored under a name made from its own contents, and a commit is a small note…How Preact worksThe same idea as React, in a few kilobytes: you describe the screen, and Preact changes only what is…How Zustand worksA store is a value, a way to change it, and a list of who wants to know. The whole core is about a hundred…How Axios worksOne way to make web requests that works the same in a browser and in Node, with hooks before and after every…How htmx worksAny element can ask the server for a piece of HTML and put it into the page. You write attributes, not…How etcd’s Raft worksSeveral servers keep the same list of changes in the same order, even when some of them crash. This is the…

Written by hand from the code as it was on 2026-11-12 (commit 2946558). LevelDB belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.