r/sqlite • • 5h ago

I load tested one SQLite database per user on a €5.99 Hetzner box and closing the databases is what slowed it down

I make a SaaS template called WarpKit (it's mine) that gives every user

their own SQLite file, and I wanted to know how far a €5.99 Hetzner CX23

with 2 shared vCPU and 4 GB goes.

With every user saving every 30 seconds over their own connection, it

handled 3,500 users online at the same time without losing a write. Past

that it slowed down because each worker keeps a cache of open databases,

and once a worker has more users than that, every save closes one

database to open another. Closing a database in WAL mode checkpoints

with two fsyncs, so the disk was struggling while the CPUs sat half

idle. When I gave the cache half the RAM it went to 3,600 users at

11 ms p95.

I wanted to skip the checkpoint on close but Bun doesn't expose

SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, so if anyone knows a better way to

keep thousands of WAL databases open cheaply I'd like to hear it.

Here's the write-up with all the numbers and the setup:

https://warpkit.dev/blog/sqlite-load-test-cx23

7 Upvotes

7 comments sorted by

1

u/dc_seed_sommelier 3h ago

Did you try compiling sqlite with the feature enabled to limit break connected dbs? I always wondered about that but never had cause to connect more than 100 or whatever the normal limit is.

1

u/bitclaw_ 3h ago

No, and I don't think it would have helped here, because every user's database is its own connection, nothing is attached. The compile-time limit you mean is SQLITE_MAX_ATTACHED, 10 by default and 125 at most, and it only counts databases ATTACHed to one connection.

With a connection per file SQLite itself has no limit on how many are open, so it comes down to file descriptors, three per database in WAL mode (the db, the -wal and the -shm), and Bun raises its soft limit to the hard one at startup, so that never got in the way either, I had up to 5,000 open per worker in one run.

What did limit it was memory per open database, about 0.5 MB with light use and a lot more once a user writes most of their pages, which is why I cut the page cache from 20 MB to 2 MB this week, and the close when the cache is full, since closing a WAL database checkpoints it. ATTACH is still handy for a query across users, but one connection runs one statement at a time, so I wouldn't serve requests through it.

1

u/dc_seed_sommelier 2h ago

Oh I see. You’re right I had mixed those up.

1

u/716green 3h ago

Haven't heard of Layerbase then?

1

u/bitclaw_ 3h ago

I hadn't, thanks. From what I read on their site it's hosted SQLite served over the Postgres wire protocol, which is neat if you want to reach a SQLite file from anywhere, but it's kind of the opposite trade-off

from what I was testing. Here the file sits on the same disk as the app, so a query is a function call with no network hop, and every user gets their own file, thousands of them on one box. Their plans count data

bases, 2 on the free and $5 plans and up to 10 on the $15 one, with no cap only on a dedicated server from $35 a month, so one database per user doesn't really fit unless you go dedicated. For a few shared databases it looks like a nice option though.

1

u/716green 2h ago

Why not use libSQL? Wouldn't it be faster?

1

u/bitclaw_ 2h ago

Maybe for some things, but I haven't measured it, so I honestly can't say. In this test the queries weren't what was slow, the CPUs sat half idle while the disk was busy with the fsyncs of the WAL checkpoint every time a database got closed, and libSQL is a fork of SQLite with the same WAL, so I'd expect the same cost there unless it lets you skip the checkpoint on close, which I haven't checked.

I went with bun:sqlite because it's built into Bun, runs in the same process and it's synchronous, nothing extra to install. Where I think libSQL really shines is when you want the database on a server or replicated, like Turso, and the template has a guide for moving the per-user databases there if someone outgrows one box. If you've seen it beat plain SQLite on local files it'd be great if you can point me to the benchmark.