RocksDB
- Problem solved: store and read back key-value pairs very quickly on a server's local flash storage, without going over the network or wearing out SSDs with useless writes.
- Era: created in 2012 at Facebook, open source on 21 November 2013, dual-licensed GPLv2 / Apache 2.0 since July 2017.
- Created by: Facebook; open source announcement signed by Dhruba Borthakur.
- Publisher: Meta (non-Apache project, GPLv2 or Apache 2.0 licence, at the user's choice).
- Category: embedded key-value storage engine (C++ library), LSM tree.
Historical context
In 2011, Google releases LevelDB, a small C++ library that stores sorted key-value pairs. It descends directly from Bigtable, where each tablet is stored by "a precursor to LevelDB", and it is designed for batch updates scattered across a large key space.
At Facebook, flash storage changes the picture. One flash card handles up to 100,000 random operations per second, and ten cards in parallel about a million. A network round trip inside the datacenter costs about 50 microseconds, as much as a flash access: querying a remote database can therefore be twice as slow as reading locally. Hence the appeal of an embedded database, a library that manages its data on the server's SSD, inside the application's own process.
LevelDB cannot handle this server load. Its single-threaded compaction causes frequent write stalls, which hurt 99th percentile latency, and its high write amplification caps SSD bandwidth. In 2012, Facebook therefore starts from the LevelDB code (version 1.5) to create RocksDB, and open sources it on 21 November 2013. At that point it manages close to a petabyte of internal data.
Key idea
Never rewrite in place: every write goes to memory, then to sorted, immutable files that background tasks merge (an LSM tree). Compared with a B-tree updated in place, far less is written to the SSD and compression is better. RocksDB deliberately stays a single-node library: replication, load balancing and SQL are left to the application that embeds it.
How it works
- MemTable and WAL: a write goes to a sorted in-memory buffer (a skiplist) and, optionally, to an on-disk log (Write Ahead Log) for crash recovery.
- SSTables: when the MemTable is full, it is flushed into a sorted, immutable file (Sorted String Table) at level 0, with an index block for binary search.
- Compaction: background threads merge the SSTables of one level with those of the next and drop overwritten or deleted values. Three styles: leveled (taken from LevelDB, the default), universal (tiered, close to Cassandra or HBase, fewer writes but costlier reads) and FIFO (the oldest files are dropped, for caches).
- Reads: lookups go through the MemTables, then level by level; Bloom filters avoid opening files that do not contain the key.
- Pluggable architecture: compression, compaction filter (for example to expire keys), merge operator (update without a prior read), file format: everything can be swapped without touching the core.
rocksdb::DB* db;
rocksdb::Options options;
options.create_if_missing = true;
rocksdb::DB::Open(options, "/data/profiles", &db);
// write: MemTable + WAL, never an in-place rewrite
db->Put(rocksdb::WriteOptions(), "user:42", "Alice");
std::string name;
db->Get(rocksdb::ReadOptions(), "user:42", &name);
No server, no network: the database is a local directory opened by the application process.
By the numbers
| Figure | Context | Source |
|---|---|---|
| 10 times faster | Than LevelDB on random writes and bulk loads, Facebook measurements (2013) | Facebook Engineering announcement |
| Close to 1 PB | Data managed by RocksDB at Facebook when the code was opened (2013) | Facebook Engineering announcement |
| 50% less storage | MyRocks versus compressed InnoDB on Facebook's user database (2016) | MyRocks post |
| 5% of the writes | Writes per transaction of RocksDB versus InnoDB under LinkBench | FAST '21 paper |
| 10 to 30 | Typical write amplification with leveled compaction (4 to 10 with universal) | FAST '21 paper |
| Over 30 applications | RocksDB uses at Facebook, for "many hundreds of petabytes" (2021) | FAST '21 paper |
Milestones
- 27 July 2011: Google open sources LevelDB (BSD-style licence).
- 2012: RocksDB is created at Facebook by forking LevelDB 1.5.
- 21 November 2013: RocksDB is released as open source.
- 2014: work starts on integrating RocksDB into MySQL.
- 8 March 2016: Flink 1.0 ships with a RocksDB state backend.
- 31 August 2016: MyRocks is announced, a MySQL storage engine built on RocksDB.
- 15 July 2017: switch to a dual GPLv2 or Apache 2.0 licence, replacing Facebook's BSD + patents licence rejected by the ASF.
- February 2021: FAST '21 paper on eight years of changing RocksDB priorities.
Did you know?
- A Bigtable legacy: the lineage starts with Bigtable's tablet storage at Google, goes through LevelDB, then RocksDB. A direct thread links the 2006 Bigtable paper to today's embedded engines.
- Shifting priorities: RocksDB first tried to save flash erase cycles, then disk space (most deployments run out of space, not IOPS), then CPU.
- A licence affair: in 2017, Apache Cassandra asks the ASF whether it can depend on RocksDB. The answer puts Facebook's BSD + patents licence in "Category X" (forbidden), and RocksDB changes its licence right away.
- Too many knobs: the authors themselves admit it has become very hard to find an "optimal" configuration: across 39 ZippyDB deployments, more than 25 distinct configurations.
Strengths and limitations
| Strengths | Limitations |
|---|---|
| Fast writes and low write amplification, easy on SSDs | Single node: replication, sharding and distributed backup are up to the application |
| More efficient compression than a fixed-page B-tree (MyRocks: half the space) | No SQL or relational model: a layer on top is needed (MyRocks) |
| Embedded in the process: no network hop | Costlier reads and scans: a scan must go through every level |
| Highly configurable (compaction, compression, filters, merge) | Hard to tune, number of options considered excessive by its own authors |
Where it fits
- A building block under other systems: RocksDB is rarely used on its own. It is the engine of MySQL (MyRocks), CockroachDB, TiDB or MongoDB, and local state for streaming: Flink, Kafka Streams (Kafka), Samza.
- Same family as HBase and Cassandra: they all inherit LSM trees and SSTables from Bigtable, but those are distributed databases while RocksDB is a local engine.
- Facebook origin, like Hive, Cassandra and Presto / Trino: see the Facebook page.
- Contrast with OLAP: RocksDB serves reads and writes of individual keys, the opposite of columnar engines such as ClickHouse or StarRocks.
Sources
raw-data/ecosysteme/rocksdb/rocksdb.mdraw-data/apache/flink/flink.md(RocksDB state backend in Flink 1.0)- https://engineering.fb.com/2013/11/21/core-infra/under-the-hood-building-and-open-sourcing-rocksdb/
- https://engineering.fb.com/2016/08/31/core-infra/myrocks-a-space-and-write-optimized-mysql-database/
- Dong et al., "Evolution of Development Priorities in Key-value Stores Serving Large-scale Applications: The RocksDB Experience", FAST '21, https://www.usenix.org/conference/fast21/presentation/dong
- https://rocksdb.org/
- https://opensource.googleblog.com/2011/07/leveldb-fast-persistent-key-value-store.html
- https://lwn.net/Articles/728178/
- Date the start of RocksDB in 2012 (exact month not sourced).
- The 2017 licence change is only sourced by the press and blogs: find a primary announcement from Meta.
- The gains over LevelDB and InnoDB are measured by Facebook itself.
- Read the VLDB 2020 paper on MyRocks.