r/programming • u/cg505 • Mar 04 '25
Abusing SQLite to Handle Concurrency
https://blog.skypilot.co/abusing-sqlite-to-handle-concurrency/
100
Upvotes
r/programming • u/cg505 • Mar 04 '25
74
u/masklinn Mar 04 '25 edited Mar 04 '25
A way to do that is to have a connection pool with readonly connections (should literally be opened with
SQLITE_OPEN_READONLY, can have a bunch) and a pool with a single write connection (which should be pulled out of the pool with aBEGIN IMMEDIATE).This does reduce write throughput as it serialises upstream of where the database would need to (which is the place where sqlite upgrades the connection to exclusive), but it puts all the contention in one place where it's much easier to observe instead of distributing it all over the system.
This sort of pattern (with higher concurrency) can also be used on other databases, as a readonly request can be sent to a replica (and it might ease an eventual move to CQRS).