Setting up embedded SurrealDB with RocksDB backend in Rust

SurrealDB RocksDB

Before we get into the use of SurrealDB from Rust, let's see a very simple example of setting it up as an embedded database.

Depending on the use case you might want a stand-alone running SurrealDB to which you can connect from some external process or you might want a database that does not need a separate process. In this case you might use an in-memory version of SurreealDB, but for my current goal I wanted to have a persistent database where the data is stored on the disk. I just did not want to deal with a separate database server process.

For the disk storage I had to pick one of the database systems that provide the file stores. (As I understand there are several options. I went with RocksDB.

Crate a crate

cargo new embedded-rocksdb
cd embedded-rocksdb


These are the dependencies in the Cargo.toml file.


name = "embedded-rocksdb"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

serde = { version = "1.0", features = ["derive"] }
surrealdb = { version = "1.1", features = ["kv-rocksdb"] }
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }

The code


use surrealdb::Surreal;
use surrealdb::engine::local::RocksDb;

async fn main() -> surrealdb::Result<()> {
    let _db = Surreal::new::<RocksDb>("tempdb").await?;

    // let current_dir = std::env::current_dir().unwrap();
    // let _db = Surreal::new::<RocksDb>(current_dir.join("tempdb")).await?;


Running the code

cargo run

Running this code will first need to compile it that might take several minutes. Then it will create a folder called tempdb thats store the database.

This folder will be created in the root of the crate.

In some earlier versions of SurrealDB this feature was broken and I had to use a full-path to the folder. I left that code in as comment to show you that you can also provide a full path to the database folder.


I know we have not done much yet, but I like to celebrate even small steps.

If you need an in-memory database with slight modifications this can be converted to one. See Setting up an in-memory SurrealDB database in Rust.

Related Pages

Setting up an in-memory SurrealDB database in Rust


Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo