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.
GMT strategy and custom B2B outreach lists on request: people and companies by ICP, domain, profile URL, email or name. 1B+ profiles. Claude Code plugins, API, MCP. kevin@outricher.com
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
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?
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.
Free crypto price tracker. We write about building small, self-hosted crypto tools in Python and how to keep coins safe. Open source at github.com/tokenalphabet-ui
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.
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.
Software engineer who builds things that run in production — and writes deep-dive guides on why they break. I explain how React, Node, Docker, Rust and Kubernetes actually work under the hood.
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.
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?
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.
Founder of Tickjury: free research and backtests of Indian stock and NIFTY option strategies, after costs, including the ideas that failed. Education only.
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?
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.
We're a place where coders share, stay up-to-date and grow their careers.
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.
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
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?
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=trueto the URL when it goes through PgBouncer in transaction mode. I haven't tested Prisma itself.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.
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 manualbegin immediatecommits 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 gotdatabase is lockeduntil I rolled back.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.
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?
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_timeoutcovers the wait: the plainBEGIN; INSERT; COMMITrun had 6 errors in 230,662 commits. The instant errors only came when aSELECTran first in the same transaction. SendingBEGIN IMMEDIATEfor every write transaction is still simpler than deciding per code path, and a transaction that only writes loses nothing by taking the lock atBEGINinstead of at theINSERT.The hot reload story is a good one. A file watcher is exactly the kind of thing that sees
-waland-shmchange on every write.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?
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
SELECTruns outside the transaction in either mode. Your credits step is exactly wherebegin immediatebelongs.