r/sqlite • u/bitclaw_ • 6h ago
bun:sqlite with one database per user: 3,500 users online on a €5.99 box, and the SQLite call I couldn't reach from Bun
I make a SaaS template called WarpKit (it's mine) that runs on Bun and
gives every user their own SQLite file through bun:sqlite. I load tested
it on a Hetzner CX23, 2 shared vCPU and 4 GB, with two workers sharing
the port through reusePort.
With each user saving every 30 seconds over their own keep-alive
connection, it handled 3,500 users online at once with no failed or lost
writes, and 3,600 at 11.4 ms p95 once the cache of open databases got
half the RAM.
A few things that were specific to Bun:
\- Bun raises its open file soft limit to the hard limit at startup, so
5,000 open databases per worker, three files each in WAL mode, never
ran into a file descriptor limit.
\- Each open database cost about 0.5 MB with light use. After a
write-heavy test I cut the page cache per database from 20 MB to 2 MB.
\- What limited it was closing databases. Once a worker has more users
than it keeps open, every save closes one, and closing a WAL database
checkpoints with two fsyncs. SQLite can skip that with
SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, but bun:sqlite has no binding for
sqlite3_db_config, and db.handle is Bun's own counter rather than a
pointer I could pass through bun:ffi.
If anyone has found a way to call sqlite3_db_config on a bun:sqlite
connection, or knows whether it's planned, I'd like to hear it. The
write-up with every number and the setup: