Why SQLite write retries can hide uneven progress

ProjectsCommunity Digest0 replies

Community DigestMember#1

In What SQLite does when two processes write at once, Remdore runs eight Python processes against one SQLite database on an M4 laptop. The experiments distinguish three outcomes that a single throughput number can hide: whether statements fail, which process gets work done, and whether a successful write is based on a current read.

Look at commits per worker

SQLite permits concurrent readers but serializes write transactions, including in write-ahead logging mode. In Remdore's tight insert loop with WAL and a five-second busy timeout, the author reports 230,662 commits and six lock errors. One process accounted for about 80 percent of those commits. Waiting reduced the number of errors in that run while leaving substantial differences in progress between workers. Adding a short pause between transactions changed the distribution. The article's ten-second runs on one machine make the distribution a useful measurement to reproduce with the intended workload. SQLite transaction documentation

Put the first read inside the transaction

Remdore next has each worker read the largest value in a column and insert its successor. In the Python legacy transaction mode used in the experiment, the driver's implicit write transaction begins at the insert. Two workers can therefore read the same maximum and both successfully insert the same successor when the schema permits duplicates.

Starting an explicit deferred transaction groups the two statements but can still fail at the write when SQLite cannot upgrade the read transaction. In WAL mode, a snapshot made stale by another commit cannot become the basis of a new write transaction. Remdore's explicit BEGIN IMMEDIATE moves writer acquisition ahead of the read. This places contention at the point where the application has yet to compute a successor, and the transaction documentation still allows a busy error at the begin statement.

The two tests suggest different signals to collect. For a worker pool, record per-worker commit counts alongside aggregate throughput. For a read-derived write, inspect transaction boundaries and check the resulting values, including when every insert reports success.

No replies yet