Lesson 3 of 9 · 25m
Transactions, Busy Errors, and Sane Concurrency
Design transaction boundaries, retry behavior, and writer ownership so concurrency problems surface as predictable backpressure instead of corruption myths.
Most "SQLite is flaky" stories are concurrency design failures. A transaction is opened at application startup, writes happen whenever a handler feels like it, readers pin snapshots forever, and eventually SQLITE_BUSY appears. The engine did what it promised. The application never decided who owned writing.
Transactions are the unit of correctness
Every write in SQLite is transactional, even when you do not say BEGIN. The database creates an implicit transaction for each statement. That convenience hides a trap: a sequence of application-level writes may be individually atomic but collectively inconsistent.
BEGIN IMMEDIATE;
UPDATE accounts SET balance = balance - 100 WHERE id = 7;
UPDATE accounts SET balance = balance + 100 WHERE id = 9;
INSERT INTO ledger(account_id, amount, reason)
VALUES (7, -100, 'transfer to 9'), (9, 100, 'transfer from 7');
COMMIT;If the process dies after the first update and before commit, none of the transaction applies. If you run these as separate implicit transactions, half a transfer is a real possibility.
BEGIN IMMEDIATE is often the right write transaction
BEGIN DEFERRED waits until a statement actually needs a lock. That can sound efficient, but it allows an application to perform reads, decide what to write, and only then discover another writer holds the database. BEGIN IMMEDIATE asks for the write lock up front, making lock acquisition failure happen before you build application state.
Use it for write transactions where you intend to write. Keep transactions short. Do not hold one across network calls, user input, file uploads, or sleep statements.
Busy errors are backpressure
SQLITE_BUSY means the lock you need is currently unavailable. Treat it as transient congestion, not database corruption:
conn.execute("PRAGMA busy_timeout = 5000")A timeout waits inside SQLite before returning failure. For batch jobs, bounded retries with jitter can still help, especially when another process checkpoints or performs maintenance. Infinite retries are just a queue with worse observability.
Single-writer architecture beats shared chaos
The cleanest embedded pattern is one writer actor per process or per file:
HTTP handlers / jobs / workers
|
v
write queue/actor
|
v
SQLite WAL
^
|
many read connectionsReads can use separate connections. Writes funnel through one owner that batches small mutations and keeps transaction scope explicit. This is not a workaround; it is using the engine according to its concurrency model.
Field notes
- Application-level invariants need explicit transactions; implicit per-statement transactions are not enough.
- Do not open a write transaction and then perform slow external work inside it.
- Reads can see a consistent snapshot, not necessarily the newest committed state.
- A single write owner makes batching, ordering, and observability easier.
SQLITE_BUSYbelongs in metrics. If you do not graph it, you are guessing.- Avoid multiple processes writing unless you have a compelling reason and have tested lock behavior under interruption.
Was this lesson helpful?