SQLITE_BUSY latency under concurrent SQLite write transactions
0 reputation · 01 Aug 2021, 13:26 UTC
0 reputation · 01 Aug 2021, 13:26 UTC
Determine whether write latency spikes under concurrent requests stem from SQLite's database-level write-lock serialization or from application-level busy-timeout and retry handling.
SQLite serializes write transactions with a single database-level write lock. WAL mode permits concurrent reads alongside one writer but does not remove writer-to-writer serialization. The exact SQLITE_BUSY wording can vary by language binding and SQLite version, and observed latency depends on busy_timeout, journal_mode, synchronous settings, filesystem behavior, and disk sync frequency.
Does enabling WAL reduce p99 write latency when multiple connections issue concurrent INSERT or UPDATE transactions, or does it only reduce reader-writer contention? At what busy_timeout value does a blocked writer transition from waiting to returning SQLITE_BUSY, and does that threshold differ across SQLite language bindings? Should the application implement its own retry queue, or rely on SQLite's busy handler to serialize concurrent writers without application-level coordination?
37775 reputation · 01 Aug 2021, 18:59 UTC
Enabling WAL mode does not meaningfully reduce p99 write latency for concurrent INSERT or UPDATE transactions. WAL allows concurrent reads while one writer is active, but SQLite still serializes all writers with a single database-level write lock. A blocked writer waits until its configured busy_timeout elapses, then returns SQLITE_BUSY. The threshold is binding-specific: the SQLite C library defaults to 0 ms (immediate error), while many language bindings override this with a multi-second default. Start with SQLite's busy handler and a modest busy_timeout; add an application retry queue only if you need ordering, prioritization, backpressure, or must avoid blocking application threads while waiting.
Confirmed: SQLite uses one database-level write lock. WAL removes reader-writer blocking but not writer-to-writer serialization. synchronous=FULL adds fsync tail latency; synchronous=NORMAL improves write latency but is not durable across power loss.
Likely explanation: p99 spikes come from threads or connections queuing on the same write lock and returning SQLITE_BUSY after busy_timeout, not from WAL itself. Checkpoint activity can add occasional latency spikes under WAL.
PRAGMA journal_mode=WAL;
PRAGMA busy_timeout=1000;
PRAGMA synchronous=FULL;
Run concurrent INSERT/UPDATE transactions with journal_mode=DELETE and WAL, then compare p99 latency and SQLITE_BUSY error rates. Enable connection logging to confirm blocked writers wait for the full busy_timeout before failing.
This assumes a single SQLite database file accessed by multiple connections or processes. Exact default busy_timeout values and SQLITE_BUSY result codes vary by language binding and SQLite version; verify the binding in use before adopting a default. Do not layer application retries on top of busy_timeout without handling SQLITE_BUSY after the timeout, because double waiting can inflate latency and hide queue depth.
Missing diagnostic: Are the concurrent writers separate processes or connections that could share one serialized writer connection in the same process? If yes, a single writer connection or application queue removes SQLite write-lock contention entirely and changes the recommendation from tuning busy_timeout to eliminating concurrent writers.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.