Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1## Files3The implementation of leveldb is similar in spirit to the representation of a4single [Bigtable tablet (section 5.3)](http://research.google.com/archive/bigtable.html).5However the organization of the files that make up the representation is6somewhat different and is explained below.8Each database is represented by a set of files stored in a directory. There are9several different types of files as documented below:11### Log files13A log file (*.log) stores a sequence of recent updates. Each update is appended14to the current log file. When the log file reaches a pre-determined size15(approximately 4MB by default), it is converted to a sorted table (see below)16and a new log file is created for future updates.18A copy of the current log file is kept in an in-memory structure (the19`memtable`). This copy is consulted on every read so that read operations20reflect all logged updates.22## Sorted tables24A sorted table (*.ldb) stores a sequence of entries sorted by key. Each entry is25either a value for the key, or a deletion marker for the key. (Deletion markers26are kept around to hide obsolete values present in older sorted tables).28The set of sorted tables are organized into a sequence of levels. The sorted29table generated from a log file is placed in a special **young** level (also30called level-0). When the number of young files exceeds a certain threshold31(currently four), all of the young files are merged together with all of the32overlapping level-1 files to produce a sequence of new level-1 files (we create33a new level-1 file for every 2MB of data.)35Files in the young level may contain overlapping keys. However files in other36levels have distinct non-overlapping key ranges. Consider level number L where37L >= 1. When the combined size of files in level-L exceeds (10^L) MB (i.e., 10MB38for level-1, 100MB for level-2, ...), one file in level-L, and all of the39overlapping files in level-(L+1) are merged to form a set of new files for40level-(L+1). These merges have the effect of gradually migrating new updates41from the young level to the largest level using only bulk reads and writes42(i.e., minimizing expensive seeks).44### Manifest46A MANIFEST file lists the set of sorted tables that make up each level, the47corresponding key ranges, and other important metadata. A new MANIFEST file48(with a new number embedded in the file name) is created whenever the database49is reopened. The MANIFEST file is formatted as a log, and changes made to the50serving state (as files are added or removed) are appended to this log.52### Current54CURRENT is a simple text file that contains the name of the latest MANIFEST55file.57### Info logs59Informational messages are printed to files named LOG and LOG.old.61### Others63Other files used for miscellaneous purposes may also be present (LOCK, *.dbtmp).65## Level 067When the log file grows above a certain size (4MB by default):68Create a brand new memtable and log file and direct future updates here.70In the background:721. Write the contents of the previous memtable to an sstable.732. Discard the memtable.743. Delete the old log file and the old memtable.754. Add the new sstable to the young (level-0) level.77## Compactions79When the size of level L exceeds its limit, we compact it in a background80thread. The compaction picks a file from level L and all overlapping files from81the next level L+1. Note that if a level-L file overlaps only part of a82level-(L+1) file, the entire file at level-(L+1) is used as an input to the83compaction and will be discarded after the compaction. Aside: because level-084is special (files in it may overlap each other), we treat compactions from85level-0 to level-1 specially: a level-0 compaction may pick more than one86level-0 file in case some of these files overlap each other.88A compaction merges the contents of the picked files to produce a sequence of89level-(L+1) files. We switch to producing a new level-(L+1) file after the90current output file has reached the target file size (2MB). We also switch to a91new output file when the key range of the current output file has grown enough92to overlap more than ten level-(L+2) files. This last rule ensures that a later93compaction of a level-(L+1) file will not pick up too much data from94level-(L+2).96The old files are discarded and the new files are added to the serving state.98Compactions for a particular level rotate through the key space. In more detail,99for each level L, we remember the ending key of the last compaction at level L.100The next compaction for level L will pick the first file that starts after this101key (wrapping around to the beginning of the key space if there is no such102file).104Compactions drop overwritten values. They also drop deletion markers if there105are no higher numbered levels that contain a file whose range overlaps the106current key.108### Timing110Level-0 compactions will read up to four 1MB files from level-0, and at worst111all the level-1 files (10MB). I.e., we will read 14MB and write 14MB.113Other than the special level-0 compactions, we will pick one 2MB file from level114L. In the worst case, this will overlap ~ 12 files from level L+1 (10 because115level-(L+1) is ten times the size of level-L, and another two at the boundaries116since the file ranges at level-L will usually not be aligned with the file117ranges at level-L+1). The compaction will therefore read 26MB and write 26MB.118Assuming a disk IO rate of 100MB/s (ballpark range for modern drives), the worst119compaction cost will be approximately 0.5 second.121If we throttle the background writing to something small, say 10% of the full122100MB/s speed, a compaction may take up to 5 seconds. If the user is writing at12310MB/s, we might build up lots of level-0 files (~50 to hold the 5*10MB). This124may significantly increase the cost of reads due to the overhead of merging more125files together on every read.127Solution 1: To reduce this problem, we might want to increase the log switching128threshold when the number of level-0 files is large. Though the downside is that129the larger this threshold, the more memory we will need to hold the130corresponding memtable.132Solution 2: We might want to decrease write rate artificially when the number of133level-0 files goes up.135Solution 3: We work on reducing the cost of very wide merges. Perhaps most of136the level-0 files will have their blocks sitting uncompressed in the cache and137we will only need to worry about the O(N) complexity in the merging iterator.139### Number of files141Instead of always making 2MB files, we could make larger files for larger levels142to reduce the total file count, though at the expense of more bursty143compactions. Alternatively, we could shard the set of files into multiple144directories.146An experiment on an ext3 filesystem on Feb 04, 2011 shows the following timings147to do 100K file opens in directories with varying number of files:150| Files in directory | Microseconds to open a file |151|-------------------:|----------------------------:|152| 1000 | 9 |153| 10000 | 10 |154| 100000 | 16 |156So maybe even the sharding is not necessary on modern filesystems?158## Recovery160* Read CURRENT to find name of the latest committed MANIFEST161* Read the named MANIFEST file162* Clean up stale files163* We could open all sstables here, but it is probably better to be lazy...164* Convert log chunk to a new level-0 sstable165* Start directing new writes to a new log file with recovered sequence#167## Garbage collection of files169`DeleteObsoleteFiles()` is called at the end of every compaction and at the end170of recovery. It finds the names of all files in the database. It deletes all log171files that are not the current log file. It deletes all table files that are not172referenced from some level and are not the output of an active compaction.
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.