Lesson 4 of 9 · 24m
Transactions and the Lock Ladder
How SQLite moves through UNLOCKED, SHARED, RESERVED, PENDING, and EXCLUSIVE states, and why deferred versus immediate transactions decide many production outcomes.
SQLite's concurrency model is not mysterious once you stop thinking in terms of threads and start thinking in terms of file locks. A transaction is not just a SQL convenience; it is the unit that determines when readers can proceed, when writers block, and what survives a crash.
Atomic commit is the core feature
A commit is all-or-nothing across tables and indexes. SQLite uses a journal to make that true. The old rollback journal copies original pages before overwriting them; if the process dies, recovery restores those pages on the next open.
BEGIN;
UPDATE accounts SET balance_cents = balance_cents - 500 WHERE id = 1;
UPDATE accounts SET balance_cents = balance_cents + 500 WHERE id = 2;
COMMIT;Either both balance updates are visible or neither is. There is no partial transfer state for another connection to observe.
Locks form a ladder
UNLOCKED -> SHARED -> RESERVED -> PENDING -> EXCLUSIVESHARED is held by readers. RESERVED declares write intent while readers continue. PENDING waits for existing readers to drain while blocking new readers. EXCLUSIVE permits modification of the database image. In rollback-journal mode, writers eventually exclude readers, so long analytical reads can block writes and long writes can stall readers.
DEFERRED, IMMEDIATE, and EXCLUSIVE are intent declarations
BEGIN DEFERRED; -- default: acquire locks lazily
BEGIN IMMEDIATE; -- acquire RESERVED now; fail fast if another writer owns it
BEGIN EXCLUSIVE; -- acquire EXCLUSIVE nowA deferred transaction can start with a read and later attempt an upgrade to write. If another writer got there first, the upgrade can fail with SQLITE_BUSY even though your process is already inside a transaction. For write-known workloads, BEGIN IMMEDIATE detects contention early and makes retry behavior predictable.
Retries belong outside the transaction body
Never blindly retry individual statements inside a transaction after SQLITE_BUSY; the transaction may already be invalid or obstructed by lock upgrade failure. Retry the whole unit of work with bounded backoff:
for attempt in range(5):
try:
with db.write_transaction():
apply_change(db)
break
except sqlite3.OperationalError:
time.sleep(0.05 * (attempt + 1))Production code should inspect the error, use jitter, and give up loudly instead of retrying forever.
Field notes
- A single write transaction touching one row still blocks every other writer because the lock is database-wide, not row-wide.
- A leaked open transaction is a concurrency bug even if no write happened yet.
- Log transaction duration before adding threads; many 'database is locked' incidents are application deadlock, not SQLite weakness.
SELECTstatements can participate in lock-upgrade failures when application logic keeps transactions open.
Was this lesson helpful?