Skip to content

RocksDB

Databases & CachingDatabaseC++

What it is

RocksDB is an embeddable, high-performance, persistent key-value store developed by Facebook. It is based on LevelDB but optimized for fast storage, low-latency access, and high throughput on flash drives and SSDs.

RocksDB provides an API for storing and retrieving key-value pairs, supporting advanced features like column families, snapshots, iterators, batch writes, and transactions. It is designed for write-heavy workloads.

Installation

sudo apt install librocksdb-dev

Getting started

The smallest useful thing you can do with it, and what each part means.

Opening a database and writing data
#include <rocksdb/db.h>
#include <iostream>

int main() {
    rocksdb::DB* db;
    rocksdb::Options options;
    options.create_if_missing = true;

    rocksdb::Status status = rocksdb::DB::Open(options, "testdb", &db);
    if (!status.ok()) {
        std::cerr << status.ToString() << std::endl;
        return 1;
    }

    status = db->Put(rocksdb::WriteOptions(), "key1", "value1");
    std::cout << "Write Status: " << status.ToString() << std::endl;

    delete db;
    return 0;
}
Creates a RocksDB database, writes a key-value pair, and closes it.

Advanced usage

Where the library earns its place over a simpler alternative.

Reading data
std::string value;
status = db->Get(rocksdb::ReadOptions(), "key1", &value);
if (status.ok()) std::cout << "Value: " << value << std::endl;
Reads the value of a key from the database.
Batch writes
rocksdb::WriteBatch batch;
batch.Put("key2", "value2");
batch.Delete("key1");
db->Write(rocksdb::WriteOptions(), &batch);
Performs multiple operations in a single atomic batch.
Using iterators
rocksdb::Iterator* it = db->NewIterator(rocksdb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
    std::cout << it->key().ToString() << " => " << it->value().ToString() << std::endl;
}
delete it;
Iterates through all key-value pairs in the database.
Transactions
rocksdb::TransactionDB* txn_db;
rocksdb::TransactionDBOptions txn_options;
rocksdb::DBOptions db_options;
db_options.create_if_missing = true;
rocksdb::TransactionDB::Open(db_options, txn_options, "testdb_txn", &txn_db);
Opens RocksDB in transaction mode for ACID-compliant operations.
Column Families
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
column_families.push_back(rocksdb::ColumnFamilyDescriptor(
    rocksdb::kDefaultColumnFamilyName, rocksdb::ColumnFamilyOptions()));
Uses column families to organize different logical databases within the same RocksDB instance.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Status::NotFound
Indicates the requested key does not exist. Always check `status.ok()` before using results.
Corruption errors
May happen if the database files are corrupted. Run RocksDB repair tools or restore from backup.
Performance degradation
Adjust compaction strategy, memory tables, and background thread settings to match workload.

Best practices

  • Use `WriteBatch` for grouped writes to improve performance.
  • Leverage column families for data partitioning and better management.
  • Use Bloom filters for fast point-lookups.
  • Monitor compaction and tune it for your workload to avoid performance bottlenecks.
  • Enable compression (e.g., Snappy, Zstd) to save storage space.

Background

Why it exists, and what it was reacting to.

RocksDB was developed at Facebook in 2012 to handle data-intensive applications that required low-latency and high-throughput storage. Built on Google’s LevelDB, it was enhanced with features like column families, transactions, bloom filters, and better multi-threading. Today, it powers large-scale systems at Facebook, LinkedIn, and other enterprises.