A lot of small apps now run on SQLite in production, and the advice that comes with it is always the same three lines: turn on WAL, set a busy_timeout, and you're fine with several processes writing. I wanted to see what "fine" looks like when eight processes write to one file at the same time, and what still goes wrong after you've done all three.

The setup

An M4 MacBook, Python 3.13 with its bundled SQLite 3.54.1, and one table:

create table t(id integer primary key, n int, who int, pad text); 

Each test starts eight separate processes, not threads, each with its own connection, and runs them for ten seconds. A writer does BEGIN; INSERT; COMMIT in a loop and counts what worked and what failed. I turned Python's own lock wait off (timeout=0) and set PRAGMA busy_timeout directly, so the only waiting is SQLite's. One caveat up front: on macOS a normal fsync doesn't force the data onto the disk, so most of these commits are much cheaper than they would be on a Linux server. I come back to that near the end.

No busy_timeout

With the default rollback journal and no timeout, the eight writers managed 160 commits between them in ten seconds. They also got 2,746,677 database is locked errors. Without a timeout SQLite doesn't wait at all. If someone else holds the lock, you get the error straight away, and a tight loop turns that into millions of errors. The processes were hitting the lock so often that they mostly got in each other's way.

WAL mode with no timeout looked better, 95,793 commits, but still 2,012,510 errors. Without a timeout, two processes trying to write at the same moment is an error every time, whatever the journal mode. Keep one number from this run in mind, though: the commits were split almost evenly, between 11,764 and 12,096 per process.

With busy_timeout = 5000

This is the setting everyone recommends, and the errors almost went away. In WAL mode, 230,662 commits and 6 errors in ten seconds. Then I looked at which process did the commits:

commits per process: 184371, 37493, 5312, 3481, 2, 1, 1, 1 

One process did 80% of the work and four did next to nothing. Without the timeout, everyone got a fair share and lots of errors. With it, the errors went away and so did the fairness. In the rollback journal run it was worse:

commits per process: 66069, 1, 1, 1, 1, 1, 1, 1 

Seven processes committed once each and then sat in their busy handler for the whole run. Each of them eventually gave up after 5.2 seconds with database is locked, retried, and waited again.

The reason is how the busy handler waits. It doesn't queue. A process that finds the lock taken sleeps for a moment and tries again, and the sleeps get longer, up to 100 ms each (that's from SQLite's source, sqliteDefaultBusyCallback). Meanwhile the process holding the lock commits, starts its next transaction a few microseconds later, and takes the lock again before anyone else wakes up. There is no fairness. Whoever is already writing keeps writing.

A tight loop of inserts is the worst case, so I added a 1 ms pause between transactions, which is closer to an app doing some work between writes. That fixed most of it. In WAL mode each of the eight processes got between 2,597 and 3,326 commits, no errors, p99 at 21.7 ms. With the rollback journal it was still uneven, from 556 to 2,858 commits per process, and one commit took 2.5 seconds.

So busy_timeout stops the errors, but it doesn't make anyone wait their turn. If one of your processes writes in a loop, like a worker draining a queue or an import script, the rest of your app can wait for seconds behind it.

Reading, then writing

The next test is the one that matters most for real code. Each writer reads first and then writes something based on what it read:

BEGIN; SELECT max(n) FROM t; INSERT INTO t(n) VALUES (<that max + 1>); COMMIT; 

With a plain BEGIN, which is BEGIN DEFERRED, and a 5 second busy_timeout, WAL mode gave me 12,451 commits and 34,148 database is locked errors. The timeout was set, but in a separate run that timed each failure, the median error came back after 0.3 ms and the slowest after 7 ms. Nobody waited five seconds.

BEGIN DEFERRED starts as a read transaction. The SELECT takes a snapshot of the database. When the INSERT wants to turn it into a write transaction, SQLite checks whether someone else committed since that snapshot. If they did, the read you did is out of date, and waiting won't fix that, so SQLite returns the error at once without calling the busy handler. In Python 3.11 and newer, e.sqlite_errorname tells you which case it was: about 60% of the errors were SQLITE_BUSY_SNAPSHOT, the rest plain SQLITE_BUSY, and both came back just as fast. The rollback journal does something similar for a different reason: two readers both wanting to become the writer would wait for each other forever, so SQLite gives one of them an error instead of letting it wait. That run had 13,294 errors, with a median of 1.4 ms before each one.

Changing BEGIN to BEGIN IMMEDIATE, which takes the write lock at the start, removed every error in both modes. WAL went from 1,245 to 1,724 commits a second, because nobody was throwing work away any more.

What Python's sqlite3 does by default

This is the part I didn't expect. Most Python code doesn't write BEGIN at all. It opens a connection with sqlite3.connect(path), runs its statements and calls conn.commit(). So I ran the same read-then-write loop that way:

