Home › Blog

I raced my own API with 20 requests at once to see if it would lie about my credits again

· 4 min read · apicreditsrace-conditiontransparency

On November 2nd I found out our own API was quietly losing track of money. Not from fraud, not from a hacked key. From a race condition in code I had written myself, six months earlier, and never once loaded with real concurrent traffic.

Today I went back with a fresh key and 20 requests fired at the exact same millisecond, just to see if the fix actually holds or if I'd fooled myself.

What was actually broken

The credit counter behind every API key lived in Workers KV: read the current usage, add the cost of the call, write it back. Simple, and wrong for this job. KV reads can be served from a cache up to about 60 seconds old. If two calls land close together, both can read the same stale number, both add their cost to it, and both write back a result that only counts one of them. Fire enough calls fast enough and the counter stops moving at all while real usage keeps climbing.

The faster and more numerous the calls, the worse it got. A burst of 31 rapid calls against a 20-credit endpoint should have hit the free daily limit partway through and started returning 402s. Instead every single one came back 200, because the write-after-read sequence kept losing the race to itself. Keyless requests (the ones limited by IP instead of a key) were untouched, since those use a different, already-atomic limiter.

The fix

I moved the monthly credit counter and the daily free-call counter off KV and onto a small D1 table, one atomic upsert statement per call: increment-and-read in a single round trip instead of read-then-add-then-write in three. A few hours later I did the same thing to the prepaid credit balance, since it was the last counter still doing the unsafe read-then-write dance. There are no prepaid-pack buyers yet, so that second fix cost nothing to ship early and saved me from finding the same bug twice.

Today's retest

Fixing a race condition and believing you fixed a race condition are different activities. The only honest way to check is to actually race it. So today, for this post, I generated a brand-new throwaway free key from our own signup form and watched its usage counter before and after hammering it with real concurrency.

A freshly created free API key on the API Keys page, with the key value blacked out with dots
▲ The throwaway key I generated for this test, value redacted, revoked right after I finished measuring.

Starting usage, read straight from the account endpoint:

GET /api/keys/usage
{"tier":"free","monthlyCredits":1000,"used":0,"remaining":1000,"prepaidBalance":0}

First batch: 10 calls to a 20-credit endpoint, all fired in parallel with Promise.all, no stagger, no delay between them. All 10 came back 200 in 3.9 seconds total.

{"used":200,"remaining":800}

10 calls × 20 credits is 200. That's exactly what came back. Not 180, not 160 from a lost race, not a counter that forgot three of them. Exactly 200.

I wasn't satisfied with 10, since the old bug got worse as concurrency went up, so I doubled down with a second, bigger burst: 20 more calls, still fully parallel, no delay, landing in 5.3 seconds.

{"used":600,"remaining":400}

200 from the first batch plus 400 from this one (20 × 20 credits) is 600. The counter agreed, down to the credit, across two batches and 30 fully concurrent calls total. I revoked the key immediately after, the way I'd tell anyone else to clean up a throwaway test credential.

Why I'm writing this down in public

I'm not claiming the system is now bulletproof under every possible load pattern, I tested 30-way concurrency, not 3,000-way. What I can say honestly is what I measured: the specific failure mode from November 2nd, fast parallel calls undercounting themselves because of a stale KV read, did not reproduce after the fix. The arithmetic held under exactly the kind of load that broke it the first time.

The broader lesson, for anyone building a metered API of their own: a KV-style cache with eventual consistency is a fine place to store a rate-limit flag or a feature toggle. It is a bad place to store a number that has to be exactly right, like credits, balances, or anything a customer could be billed on. Those need one atomic write per change, not a read-modify-write sequence that two requests can both win at the same time. I learned that the expensive way, by checking real traffic instead of sequential test calls.

If you want to check this kind of thing on your own key, the usage endpoint is public and documented on the API key page, and the full developer docs list the cost of every endpoint. I've also written before about a case where the file API's own daily allowance meant a real call used zero credits when I expected it to cost something. Between that post and this one, the pattern is the same: read the actual response and the actual counter, don't assume the number in your head is the number on the server.


Related: two ways to check a free tool isn't sending your data anywhere.

The file tools on this site run in your browser and keep your files on your device. A few tools reach the network by design, and their pages say so: the speed test and IP lookup, online voices in text to speech, and AI features used with your own provider key. The REST API and MCP server process requests on our server. Browse all 98 browser tools or read the API docs.

More from the blog