Google, July 2011
In order to test LevelDB's performance, we benchmark it against other well-established database implementations. We compare LevelDB (revision 39) against SQLite3 (version 18.104.22.168) and Kyoto Cabinet's (version 1.2.67) TreeDB (a B+Tree based key-value store). We would like to acknowledge Scott Hess and Mikio Hirabayashi for their suggestions and contributions to the SQLite3 and Kyoto Cabinet benchmarks, respectively.
Benchmarks were all performed on a six-core Intel(R) Xeon(R) CPU X5650 @ 2.67GHz, with 12288 KB of total L3 cache and 12 GB of DDR3 RAM at 1333 MHz. (Note that LevelDB uses at most two CPUs since the benchmarks are single threaded: one to run the benchmark, and one for background compactions.) We ran the benchmarks on two machines (with identical processors), one with an Ext3 file system and one with an Ext4 file system. The machine with the Ext3 file system has a SATA Hitachi HDS721050CLA362 hard drive. The machine with the Ext4 file system has a SATA Samsung HD502HJ hard drive. Both hard drives spin at 7200 RPM and have hard drive write-caching enabled (using `hdparm -W 1 [device]`). The numbers reported below are the median of three measurements.
We wrote benchmark tools for SQLite and Kyoto TreeDB based on LevelDB's db_bench. The code for each of the benchmarks resides here:
This section gives the baseline performance of all the databases. Following sections show how performance changes as various parameters are varied. For the baseline:
|Kyoto TreeDB||1,010,000 ops/sec|
|Kyoto TreeDB||151,000 ops/sec|
|Kyoto TreeDB||342,000 ops/sec|
|Kyoto TreeDB||88,500 ops/sec|
LevelDB outperforms both SQLite3 and TreeDB in sequential and random write operations and sequential read operations. Kyoto Cabinet has the fastest random read operations.
For this benchmark, we start with an empty database, and write 100,000 byte values (~50% compressible). To keep the benchmark running time reasonable, we stop after writing 1000 values.
|Kyoto TreeDB||1,000 ops/sec|
|Kyoto TreeDB||1,100 ops/sec|
LevelDB doesn't perform as well with large values of 100,000 bytes each. This is because LevelDB writes keys and values at least twice: first time to the transaction log, and second time (during a compaction) to a sorted file. With larger values, LevelDB's per-operation efficiency is swamped by the cost of extra copies of large values.
A batch write is a set of writes that are applied atomically to the underlying database. A single batch of N writes may be significantly faster than N individual writes. The following benchmark writes one thousand batches where each batch contains one thousand 100-byte values. TreeDB does not support batch writes and is omitted from this benchmark.
|LevelDB||840,000 entries/sec||(1.08x baseline)|
|SQLite3||124,000 entries/sec||(2.55x baseline)|
|LevelDB||221,000 entries/sec||(1.35x baseline)|
|SQLite3||22,000 entries/sec||(2.23x baseline)|
Because of the way LevelDB persistent storage is organized, batches of random writes are not much slower (only a factor of 4x) than batches of sequential writes.
In the following benchmark, we enable the synchronous writing modes of all of the databases. Since this change significantly slows down the benchmark, we stop after 10,000 writes. For synchronous write tests, we've disabled hard drive write-caching (using `hdparm -W 0 [device]`).
|LevelDB||100 ops/sec||(0.003x baseline)|
|Kyoto TreeDB||7 ops/sec||(0.0004x baseline)|
|SQLite3||88 ops/sec||(0.002x baseline)|
|LevelDB||100 ops/sec||(0.015x baseline)|
|Kyoto TreeDB||8 ops/sec||(0.001x baseline)|
|SQLite3||88 ops/sec||(0.009x baseline)|
Also see the
ext4 performance numbers below
since synchronous writes behave significantly differently
In the baseline measurements, LevelDB and TreeDB were using light-weight compression (Snappy for LevelDB, and LZO for TreeDB). SQLite3, by default does not use compression. The experiments below show what happens when compression is disabled in all of the databases (the SQLite3 numbers are just a copy of its baseline measurements):
|LevelDB||594,000 ops/sec||(0.76x baseline)|
|Kyoto TreeDB||485,000 ops/sec||(1.42x baseline)|
|SQLite3||48,600 ops/sec||(1.00x baseline)|
|LevelDB||135,000 ops/sec||(0.82x baseline)|
|Kyoto TreeDB||159,000 ops/sec||(1.80x baseline)|
|SQLite3||9,860 ops/sec||(1.00x baseline)|
LevelDB's write performance is better with compression than without since compression decreases the amount of data that has to be written to disk. Therefore LevelDB users can leave compression enabled in most scenarios without having worry about a tradeoff between space usage and performance. TreeDB's performance on the other hand is better without compression than with compression. Presumably this is because TreeDB's compression library (LZO) is more expensive than LevelDB's compression library (Snappy).
We increased the overall cache size for each database to 128 MB. For LevelDB, we partitioned 128 MB into a 120 MB write buffer and 8 MB of cache (up from 2 MB of write buffer and 2 MB of cache). For SQLite3, we kept the page size at 1024 bytes, but increased the number of pages to 131,072 (up from 4096). For TreeDB, we also kept the page size at 1024 bytes, but increased the cache size to 128 MB (up from 4 MB).
|LevelDB||812,000 ops/sec||(1.04x baseline)|
|Kyoto TreeDB||321,000 ops/sec||(0.94x baseline)|
|SQLite3||48,500 ops/sec||(1.00x baseline)|
|LevelDB||355,000 ops/sec||(2.16x baseline)|
|Kyoto TreeDB||284,000 ops/sec||(3.21x baseline)|
|SQLite3||9,670 ops/sec||(0.98x baseline)|
SQLite's performance does not change substantially when compared to the baseline, but the random write performance for both LevelDB and TreeDB increases significantly. LevelDB's performance improves because a larger write buffer reduces the need to merge sorted files (since it creates a smaller number of larger sorted files). TreeDB's performance goes up because the entire database is available in memory for fast in-place updates.
We increased the overall memory usage to 128 MB for each database. For LevelDB, we allocated 8 MB to LevelDB's write buffer and 120 MB to LevelDB's cache. The other databases don't differentiate between a write buffer and a cache, so we simply set their cache size to 128 MB.
|LevelDB||5,210,000 ops/sec||(1.29x baseline)|
|Kyoto TreeDB||1,070,000 ops/sec||(1.06x baseline)|
|SQLite3||609,000 ops/sec||(1.59x baseline)|
|LevelDB||190,000 ops/sec||(1.47x baseline)|
|Kyoto TreeDB||463,000 ops/sec||(3.07x baseline)|
|SQLite3||186,000 ops/sec||(1.39x baseline)|
As expected, the read performance of all of the databases increases when the caches are enlarged. In particular, TreeDB seems to make very effective use of a cache that is large enough to hold the entire database.
For this benchmark, we populated a database with 1 million entries consisting of 16 byte keys and 100 byte values. We compiled LevelDB and Kyoto Cabinet without compression support, so results that are read out from the database are already uncompressed. We've listed the SQLite3 baseline read performance as a point of comparison.
|LevelDB||4,880,000 ops/sec||(1.21x baseline)|
|Kyoto TreeDB||1,230,000 ops/sec||(3.60x baseline)|
|SQLite3||383,000 ops/sec||(1.00x baseline)|
|LevelDB||149,000 ops/sec||(1.16x baseline)|
|Kyoto TreeDB||175,000 ops/sec||(1.16x baseline)|
|SQLite3||134,000 ops/sec||(1.00x baseline)|
Performance of both LevelDB and TreeDB improves a small amount when compression is disabled. Note however that under different workloads, performance may very well be better with compression if it allows more of the working set to fit in memory.
The preceding numbers are for an ext3 file system. Synchronous writes are much slower under ext4 (LevelDB drops to ~31 writes / second and TreeDB drops to ~5 writes / second; SQLite3's synchronous writes do not noticeably drop) due to ext4's different handling of fsync / msync calls. Even LevelDB's asynchronous write performance drops somewhat since it spreads its storage across multiple files and issues fsync calls when switching to a new file.
Jeff Dean and Sanjay Ghemawat wrote LevelDB. Kevin Tseng wrote and compiled these benchmarks. Mikio Hirabayashi, Scott Hess, and Gabor Cselle provided help and advice.