SQLite WAL split pools and BEGIN IMMEDIATE
SQLite runs inside the same process as your Axum server, so queries pay zero network latency. Out of the box, however, default SQLite settings trigger SQLITE_BUSY and SQLITE_BUSY_SNAPSHOT errors under concurrent web traffic. slabs-db avoids both failure modes with split connection pools, tuned pragmas, and BEGIN IMMEDIATE write transactions.
Why single-pool SQLite fails under web concurrency
SQLite allows many concurrent readers alongside one active writer in Write-Ahead Logging (WAL) mode. When an application puts both reads and writes onto one general-purpose SqlitePool and opens default DEFERRED transactions (BEGIN), two bugs appear under load:
- Connection A starts a deferred
BEGIN, runs aSELECT, and holds a read snapshot. Connection B writes and commits. When Connection A later runs anINSERTorUPDATEinside the same transaction, SQLite cannot upgrade its stale read snapshot to a write lock and fails immediately withSQLITE_BUSY_SNAPSHOT, even with a 10-secondbusy_timeout. - Multiple connections competing for the write lock inside SQLite trigger retry loops and busy waits instead of queueing cleanly in Tokio.
How slabs_db::Db solves it
For every file-backed SQLite database, Db::connect creates two distinct SqlitePool instances:
ctx.db.reader()holds up to 16 connections opened withread_only(true)and a 5-secondbusy_timeout. Concurrent HTTP requests executeSELECTqueries in parallel without blocking writers or each other.ctx.db.writer()(aliased asctx.db.pool()) holds a single connection (max_connections(1)) with a 10-secondbusy_timeout. Tokio queues concurrent writers in memory on the pool semaphore instead of letting multiple SQLite connections collide on the file lock.
When you call ctx.db.begin().await?, slabs-db runs BEGIN IMMEDIATE on the single writer connection (begin_with("BEGIN IMMEDIATE")). SQLite acquires the write lock up front before any read runs inside the transaction, so a write-upgrade deadlock cannot happen.
Pragmas applied automatically
Db::connect sets these SQLite pragmas on startup:
| Pragma | Value | Effect |
|---|---|---|
journal_mode | WAL (file-backed only) | Readers do not block writers, and writers do not block readers. |
synchronous | NORMAL | Safe durability in WAL mode without an fsync on every transaction commit. |
foreign_keys | ON | Enforces REFERENCES and ON DELETE CASCADE constraints on every connection. |
cache_size | -64000 | Allocates roughly 64 MiB of page cache per connection. |
temp_store | MEMORY | Keeps temporary B-trees and sort buffers in RAM. |
mmap_size | 268435456 (file-backed only) | Memory-maps up to 256 MiB of the database file for fast read access. |
busy_timeout | 10s writer, 5s reader | Waits cleanly during brief checkpoints or external CLI migrations. |
In-memory databases during parallel tests
SQLite isolates :memory: databases per connection, so opening a second connection creates a separate empty database. When database_url is "sqlite::memory:" or contains "mode=memory", Db::connect clones the single writer pool for reader(). Your slabs still call ctx.db.reader() and ctx.db.begin() identically in production and in cargo nextest run.
Choosing between SQLite and PostgreSQL
SQLite with split WAL pools handles single-node web applications with thousands of reads per second and zero external database operations, and Litestream replicates WAL frames continuously to S3-compatible storage. Move to slabs_db::PgDb (DATABASE_URL=postgres://...) when you need multi-replica rolling deploys, horizontal write scaling, or PostgreSQL extensions.