liveBook · Manning

SQLite

As explained in Latency

SQLite is an embedded relational database engine that stores and processes database contents within an application process.

SQLite is an embedded relational database engine that stores and processes database contents inside the application process. Embedding it colocates data with computation, avoiding network access between an API server and its database. section 3.5 section 3.6

A Rust REST API can open a SQLite database, share the connection through an Arc<Mutex<Connection>>, and execute queries directly from request handlers. section 3.5

A practical SQLite integration follows this shape:

  • Open a database connection using a path such as hello.db. section 3.5
  • Share the connection with the API application and protect access with a mutex. section 3.5
  • Execute a query such as SELECT 'hello world' with query_row. section 3.5
  • Measure the resulting HTTP latency with wrk rather than assuming that colocation improves every workload equally. section 3.5

Embedded REST API benchmark

A SQLite-backed /hello endpoint measured 97.66 microseconds average latency and 188,469.82 requests per second in a wrk test. The PostgreSQL version measured 8.06 milliseconds and 1,460.66 requests per second. The latency comparison attributes much of SQLite's steadier distribution to the absence of 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 its connection with the Actix Web application. section 3.5

$ wrk --latency http://127.0.0.1:8080/hello
Running 10s test @ http://127.0.0.1:8080/hello
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    97.66us  419.07us  13.76ms   97.73%
    Req/Sec    94.72k    13.77k  140.88k    87.13%
  Latency Distribution
     50%   41.00us
     75%   51.00us
     90%   72.00us
     99%    1.97ms
  1903434 requests in 10.10s, 232.35MB read
Requests/sec: 188469.82
Transfer/sec:     23.01MB

Inspect the wrk output for the SQLite-backed endpoint, including average latency, tail latency, throughput, and request count. section 3.5

How Latency covers it

How deep it goes
Demonstrates connection sharing, query execution, and HTTP benchmarking, but does not detail SQLite storage internals or schema design.
Examples
Compares SQLite and PostgreSQL through a Rust /hello endpoint, wrk measurements, and latency-distribution analysis.
What it assumes
Assumes Rust, Actix Web, asynchronous request handling, and basic REST API construction; demonstrates SQLite integration directly.
What you can do after
Builds a Rust REST endpoint backed by SQLite and evaluates it with wrk latency and throughput measurements.
How it connects
Connects SQLite with embedded databases, REST APIs, colocation, latency, PostgreSQL, and HTTP benchmarking.

Also explained in

Compare how 15 books explain SQLite →

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.