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

