TACG launch night and the $20 spend cap
EngineeringDec 5, 2026Updated Dec 6, 20267 min read
TACG is my browser version of a fan-made Arknights card game. Two of its failures were in the parts meant to keep it safe to run: the queue that saves each finished match, and the meter that closes online play before it can run up a bill.
4 ways saving a match record went wrong launch day, Oct 18, 2026
$20.01 vs ~$5 the meter's estimate vs real usage at list prices Nov 8, 2026
~27 min online play closed by the spend cap Nov 8, 2026
How this was builtClaude Code and OpenAI Codex wrote the code and traced the failures under my direction. I chose Cloudflare and Supabase and set the hard cap of $20 a month. I gave the go for production on Oct 18 after staging showed one saved row per match, and on Nov 8 I raised the cap once Cloudflare's own usage numbers put real usage at about $5. The lead agent made and fixed the Hyperdrive mix-up on launch night. The source code is private.
Contents6 sections
01Two parts that exist for safety
TACG runs online matches in Cloudflare Durable Objects. Each room has its own Room object that runs the rules, and a single Lobby object pairs players.
Two more parts sit beside that. When a match ends, its record (the seed, the settings, and every action) goes to a Supabase Postgres table, so a match can be replayed when a player reports a bug. And because Cloudflare has no hard billing cap, the Lobby keeps a running estimate of the month's cost and closes online play when the estimate reaches a limit I set.
Both parts failed. Neither failure was in the game rules.
02Oct 18: four ways saving a record went wrong
Launch day, Oct 18, 2026 (Eastern time)
- Fixed 18:43
No records arrive
On the first staging deploy, matches played to the end and no row reached Postgres. The likely cause was that the write was still running when the request handler returned, and the runtime cancelled it. The fix was an outbox: each record is saved in the room's own storage first, and the send is retried until it succeeds.
- Fixed 18:54
A certificate the Worker would not accept
With the outbox, the rows queued up and still did not send. The same connection string worked from my machine. Supabase's connection pooler presents a certificate that chains to Supabase's own certificate authority, and the Workers runtime verifies the chain and refused it. The fix was Cloudflare Hyperdrive, which makes the database connection from Cloudflare's side.
- Fixed 19:35
One match, three rows
Records arrived, and one match arrived three times. A Durable Object interleaves handlers at every await, so a message handler and the room's alarm could each read the same queued row and send it before the other deleted it. Now a send that starts while another is running joins it, the insert skips a row that already exists, and a unique index backs that up. Staging then showed one row per match.
- Live 23:38
Production goes live, and records hang
Production opened behind a shared tester login. There the sends neither failed nor finished: 0 tries logged, 0 errors, 0 rows. The lead agent had pointed the production Hyperdrive at the pooler address, and staging used the direct database address. Pointing production at the direct address fixed it, and the next test matches saved.
The first three times are when each fix was committed. Each fix let the write get one step further and exposed the next problem. At most one record was lost that night, and it came from a test room.
03How a record is saved now
- RoomA match ends
- Room SQLiteQueue the record
- One at a timeSend
- CloudflareHyperdrive
- Supabase PostgresInsert once
- Room SQLiteDelete the row
- Queue
- The record is written to the room's own SQLite storage in the same step that ends the match. A room that restarts in the middle of a send still has the row.
- Send
- One send runs at a time, and a second caller joins the one in flight. The row is leased before the send starts, so an alarm that fires during the network wait does not send it again. A send gets 20 seconds, because the Postgres client has no query timeout of its own.
- Connect
- Hyperdrive makes the connection from Cloudflare's side, to the direct database address.
- Insert
- The insert skips a row that already exists for the same environment, match code, match number, and start time. A write that lands late or twice changes nothing.
- Clear
- The queued row is deleted once the insert succeeds. A failed send waits 30 seconds, then 2 minutes, then 10 minutes, then 1 hour between tries. After 30 tries the row is dropped with a log line, so a dead database cannot keep a room alive for ever.
The table has a size limit of 200 MiB. Each insert takes a transaction-level advisory lock, reads the real size of the table, and skips the write if it would pass the limit. A skipped record is dropped from the queue with a log line. The check comes before the write because Postgres does not give the space back when an insert rolls back.
04Nov 8: the spend cap closes the game
The cap was $20 a month, which included the $5 Workers Paid plan. At 16:59 Eastern on Nov 8, 2026 the meter's estimate reached $20.01. The Lobby closed online play and disconnected every lobby socket. By design, matches already running are allowed to finish.
Cloudflare's own usage numbers for the same days, priced at list prices, added up to about the $5 plan fee.
Cloudflare's log figure covers the 7 days it keeps. Worker requests matched: 1,796,316 counted by Cloudflare against about 1.77 million on the meter.
The meter was built to guess high. Each Worker isolate, one running copy of the Worker code, paid in advance for a block of work: 100 requests, 4,000 log lines, and 300 R2 writes plus 300 reads. The R2 calls come from journal uploads, small batches of diagnostic events that a player's page sends to R2. Isolates rarely lived long enough to use a block. One handled about 4.5 journal uploads on average, and the rest of its block stayed on the estimate.
Nov 8, 2026 (Eastern time)
- 16:59
Online play closes
The estimate reaches $20.01 against the $20 cap.
- By 17:15
The gap is measured
The agent on watch compares the meter with Cloudflare's usage numbers and finds that real usage comes to about the $5 plan fee.
- 17:25
I raise the cap to $50
A release with the new cap goes live. The Lobby picks it up at 17:26 and online play reopens, about 27 minutes after it closed.
- By 17:58
The meter is rebuilt
The new code counts the calls an isolate really makes, and it has passed CI. A replay of that week through it comes out at about $5.
- 18:43
The new meter goes live
It ships after a review and a staging pass. One live match runs through the deploy and another through the restart five minutes later, and both finish normally.
- 18:52
The estimate is reset
From $21.43 to $5.00, using fresh numbers from Cloudflare.
05How the meter counts now
- Every log line and every R2 call is counted when it happens and settled with the isolate's next report to the Lobby.
- A journal upload pays in advance for exactly its normal path: 3 writes and 4 reads.
- A test replays the week of Nov 1 to 8, 2026 through the meter at 1/20 scale.
- The Lobby logs a warning at 50%, 75%, and 90% of the cap.
- An admin command resets the estimate to Cloudflare's real numbers.
The meter is still built to err high. In that replay each unit came out between 1.0 and 1.5 times the real count. It is not a guarantee: with a different mix of log lines, the same replay counted 0.94 times the real number. It also fails closed: a journal upload that cannot reserve its cost with the Lobby is not stored.
As of Nov 8, 2026 the game's usage came to about $0.35 a month on top of the $5 plan. The cap has stayed at $50 since then.
06What the two failures had in common
- Both were in safety code. One part protects match data and the other protects my bill, and the game rules were not involved in either failure.
- Both looked fine from the wrong place. The database write worked from my machine and failed from a Worker. The meter looked safe because it guessed high, and its gap from Cloudflare's numbers was measured after it had closed the game.
- Each was settled by a number from outside the system: rows in the Postgres table, and Cloudflare's own counts.