# How to Rate Limit an API Before the Bill Arrives: What an Unthrottled Burst Actually Costs

A loop with no account spent four dollars in three tenths of a second, and opened fifty accounts in under one. Then the daily cap we added turned out to let two to five times its own limit through, and never the same amount twice.

URL: https://www.back4app.com/blog/rate-limit-an-api-before-the-bill-arrives
Published: 2026-11-20 · Tested: 2026-10-29
Tested on: Local: Docker 29.4.3 on macOS, arm64 · Parse Server 7.4.0 with MongoDB 7 · Node 22.21.1, no dependencies
Publisher: Back4app Engineering

Your app has one expensive endpoint. It calls a provider that charges per request, and it was wired up the day the feature was asked for.

**A paid call with no limit in front of it is a withdrawal form, and the person filling it in is whoever found your app.** Nothing has to be broken for this to cost money; it only has to be reachable.

So we pointed a loop at one, from one laptop, with no account and no tricks — and then added the limit, twice, because the first way of writing it did not work.

## Watch it instead

No rate limiting is number 5 in our countdown of the security holes that turn up most often in apps built with AI.

## What did the same burst cost under each limit?

Four dollars, forty cents, ninety cents and twenty cents — for the identical 200 requests:

![The same burst of two hundred requests measured four ways: with no limit all two hundred got through costing four dollars in three tenths of a second, with the cap written as look up the row check it and add one between thirty-eight and fifty got through, with the row already present between eleven and twenty-three, and with the version that adds one before checking exactly ten](/blog/blog-assets/rate-limit-an-api-before-the-bill-arrives/the-cap-that-was-not-a-cap.png)

The row to sit with is the second one. That is a cap of ten, written by someone who meant it, letting two to five times its own limit through — and a different amount almost every time.

How we measured: one Parse Server 7.4.0 backend in Docker, a provider stub that bills two cents a call, and a Node 22 script sending 200 requests with 25 in flight. The cap figures are five runs each, reported as a range because a race has no single answer. [The lab is in the repo](https://github.com/templates-back4app/ratelimit-lab).

## How fast is an unlimited endpoint, really?

Fast enough that no alert helps. This is the backend as it arrives, with the function an assistant writes when you ask for an AI feature:

```text
requests the backend accepted:                200 of 200
calls that reached the paid provider:         200
what that cost:                               $4.00 in 0.3s
the rate it sustained:                        769 paid calls per second, from one laptop
bot accounts created in one burst:            50 of 50 in 0.8s
```

Two cents a call is a modest price. The number that matters is not the four dollars, it is the several hundred per second: a budget of any size is a duration at that rate, and the duration is short.

That last line is the other door. Limiting per account does nothing if accounts are free and instant, which is why a sign-up limit and a usage limit are one control, not two.

## Why did the cap let several times its limit through?

Because of the order of three operations, and it is the kind of bug that never shows up when you test by hand. The function an assistant writes looks like this:

```js
// Stack: Parse Server 7.4.0 | File: cloud/main.js — the version that does not hold
const row = await q.first({ useMasterKey: true });
if (cap && row.get('calls') >= cap) throw new Parse.Error(141, 'daily limit reached');
row.increment('calls');
await row.save(null, { useMasterKey: true });
```

Read it as twenty-five requests arriving together. All twenty-five run the query, all twenty-five see the same count, all twenty-five pass the comparison, and only then do any of them write. The check happened before the facts changed.

```text
as written: look up the row, check it, add one
  between 23 and 50 calls got through, over 5 runs   —   $0.75 per run against a budget of $0.20
the same code, with today's row already there
  between 10 and 23 calls got through, over 5 runs   —   $0.41 per run against a budget of $0.20
```

The middle line isolates the bigger half of the problem. When no counting row exists yet, every concurrent request also *creates* one, so the burst ends up counting against several independent counters at once.

And look at the bottom of that second range. Ten is the cap, so some of those runs enforced it perfectly — the broken version is right often enough to pass a test you run once. That is the whole reason it reaches production: it does not fail every time, it fails when the traffic arrives together.

## What does a cap that holds look like?

Add one first, then look at the number you were given. Each request then has its own count instead of a shared snapshot:

```js
// Stack: Parse Server 7.4.0 | File: cloud/main.js — the version that does
row.increment('calls');
await row.save(null, { useMasterKey: true });   // the increment is the atomic part
const mine = row.get('calls');
if (cap && mine > cap) throw new Parse.Error(141, `daily limit reached (${cap} calls)`);
```

```text
add one first, then look at the number you got
  exactly 10 calls got through, over 5 runs   —   $0.20 per run against a budget of $0.20
```

Five runs, the same answer five times. That is what a limit is supposed to look like, and the contrast with the range above is the entire argument for writing it this way.

## Can you switch on the server's own rate limit?

Yes, but not the way the documentation's option list suggests, and this cost us an afternoon. Parse Server 7.4.0 refuses to start when the limit is passed as an environment variable:

```text
rateLimit must be an array of objects
```

The env-var parser for array options splits the string on commas, so a JSON array arrives as a list of fragments rather than objects. A config file works:

```json
{ "rateLimit": [ { "requestPath": "/functions/*", "requestTimeWindow": 60000,
                   "requestCount": 20, "errorResponseMessage": "too many requests" } ] }
```

With that in place the same burst was cut off at the door:

```text
requests the backend accepted:                20 of 200
requests the backend refused:                 180 (too many requests)
what that cost:                               $0.40 in 0.2s
```

## Which errors mean the limits are working?

The ones your own monitoring will report as a spike in failures, which is why people switch them off again. Each of these is the system doing its job:

`too many requests` from the server limit — a client sent more than the window allows. Legitimate if it is a bot; a bug in your app if a single user hits it while clicking normally.

`daily limit reached (10 calls)` — the per-person cap. If real customers see this, the cap is too low for your product rather than wrong in principle.

`sign in first` on a function that used to answer — expected, and the point: the capped version cannot work for an anonymous caller, because there is nobody to count against.

`no usage row for today` — the counting row was never created for this person. It means your sign-up path and your counting path disagree about when a user starts existing.

## When is a limit the wrong thing to add?

When the endpoint is cheap and the limit would be the only thing that ever breaks. A read of public data costs you almost nothing per call, and a tight limit on it mostly produces support tickets from someone on a shared office address.

The sorting rule is cost per call, not sensitivity. Anything that spends money per request — an AI provider, an SMS gateway, an e-mail sender, an image generator — needs a cap before it needs anything else. Anything that just serves a row from your own database needs a limit only when volume is the problem.

There is also a real cost to capping too early. If your limit is a guess, you will spend your first month explaining it to the people who hit it, which is a worse problem than the bill you were avoiding. Measure what a normal person does for a week, then set the cap well above it.

## What is worth doing before you close this tab?

Set a hard spending cap at your provider. Not an alert — a cap, the kind that stops serving rather than the kind that sends an e-mail. It takes two minutes and it is the only control that works while you are asleep.

Then point a loop of two hundred requests at your own most expensive endpoint and read the invoice line afterwards. If that number surprises you, you have found the thing to fix first, and you found it for the price of two hundred calls rather than two hundred thousand.
