Back to blog

One user uploads 10,000 photos a minute: where do you draw the line?

Thắng Đoàn
Thắng Đoàn

A FotoFlow customer's export script has a bug: it uploads in a tight loop, 10,000 photos a minute, 5,000 requests a second at its peak. Nobody attacked anyone. But at 300,000 users the API servers spend nearly all their time on one tenant, everyone else's requests queue behind the flood and time out. Shared capacity needs a fairness rule. Without one, the noisiest client wins.

A budget at the front door

Put a rate limiter in the gateway in front of the API. Each client, identified by API key, user id or IP, gets a budget: 10 requests a second, say. Requests within budget pass. The rest are rejected immediately with 429 Too Many Requests, before they cost the API anything.

Rejecting early is cheap: the gateway answers in microseconds, and the API never sees the excess. The same flood now costs the script's owner nothing but 429s.

The token bucket

The common counting algorithm is the token bucket. Each client has a bucket holding up to 10 tokens, refilled at 10 per second. Every request takes one; an empty bucket means 429. A real user's bucket stays mostly full; the script drains its bucket in the first instant and it stays empty.

Two numbers do the work: bucket size allows a short burst, refill rate caps the long-run average. A fixed window, by contrast, counts per calendar minute, so a client can spend a full minute's budget at 12:00:59 and again at 12:01:00: double the limit in two seconds. Sliding windows smooth that edge; use those or buckets.

Everyone must count together

One gateway is itself a single point of failure, so you run several. If each counts alone, a client hitting 10 gateways gets 10 times its limit. Gateways keep counters in a shared, fast store like Redis: each request does one atomic increment and checks the total.

That shared hop adds about a millisecond per request. Some systems trade exactness for speed by syncing local counters every second instead. Both are fine; silently multiplying limits across gateways is not.

A good 429 also teaches: a Retry-After: 30 header plus headers showing the limit and what is left. Well-behaved clients slow down and come back; the script's owner fixes their loop when they see the pattern. Publish the limits in the docs, and sell higher tiers to customers who genuinely need more.

What this does not solve

Per-user limits cannot see aggregate abuse. One customer runs a nightly import with 10,000 well-behaved users, each under their own limit, together eating 70% of your capacity every night. Add per-tenant limits sized by plan on top: layer user, tenant, endpoint (expensive endpoints get lower limits) and a global ceiling.

And rate limits only police individuals. On launch day everyone arrives at once, total traffic above what you can serve, and every client is well within its rights. Then load shedding decides what to drop: background work like prefetching, recommendations and analytics gets rejected first, so logins and checkouts keep working. Failing some requests on purpose beats failing all of them by accident.

One buggy script outranked 300,000 users. What fairness rule was missing, and who else could exploit its absence?
Launch day traffic is above capacity and every request is legitimate. What would you drop to keep logins alive?

The flood is contained at the door. Next in the series: A downstream call hangs: how long do you wait before giving up?

Sources

OWASP: Denial of Service Cheat Sheet

Wikipedia: token bucket

MDN: HTTP status codes

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.