NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / EMBEDDED ENGINE / THE EMBEDDED ENGINE

The embedded engine

rasterizeddb_embedded runs the whole RasterizedDB engine inside your Rust program, with no separate server process to install, start or monitor. Use it when the database belongs to one application: a desktop or command-line tool, a service that keeps its own state, or tests that need a real database.

How it works

Builder::start opens a database, either a directory or memory only. It then runs the engine on threads of its own, the same way the rasterizeddb server binary runs it, and serves the native protocol on a private endpoint that only your program can reach. Embedded::connect gives you a rasterizeddb_client::Client for that endpoint. From there you use the database exactly as you would use a server:

rust
use rasterizeddb_embedded::Builder;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let db = Builder::open("./data").start().await?;
    let client = db.connect().await?;

    client.execute("CREATE TABLE IF NOT EXISTS notes (id BIGINT, body TEXT)", &[]).await?;
    client.execute("INSERT INTO notes VALUES (1, 'hello')", &[]).await?;
    let rows = client.query("SELECT body FROM notes WHERE id = 1", &[]).await?;
    println!("{}", rows.iter().next().unwrap().get::<String>(0)?);

    drop(client);
    db.shutdown().await?;
    Ok(())
}

The engine gets its own threads for two reasons:

  • Stack size. The engine runs every statement on 32 MiB stacks, as the server does. Tokio's default 2 MiB stack can overflow on a deeply nested statement. Because the engine has its own runtime, it gets those stacks whatever runtime your program uses, or if it uses none.
  • One code path. Statements reach the engine through the same session layer that network clients go through: transactions, views, sequences, functions and privileges. An embedded database gives the same answers a server would.

Embedded or server

Choose embedded whenChoose the server when
One program owns the dataSeveral programs or machines share the data
You want a single binary with nothing else to deployOther people or tools need to connect (psql, ORMs in other languages, RDB Studio)
Tests need a real database that starts and stops with the testYou need the PostgreSQL, MySQL, SQL Server or Redis protocols
The database is the program's own stateYou want to run it as a service of its own, with replication or scheduled backups

You don't have to decide once and for all. The application code is the same rasterizeddb_client code in both cases, so you can start embedded and later point the same code at a server by changing the address you connect to. A data directory written by the embedded engine opens in the server binary, and a server's directory opens embedded.

What is the same

  • SQL. The engine is built with the server's default features, so the same statements, types, indexes, JSON, partitioning and key/value statements are available.
  • The client API. Typed queries, transactions, the ORM, migrations and the key/value API from rasterizeddb_client all work unchanged.
  • Durability. Commits go through the same write-ahead log. With synchronous commit on (the default), a committed transaction survives a crash.
  • Settings. Every server setting is a Builder method, or a key you set with Builder::config. SHOW CONFIG and SET CONFIG work through any client, as they do on a server.
  • Multiple databases. CREATE DATABASE works, and a client chooses one with Options::database. See Several databases.

What is different

EmbeddedServer
Protocols servedThe native protocol, on a private endpoint onlyPostgreSQL, native, MySQL / MariaDB, SQL Server, Redis
Endpoint on Linux and other Unix systemsA unix socket in a fresh 0700 directory under the temporary directory (socket file 0600)The addresses and sockets you configure
Endpoint on WindowsTLS on 127.0.0.1, on a port the system picksThe addresses you configure
AuthenticationUnix: none needed, because only your operating-system user can open the socket. Windows: a password (Builder::password) is required, unless you set allow_unauthenticated_loopback(true)As configured
ConfigurationBuilder methods, plus rasterizeddb.conf in the data directoryCommand-line flags, plus rasterizeddb.conf
Ctrl+C and SIGTERMLeft to your program. The engine installs no signal handlersThe server stops cleanly on them
StoppingEmbedded::shutdown, shutdown_blocking, or dropping the EmbeddedA signal

Because the embedded engine opens no network port, you can't point psql, Studio or another machine at it. If other processes need the data, run the server on the same directory instead, after the embedded engine has stopped.

One engine per process, one process per directory

One engine at a time in a process. The engine keeps its settings in process-wide state, and SET CONFIG saves to the running engine's directory. A second engine running beside the first would change the first one's settings and save them into the wrong file. So start fails with std::io::ErrorKind::AlreadyExists while another engine runs in the process, whatever directory either one uses. To serve several databases, create them in the one engine with CREATE DATABASE. Starting again after a stop is fine.

One engine per directory. While an engine has a directory open, it holds an operating-system lock on rasterizeddb.lock in that directory. Any other opener is refused with an "already open" error before it reads anything: a second process, the server binary, or another engine in the same process. The message names the process that holds the lock. The lock is released when its holder ends, even if the holder was killed, so a crash never leaves a directory locked.

Platforms and CPU

  • Linux and other Unix systems. The endpoint is a unix socket that only your operating-system user can open, so no password is needed.
  • Windows. There are no unix sockets on Windows, so the endpoint is a TLS listener on 127.0.0.1. Any local process can reach that port, so start refuses to run without Builder::password(user, password), or Builder::allow_unauthenticated_loopback(true) if you accept that risk. The client that Embedded::connect returns pins the engine's own certificate, so a process that grabbed the port first cannot pose as the engine.
  • x86-64 needs an AVX2 CPU (x86-64-v3: Intel Haswell, AMD Excavator or later). start checks this and fails with ErrorKind::Unsupported, naming the missing instructions, instead of crashing later with an illegal instruction. For full speed, build with -C target-cpu=x86-64-v3 (see Getting started).
  • ARM64 (aarch64) needs nothing extra.

Limits

  • The engine serves only its private native endpoint. It has no PostgreSQL, MySQL, SQL Server or Redis listener, so the native protocol's limits apply to everything you send. See the Rust client.
  • One engine runs per process at a time, and some settings can be chosen only once per process. See Settings are process-wide.
  • A database held in memory (Builder::in_memory) is gone when the engine stops, and it has no settings file.
  • On x86-64, the engine refuses to start on a CPU without AVX2.
  • The license is GPL-3.0-only.
✓ page rendered in 164 µs axum + tera + htmx · no other javascript