c = sqlite3.connect("pd.db") n = c.execute("select max(n) from t").fetchone()[0] c.execute("insert into t(n) values (?)", (n + 1,)) c.commit() 

In WAL mode: 40,345 commits, zero errors, and 23,197 duplicate values of n. More than half the inserts were based on a number some other process had already used. Nothing raised an exception. I ran it two more times and got 24,222 and 24,303 duplicates.

The default sqlite3 connection doesn't open a transaction for a SELECT. It only sends BEGIN right before an INSERT, UPDATE, DELETE or REPLACE. So the SELECT ran on its own, then the insert got its own short transaction, and between the two another process was free to insert the same number. Each statement was perfectly atomic. The read and the write together weren't.

isolation_level="IMMEDIATE" sounds like the fix and isn't: 23,063 duplicates, because the BEGIN IMMEDIATE is still only sent before the INSERT, after the SELECT already ran. autocommit=False, the newer mode added in Python 3.12, does put the SELECT inside a transaction. That gave zero duplicates but 38,121 errors, because it opens with a deferred BEGIN, the same as the test above.

What worked was taking control of the transaction myself:

c = sqlite3.connect("pd.db", isolation_level=None) c.execute("begin immediate") n = c.execute("select max(n) from t").fetchone()[0] c.execute("insert into t(n) values (?)", (n + 1,)) c.execute("commit") 

Zero duplicates and zero errors in both journal modes, and the same in all three WAL runs.

Readers and writers together

The other thing WAL is supposed to fix is readers getting in the way of writers. I ran four writers with a 1 ms pause and four processes reading count(*) as fast as they could.

With the rollback journal the writers managed 109 commits a second. A writer has to wait until no one is reading before it can commit, and with four busy readers that moment rarely comes. The p99 commit took 880 ms and the slowest 3.4 seconds. In WAL mode the same writers did 1,781 a second with a p99 of 10 ms, while the readers kept going. If you only change one setting, this is the one, and it's the main reason the WAL advice exists.

WAL has its own way to get stuck

WAL mode writes new pages to a separate -wal file and copies them back into the database in a checkpoint. The WAL file can only start over from the beginning when no reader is using it. So I ran two writers plus readers that each held a read transaction for half a second, starting the next one right away.

With no readers, the WAL file stayed at 4.1 MB for 30 seconds. With one reader that never paused, it reached 159 MB in 30 seconds while the database itself was 7.5 MB, and it kept growing at the same rate. A 50 ms gap between that reader's transactions was enough to keep it at 6.3 MB. Once the readers stopped, PRAGMA wal_checkpoint(TRUNCATE) brought it back to zero.

Long-running read transactions are easy to have without noticing, for example a report query, a cursor someone forgot to close, or a connection pool that keeps a transaction open. They don't block writers, but the WAL file keeps growing until they finish.

When fsync actually flushes

Everything above used macOS's default fsync, which leaves the data in the drive's cache. PRAGMA fullfsync=1 makes SQLite flush to the disk itself, which is closer to what a Linux server pays for every commit. The same eight writers with a 1 ms pause:

  • Rollback journal: 1,703 commits a second dropped to 74, with a p99 of 2.8 seconds.
  • WAL: 2,245 dropped to 165, with a p99 of 1.1 seconds.
  • WAL with synchronous=NORMAL: 426, p99 250 ms.

The slowest commits took 4.2 to 4.5 seconds, close to the 5 second timeout. When every commit really waits for the disk, the lock is held much longer, and everything about waiting gets worse. synchronous=NORMAL in WAL mode doesn't flush on every commit, only at checkpoints. You can lose the last few commits if the power goes, but the database won't be corrupted, and that's a trade a lot of apps can accept.

What I'd take from it

  • Use WAL. Readers stop blocking writers, and that was 109 against 1,781 commits a second in my test.
  • Set busy_timeout, but don't expect it to be fair. A process writing in a loop can keep everyone else waiting. Keep write transactions short and leave some time between them.
  • If a transaction reads and then writes, start it with BEGIN IMMEDIATE. Otherwise busy_timeout does nothing for it.
  • In Python, don't rely on the default transaction handling for read-then-write. Use isolation_level=None and send BEGIN IMMEDIATE yourself.
  • Don't keep read transactions open. The WAL can't reset while one is running.
  • Measure with real flushes. A Mac's default fsync made commits look 13 to 23 times faster than they were with real flushes.

Caveats

One machine, an M4 laptop with a fast SSD, and a table with almost nothing in it. Ten second runs. A Linux server with a different disk will give different numbers, especially in the fsync section. The busy handler details are from the SQLite version Python 3.13 ships with. The read-then-write test with max(n) is a stand-in for any "read a value, decide, write" logic; the duplicates come from the transaction boundaries, not from that query.