DEV Community

What SQLite does when two processes write at once

Remdore on December 02, 2026

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_time...
Collapse
 
bpecha14 profile image
Bogdan •

Great breakdown! I'm currently using Prisma ORM with SQLite for a Next.js app (mostly for simple telemetry and waitlist forms). Have you ever run into 'database is locked' errors in serverless environments (like Vercel) with this setup? Would love to hear your thoughts on handling concurrent writes there.

Collapse
 
kevinhall profile image
Kevin Hall •

on vercel i wouldn't count on a sqlite file for anything you write, the filesystem there doesn't carry over between instances, so a hosted database is the safer spot for a waitlist

Collapse
 
bpecha14 profile image
Bogdan •

You're absolutely right, thanks for pointing that out! I actually ran into this exact limitation with Vercel's ephemeral filesystem. For local dev and prototyping, SQLite is great, but for production, I'm definitely planning to migrate to a hosted Postgres (or maybe Turso for edge SQLite). Have you had a better experience with any specific hosted DB for serverless architectures?

Thread Thread
 
remdore profile image
Remdore •

For serverless, the bigger question is how many connections the database gets. Every function instance opens its own, and a burst can open a few hundred at once. I measured that on DigitalOcean's managed Postgres a few days ago: the smallest plan has 22 usable connections, and a connection pool in transaction mode in front of it let 200 clients take turns on them. The catches are in the write-up. Prisma's docs say to add pgbouncer=true to the URL when it goes through PgBouncer in transaction mode. I haven't tested Prisma itself.

Collapse
 
token_alphabet_bb8b22df52 profile image
Token Alphabet •

The 23,197 silent duplicates are the number that stays with me, because nothing raised. A UNIQUE constraint on n is a cheap second line even after the switch to begin immediate: the same race then shows up as an IntegrityError you can log and retry. With isolation_level=None you also own every transaction, so a code path that raises between begin and commit keeps the write lock until the connection closes, and a small context manager that always commits or rolls back is worth adding.

Collapse
 
remdore profile image
Remdore •

Both worth doing. The UNIQUE constraint turns a silent duplicate into an error you can see, which is the whole problem with the default mode. I checked the context manager in Python 3.13 with isolation_level=None: with conn: after a manual begin immediate commits when the block finishes and rolls back on an exception, so you don't need to write your own. Without it, the connection that raised stayed in its transaction, and a second connection with timeout 0 got database is locked until I rolled back.

Collapse
 
smtahosin profile image
S M Tahosin • • Edited

Starvation figures surprised me, my reporting task plus website on single SQLite file gave slow writes without error, lock convoy holder keeps winning, busy_timeout fairness breaks mostly. I will keep an eye out on the single writer front also, Python default duplicates side I have not tried properly yet.

Collapse
 
milochen0418 profile image
Milo Chen •

23,197 duplicates and zero errors. That run is scary, and I like that you ran it three times. I didn't know isolation_level="IMMEDIATE" still sends the BEGIN after the SELECT.

I use stdlib sqlite3 in WAL mode as the only store for a self-hosted Slack-like chat. My own WAL surprise was a silly one: the -wal and -shm files sat in the project root, Reflex's dev hot reload saw them change, and it restarted forever. I moved the database out of the project folder.

In your own apps now, do you send BEGIN IMMEDIATE on every write, or only where a read decides the write?

Collapse
 
remdore profile image
Remdore •

Only where a read decides the write, if you go by what the tests showed. A transaction whose first statement is the write takes the write lock at that statement, and busy_timeout covers the wait: the plain BEGIN; INSERT; COMMIT run had 6 errors in 230,662 commits. The instant errors only came when a SELECT ran first in the same transaction. Sending BEGIN IMMEDIATE for every write transaction is still simpler than deciding per code path, and a transaction that only writes loses nothing by taking the lock at BEGIN instead of at the INSERT.

The hot reload story is a good one. A file watcher is exactly the kind of thing that sees -wal and -shm change on every write.

Collapse
 
shashank_tickjury profile image
Shashank Tickjury •

The 23,197 duplicates with zero errors is the part that got me. I run a small Python site on SQLite in WAL mode. The step that spends a user's credits starts with begin immediate, so two clicks can't spend the same credits. Did the default connection give duplicates with the rollback journal too, or only in WAL?

Collapse
 
remdore profile image
Remdore •

Both. With the rollback journal the default connection gave 2,851 duplicates out of 7,714 commits, about 37%, against 23,197 out of 40,345 in WAL. Fewer in total because the rollback journal got through far fewer transactions in the same ten seconds, but it's the same cause: the SELECT runs outside the transaction in either mode. Your credits step is exactly where begin immediate belongs.