Back to blog
engineering

My SQLite template choked at 1,000 users on a small server, and it was one setting

By Daniel Chavez

I was writing a post about how many users WarpKit can handle, and I wanted a number from a server people actually rent for a side project, so I load tested it on a Hetzner CX23, €5.99 a month, 2 shared vCPU and 4 GB, with the template exactly as it ships. I expected much more than 1,000 users. At 1,000 users each saving every 3 seconds, p95 was 9.7 seconds.

To be honest my first thought was that my template wasn't fit for production. It never showed up on my live sites, only in the test, but the test is the thing I'd point people to, so I had to find out why.

Why didn't I catch it before?

I had a benchmark already, from October, and it looked great: 5,000 users on one server, p95 5.7 ms. But that server was a CCX33 with 8 dedicated cores and 32 GB, €117 to €138 a month depending on the location, and for that run I had raised USER_DB_MAX_CONNECTIONS to 6,000. So I had never tested the default on the kind of box most of us start with.

What was the cause?

WarpKit gives every user their own SQLite database, and each worker keeps a cache of open databases. The default cap was 200 per worker. Past that, every request closes one database to open another, and closing a SQLite database in WAL mode writes its log back into the database with two fsyncs. On the CX23 the disk was the one waiting, 20 to 28% iowait, while the CPU sat at 40 to 45%.

The load test makes this worse than real life, because it spreads each user across both workers, so each worker sees all of them. With real browsers each worker would see about half. Still, 400 users was where it fell over, and that's not a number I want to put my name on.

What did I try?

First I raised the cap to 2,200 by hand, and the same box carried 2,000 users at p95 166 ms. So the box was fine, the setting was the problem. But at 2,200 each worker used about 1 GB, and WarpKit apps build on the server they run on, through RunMist, and a build needs up to 1.9 GB. The two don't fit together in 4 GB, so a fixed bigger number would only move the problem.

Then I looked at closing a database without writing the log back. SQLite has an option for it, but Bun doesn't expose it, and the workarounds I tried either kept the fsyncs or made a crash less safe. I'm not going to trade durability for a benchmark, so I left that alone.

What did I change?

The template now sizes the cache from the server's memory when it starts, a quarter of the RAM when the app builds on the same server, as RunMist does, and half when it builds somewhere else, using the memory limit when the app runs under one, split across the workers at about 0.5 MB per open database. On a CX23 under RunMist that's 956 per worker. On a big server it goes up to 5,000, and you can still set the number yourself.

A quarter leaves room for the build. I ran a production build on the same box while it was serving 1,000 users with full caches, and it peaked at 1.23 GB and nothing got killed, although p95 went to 2.75 s while the build had both vCPUs.

And now?

Same box, same nonstop test, defaults:

Users saving every 3 sp95 beforep95 now
40050 ms11.6 ms
1,0009.7 s17.1 ms
1,400not tested90.8 ms

1,400 is where the CPU runs out on that test now, which is where I want the limit to be. Then I ran something closer to real use, every user on their own connection saving every 30 seconds, and the same €5.99 box carried 3,500 users online at once with the defaults. With the build moved off the box and the cache given half the RAM, 3,600 users ran at 11 ms p95 with the CPUs two thirds idle. No write was lost in any of the runs. The full numbers and the setup are in the benchmark post on warpkit.dev.

What I took from it is to test the defaults on the cheapest box someone would really use, because a benchmark on a big server with a tuned setting told me nothing about the people I built this for, the little guys like me trying to squeeze the most out of their little Hetzner boxes and scale cheaply for as long as possible. The fix is in WarpKit and in all my sites that run on it.