RocksDB: A Persistent Key-Value Store for Flash and RAM Storage
1. Prerequisites
- Operating System: Linux, macOS, or Windows (with WSL recommended)
- Compiler: C++11 compatible compiler (GCC 4.8+, Clang 3.3+, or Visual Studio 2015+)
- Build System: CMake 2.8.12 or newer
- Dependencies:
- zlib (for compression)
- bzip2 (for compression)
- lz4 (for compression)
- zstd (for compression)
- snappy (for compression)
- gflags (for examples)
- glog (for examples)
2. Installation
Clone the Repository
git clone https://github.com/facebook/rocksdb.git
cd rocksdb
Build and Install
# Build with default options
make
# Or build with specific options (e.g., with ZSTD support)
make static_lib
# Install to system (requires sudo)
sudo make install
Build Examples
# Build examples
make all_examples
# Run a simple example
./examples/simple_example
3. Configuration
RocksDB configuration is primarily done through the Options class when creating a database instance. Key configuration options include:
#include "rocksdb/db.h"
rocksdb::DB* db;
rocksdb::Options options;
options.create_if_missing = true;
options.write_buffer_size = 64 * 1024 * 1024; // 64MB
options.max_write_buffer_number = 3;
options.target_file_size_base = 64 * 1024 * 1024; // 64MB
rocksdb::Status status = rocksdb::DB::Open(options, "/path/to/db", &db);
Environment Variables
ROCKSDB_DISABLE_JEMALLOC: Set to 1 to disable jemallocROCKSDB_DISABLE_GFLAGS: Set to 1 to disable gflags
4. Build & Run
Development Build
# Clean build
make clean
# Build with debug symbols
make debug
# Run tests
make check
Production Build
# Optimized build
make static_lib
# Build shared library
make shared_lib
# Install to system
sudo make install
Running Locally
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/status.h"
using namespace rocksdb;
int main() {
DB* db;
Options options;
options.create_if_missing = true;
Status status = DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());
std::string value;
status = db->Get(ReadOptions(), "key1", &value);
if (status.IsNotFound()) {
status = db->Put(WriteOptions(), "key1", "value1");
}
delete db;
return 0;
}
5. Deployment
Platform Recommendations
Linux Servers: Deploy on Ubuntu 18.04+ or CentOS 7+ with SSD storage for optimal performance.
Docker Deployment:
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
build-essential \
cmake \
zlib1g-dev \
libbz2-dev \
liblz4-dev \
libzstd-dev \
libsnappy-dev \
libgflags-dev \
libgoogle-glog-dev
WORKDIR /app
COPY . .
RUN make static_lib
CMD ["./your_application"]
Kubernetes Deployment: Use StatefulSets for persistent storage with SSD-backed volumes.
Performance Considerations
- Use SSD storage for optimal performance
- Monitor memory usage (RocksDB can consume significant RAM)
- Configure appropriate compaction settings based on workload
- Use bloom filters for range queries
6. Troubleshooting
Common Issues
1. "Symbol lookup error" when running examples
# Solution: Ensure all dependencies are installed
sudo apt-get install libsnappy-dev liblz4-dev libzstd-dev
make clean && make
2. "Corruption: corrupted compressed block contents"
# Solution: This usually indicates hardware issues or corrupted storage
# Check disk health and consider using checksums
options.compression_opts.zlib = true;
3. High memory usage
# Solution: Adjust write buffer and cache settings
options.write_buffer_size = 32 * 1024 * 1024; // 32MB
options.max_write_buffer_number = 2;
options.block_cache = NewLRUCache(512 * 1024 * 1024); // 512MB cache
4. Slow reads
# Solution: Enable bloom filters and adjust block cache
options.filter_policy = NewBloomFilterPolicy(10);
options.no_block_cache = false;
options.block_cache_size_mb = 1024;
Getting Help
- GitHub Issues: https://github.com/facebook/rocksdb/issues
- Developer Group: https://www.facebook.com/groups/rocksdb.dev/
- Email List: https://groups.google.com/g/rocksdb
Performance Monitoring
Use RocksDB's built-in statistics:
Options options;
options.statistics = rocksdb::CreateDBStatistics();
// ... open database ...
std::string stats;
options.statistics->ToString(&stats);
std::cout << stats << std::endl;