Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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:

  1. Connection A starts a deferred BEGIN, runs a SELECT, and holds a read snapshot. Connection B writes and commits. When Connection A later runs an INSERT or UPDATE inside the same transaction, SQLite cannot upgrade its stale read snapshot to a write lock and fails immediately with SQLITE_BUSY_SNAPSHOT, even with a 10-second busy_timeout.
  2. 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 with read_only(true) and a 5-second busy_timeout. Concurrent HTTP requests execute SELECT queries in parallel without blocking writers or each other.
  • ctx.db.writer() (aliased as ctx.db.pool()) holds a single connection (max_connections(1)) with a 10-second busy_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:

PragmaValueEffect
journal_modeWAL (file-backed only)Readers do not block writers, and writers do not block readers.
synchronousNORMALSafe durability in WAL mode without an fsync on every transaction commit.
foreign_keysONEnforces REFERENCES and ON DELETE CASCADE constraints on every connection.
cache_size-64000Allocates roughly 64 MiB of page cache per connection.
temp_storeMEMORYKeeps temporary B-trees and sort buffers in RAM.
mmap_size268435456 (file-backed only)Memory-maps up to 256 MiB of the database file for fast read access.
busy_timeout10s writer, 5s readerWaits 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.