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














