One database object under load
13 to 16 Nov 2026
- We moved work off World, the one database object. Staging then stayed healthy as players online went from about 1,000 to 8,000, with p95 at 175 ms.
- A game end used to run 844 database statements and now runs 242. World was busy for 9.2% of the hour before and 7.1% after, then 6.9% and 5.2% after a later round.
- Ids are never reused now. A sequence table hands them out at about 40 µs per insert.
On a database with a single writer, count the statements per request. Those counts compare across machines when the timings don’t.
One Durable Object, World, holds every account, map and purchase, and runs matchmaking. It answers one request at a time from its own SQLite, which is simple and race-free but does not scale alone. What worked was to move everything off World that can be answered elsewhere, then count the statements each request runs. Current state: Data and saving.
Measured how: load tests on staging with simulated players from one machine (60% signed in, 30% in background tabs), and a database benchmark that replays an hour of requests on 50,000 accounts, 5,000 maps and 500,000 player records. It runs 100 calls a second and 3,000 game ends an hour on a simulated clock. Machine load changes times but not statement counts, so we ran 4 interleaved pairs and adjusted times by 7 unchanged routes.
Off World (13 Nov)
- An edge cache answers public, mostly read lists from the Worker’s memory for a few seconds to a minute.
- Each tab makes one combined poll for presence, the online numbers, chat and the games lists.
- Presence moved to 8 shards, global chat to its own object, and daily checks off World. The site had frozen for up to a minute every few minutes.
- Pages keep data loaded in the last 30 to 60 seconds.
| Players online | Requests a second | To World a second | p50 / p95 | Errors |
|---|
| 1,000, before | 280 | 280 | 99 / 358 ms | 0% |
| 1,500, before | 441 | 377 | 2,103 / 10,111 ms | 14.7% |
| 2,000, before | 513 | 291 | 2,773 / 10,369 ms | 43% |
| 1,000, after | 133 | 22 | 38 / 90 ms | 0% |
| 2,000, after | 264 | 47 | 37 / 85 ms | 0% |
| 4,000, after | 530 | 81 | 37 / 117 ms | 0% |
| 8,000, second round | | | p95 175 ms | 0.02% |
Staging, 13 Nov 2026.
Posted that day: Ready for a crowd.
Fewer statements per request (15 and 16 Nov)
- Indexes for common lookups, short caches for boards and profiles, and merged writes took a game end from 844 statements to 242.
- A profile’s stat windows (today, week, month, all time, last 300 games, best 300) now come from one read of the newest 20,000 games, each window a prefix of it.
- Each queue’s rating and season rows come from one read each. A friends list is one statement with one JSON value. The store’s heirloom stock takes two grouped statements that join copies first, since otherwise SQLite walked every account.
- Unlisted maps, site settings (read 10 times a page) and map lists stay in memory until a write clears them. A vote now updates its map’s card instead of clearing every list. At 4,000 votes an hour, that had rebuilt the lists about once a second.
- A season place now comes from a sorted copy in memory, not a count over the whole board, and a game end reads the queue rating once instead of three times.
- Player search uses a table of name pieces (5.8 rows per account), built 2,000 accounts at a time in steps of about 12 ms. Search p50 fell from 12.7 to 0.6 ms. A map page’s numbers take one statement, not seven.
World’s busy share of the hour fell in three separate sets: 9.2% to 7.1% in round 6 (machine load 9 to 14), 5.9% to 5.4% in the sweep, and 6.9% to 5.2% in round 7 (load 7 to 12). Map stats at p99 fell from 175 to 2.6 ms, and the median signed-in map list from 6.6 to 0.6 ms. Still left: a game end with 8 players runs 239 statements, and player search 71 for the friend and block checks on its 20 results.
Cloudflare’s 100-value limit
Cloudflare’s SQLite accepts at most 100 bound values per statement, and a big settings save hit that live. A test now feeds 300 values to every listed query builder whose values grow with its input. The benchmark fails any statement over 100, as Cloudflare does, and a code scan fails any such builder missing from the list.
Never reusing ids
SQLite gives a new row the highest id plus one. When the newest account was deleted, the next sign-up got its id, while rate limits, caches, reports and ledger lines still pointed at it. AUTOINCREMENT needs a table rebuild, and the rename at its end re-checks every trigger. The ledgers’ triggers name the accounts table, so it fails after the old table is dropped. The fix is one sequence table, id_seq. Each insert takes the next id in one indexed statement of about 40 µs. The first use per table starts above the highest id stored anywhere. Nothing is rebuilt or run at startup.
| Table | Rows | First use | Statements | Each id after |
|---|
| Accounts | 50,000 | 13 ms | 234 | 41 µs |
| Notifications | 200,000 | 0.5 ms | 5 | 40 µs |
| Messages | 50,000 | 1.2 ms | 128 | 38 µs |
| Feedback | 5,000 | 1.2 ms | 127 | 38 µs |
A copy of the benchmark seed, load 3.2, at most 2 bound values per statement. A game end runs 261 statements instead of 248. World’s busy share went from 7.5 to 7.6%, within noise (load 9 to 11).
Reuse
- For a database object with one writer, count statements per route. They do not change with load, so they compare across machines.
- Never let SQLite reuse the ids of rows other data points at. With triggers, a sequence table is simpler than migrating to AUTOINCREMENT.
- Test every growing query builder at 3 times the platform’s bound-value limit.