Language: CPP
Database
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.
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.
sudo apt install librocksdb-devbrew install rocksdbvcpkg install rocksdb or build from source with CMakeRocksDB 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.
#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.
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.
rocksdb::WriteBatch batch;
batch.Put("key2", "value2");
batch.Delete("key1");
db->Write(rocksdb::WriteOptions(), &batch);Performs multiple operations in a single atomic batch.
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.
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.
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.
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.