A database provides persistent state for a web service, allowing application code to create, read, update, delete, and list structured records. section 9.0
A practical service architecture treats the database as a stateful tier:
- Use SQLite for the working implementation; a production deployment might instead use a SQL database such as PostgreSQL. section 9.2
- Let each horizontally scalable API instance connect independently to the database for storing and retrieving state. section 9.2
- Configure the database connection through an environment variable alongside logging configuration. section 9.2
- Model records with Rust structs and derive sqlx::FromRow so SQLx can map query results into application types. section 9.6.2
Todo persistence with SQLx
Implement list and read operations with parameter binding: query_as("select * from todos").fetch_all(&dbpool) and query_as("select * from todos where id = ?").bind(id).fetch_one(&dbpool). Await each query and map sqlx::Error into the service error type. Apply the same pattern to create, update, and delete operations, taking care to bind values in SQL placeholder order. section 9.6.2
Use the architecture diagram to trace requests from the load balancer through API instances to the stateful database tier. section 9.2























