liveBook · Manning

Embedded database

As explained in Latency

An embedded database is a database engine that runs in the same process or memory space as the application using it.

An embedded database runs within the application’s process or memory space instead of requiring a separate database server. SQLite provides the concrete implementation here, colocated with a Rust REST API. section 3.5

A practical embedded-database setup involves:

  • Opening a SQLite database and sharing its connection through an application-managed Arc<Mutex<Connection>>. section 3.5
  • Handling requests with a direct query such as SELECT 'hello world', without a network connection pool. section 3.5
  • Comparing latency against PostgreSQL using wrk rather than assuming colocated access is always faster. section 3.5

SQLite versus PostgreSQL latency

A wrk test reports SQLite at 97.66 microseconds average latency and 188,469 requests per second, versus PostgreSQL at 8.06 milliseconds and 1,460 requests per second. The latency distribution attributes much of the difference to avoiding network-access variability. section 3.5

Listing 3.3 REST API server scaffolding with SQLite
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let database_url = env::var("DATABASE_URL")?;
    let conn = Arc::new(Mutex::new(Connection::open(
        database_url,
    )?));
    let app = move || {
        App::new()
            .app_data(web::Data::new(conn.clone()))
            .service(
                web::resource("/hello")
                    .route(web::get().to(say_hello)),
            )
    };
    Ok(HttpServer::new(app)
        .bind("127.0.0.1:8080")?
        .run()
        .await?)
}

Inspect the Rust server scaffolding that opens SQLite and shares the connection with the REST API. section 3.5

How Latency covers it

How deep it goes
Descends through connection setup, request handling, benchmarking, and latency distributions, but does not develop database internals.
Examples
Uses a SQLite-backed /hello endpoint, direct SQL queries, wrk output, and an eCDF comparing SQLite with PostgreSQL.
What it assumes
Assumes basic Rust and HTTP-server familiarity while requiring Rust, PostgreSQL, and wrk installation for the comparison workflow.
What you can do after
Apply it via Rust, Actix Web, SQLite, and wrk to build and measure a colocated database-backed endpoint.
How it connects
Connects embedded databases with SQLite, REST APIs, Rust, Actix Web, PostgreSQL, network latency, and HTTP benchmarking.

Also explained in

Compare how 2 books explain embedded database →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.