Sphere
Log in Join
☆ 14

sqlite database is locked error under concurrent writers

SQLite 3.45, a small Go service with several goroutines writing. Under load I get: SQLITE_BUSY: database is locked I set db.SetMaxOpenConns(10). Reads are fine, it's concurrent writes that fail. I thought SQLite handled locking. What's the correct way to configure this? We're moving from a single script to a small service, so this is new ground and I'd rather not learn it in production.

⚑ Report

4 replies

☆ 0
G glitch ·2026-10-13#1

SQLite allows only one writer at a time. Multiple connections trying to write simultaneously give SQLITE_BUSY. Two things: enable WAL mode with PRAGMA journal_mode=WAL so readers don't block the writer, and set a busy timeout with PRAGMA busy_timeout=5000 so writers wait instead of failing immediately.

⚑ Report
☆ 0
P paper83 ·2026-10-14#2

Also, with SetMaxOpenConns(10) each connection needs those PRAGMAs set, right? pragmas are per-connection in SQLite. I set MaxOpenConns back down for the write path and added the pragma to the DSN so every connection gets it. A quick load test with twenty concurrent goroutines no longer produces SQLITE_BUSY.

⚑ Report
☆ 0
S stack ·2026-10-17#3

Correct. journal_mode is persistent in the database file so you set it once, but busy_timeout is per-connection. The reliable way in Go's database/sql is to use a DSN that applies pragmas on connect, e.g. with modernc.org/sqlite you can use _pragma=busy_timeout(5000) in the DSN, or set MaxOpenConns(1) for writes.

⚑ Report
☆ 0
R rust96 ·2026-10-20#4

Set WAL mode once, added the busy_timeout pragma to the DSN so every connection gets it, and capped write concurrency. SQLITE_BUSY is gone. For a write-heavy workload SQLite still has a single-writer ceiling, but for this service it's plenty. That is the short version, anyway.

⚑ Report
Log in to reply.