Databases / Backend

Measure concurrency in SQLite WAL mode

Use a two-connection experiment to understand SQLite snapshots, writer contention, checkpoints and the limits of a local database.

A local database with a parallel write-ahead log and two reader snapshots

SQLite can be a good fit for a workshop check-in tool running on one machine. Write-ahead logging, or WAL, can let a dashboard keep reading while somebody records an arrival. It still has one writer at a time. That distinction matters when a successful prototype moves from a laptop to a service used by several organisers.

Watch two connections disagree for a valid reason

Open sqlite3 workshop.db in terminal A. Set WAL mode on this fresh, disposable local database.

PRAGMA journal_mode = WAL;
CREATE TABLE checkins (
  id INTEGER PRIMARY KEY,
  badge TEXT NOT NULL
);
INSERT INTO checkins (badge) VALUES ('first');

BEGIN;
SELECT COUNT(*) FROM checkins;

Leave that transaction open. Open the same database in terminal B and run:

INSERT INTO checkins (badge) VALUES ('second');
SELECT COUNT(*) FROM checkins;

B sees two rows. A still sees one inside its open transaction. After A commits, its next read sees both. A acquired a read snapshot on its first read. B appended a new committed version to the log without rewriting the version A was reading. SQLite write-ahead logging

Avoid holding the write slot

In terminal B, use BEGIN IMMEDIATE to start a write transaction, then leave it open. Run an insert in A. The second writer can encounter SQLITE_BUSY. End B's transaction with ROLLBACK now that the experiment is finished.

A small busy timeout gives a transient lock time to clear. A CLI session can set PRAGMA busy_timeout = 500; as a starting point for the experiment. Configure the timeout separately for each application connection. Keep real transactions short and avoid waiting on a network request or a person's input while holding SQLite's write slot.

A stale read snapshot may fail to upgrade to a writer after another connection commits. In that case, release the snapshot and retry the entire read–modify–write operation from a fresh transaction, if your operation is safe to retry. Starting an operation that is known to write with BEGIN IMMEDIATE acquires the writer up front or fails early. SQLite transaction control · SQLite isolation and snapshots

Give checkpoints room to run

A checkpoint moves committed changes from the WAL back into the main database file. It can copy only the data that older readers no longer need in the log. A dashboard that holds a read transaction open for minutes can keep the file growing even when each individual write is small.

Monitor WAL size and transaction duration in the real workload. A growing WAL is a reason to check reader lifetimes before changing the checkpoint schedule. PRAGMA wal_checkpoint(PASSIVE); attempts a checkpoint without waiting for competing readers or writers. Its output can help you compare the frame count with the number checkpointed. For reliable backups, use SQLite's backup API or another supported snapshot method. Copying the main file alone while a live WAL holds committed changes can miss them. Online Backup API

Decide where the database will live

Use WAL on a local filesystem with processes on the same host. Keep a maintained SQLite release and back the database up. Measure how long writes queue during a busy session. If the application needs multiple hosts with independent writers, plan for a database server or deliberately route every database operation through one service that owns the local file.

Cover illustration by ACM MPSTME.