This guide will walk you through creating your first RocksDB application. You’ll learn how to open a database, perform basic operations, and understand core concepts.
This guide assumes you have already installed RocksDB on your system.
using ROCKSDB_NAMESPACE::DB;using ROCKSDB_NAMESPACE::Options;using ROCKSDB_NAMESPACE::ReadOptions;using ROCKSDB_NAMESPACE::Status;using ROCKSDB_NAMESPACE::WriteBatch;using ROCKSDB_NAMESPACE::WriteOptions;
2
Configure and Open the Database
Configure your database options and open it:
std::unique_ptr<DB> db;Options options;// Optimize RocksDB - easiest way to get good performanceoptions.IncreaseParallelism();options.OptimizeLevelStyleCompaction();// Create the DB if it's not already presentoptions.create_if_missing = true;// Open DBStatus s = DB::Open(options, "/tmp/rocksdb_simple_example", &db);assert(s.ok());
Performance Tip: IncreaseParallelism() sets the number of background threads to the number of CPU cores, and OptimizeLevelStyleCompaction() configures options optimized for level-style compaction.
3
Write Data (Put)
Store a key-value pair:
// Put key-values = db->Put(WriteOptions(), "key1", "value");assert(s.ok());
Here’s the complete working example from the RocksDB source code:
examples/simple_example.cc
#include <cstdio>#include <memory>#include <string>#include "rocksdb/db.h"#include "rocksdb/options.h"#include "rocksdb/slice.h"using ROCKSDB_NAMESPACE::DB;using ROCKSDB_NAMESPACE::Options;using ROCKSDB_NAMESPACE::PinnableSlice;using ROCKSDB_NAMESPACE::ReadOptions;using ROCKSDB_NAMESPACE::Status;using ROCKSDB_NAMESPACE::WriteBatch;using ROCKSDB_NAMESPACE::WriteOptions;#if defined(OS_WIN)std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_simple_example";#elsestd::string kDBPath = "/tmp/rocksdb_simple_example";#endifint main() { std::unique_ptr<DB> db; Options options; // Optimize RocksDB. This is the easiest way to get RocksDB to perform well options.IncreaseParallelism(); options.OptimizeLevelStyleCompaction(); // create the DB if it's not already present options.create_if_missing = true; // open DB Status s = DB::Open(options, kDBPath, &db); assert(s.ok()); // Put key-value s = db->Put(WriteOptions(), "key1", "value"); assert(s.ok()); std::string value; // get value s = db->Get(ReadOptions(), "key1", &value); assert(s.ok()); assert(value == "value"); // atomically apply a set of updates { WriteBatch batch; batch.Delete("key1"); batch.Put("key2", value); s = db->Write(WriteOptions(), &batch); } s = db->Get(ReadOptions(), "key1", &value); assert(s.IsNotFound()); db->Get(ReadOptions(), "key2", &value); assert(value == "value"); db.reset(); return 0;}
Performance Optimization: PinnableSlice can avoid a memcpy by directly pointing to RocksDB’s internal buffer when possible. Always call Reset() before reusing a PinnableSlice.
std::string string_val;// If it cannot pin the value, it copies the value to its internal bufferPinnableSlice pinnable_val(&string_val);db->Get(ReadOptions(), db->DefaultColumnFamily(), "key2", &pinnable_val);assert(pinnable_val == "value");// If the value is not pinned, the internal buffer must have the valueassert(pinnable_val.IsPinned() || string_val == "value");
Options options;// Create database if missingoptions.create_if_missing = true;// Error if database already existsoptions.error_if_exists = false;// Optimize for your workloadoptions.IncreaseParallelism(); // Use all CPU coresoptions.OptimizeLevelStyleCompaction(); // Optimize for level compaction// Set write buffer size (default: 64MB)options.write_buffer_size = 64 * 1024 * 1024;// Maximum number of write buffersoptions.max_write_buffer_number = 3;
ReadOptions read_options;// Use a snapshot for consistent readsread_options.snapshot = db->GetSnapshot();// Fill block cacheread_options.fill_cache = true;
Memory Management: Always delete the DB object when done, or use std::unique_ptr<DB> for automatic cleanup. Close all iterators before closing the database.
Error Handling: Always check the Status returned by RocksDB operations. Ignoring errors can lead to data loss or corruption.
PinnableSlice Reuse: Always call Reset() on a PinnableSlice before reusing it. The slice is invalid after reset until the next Get call.
# Compile the exampleg++ -std=c++20 -o simple_example simple_example.cc -lrocksdb# Run it./simple_example# Check the created databasels -la /tmp/rocksdb_simple_example/
Congratulations! You’ve created your first RocksDB application. Continue exploring the documentation to learn about advanced features.