Bugpit

Lessons › Keep it running

Someone else's limit

A delivery app on a Saturday evening: 7,000 req/s, and about 1 in 110 asks for a route quote while the customer waits. That's 63 quotes a second. Your Maps API quota is 3,000 calls a minute, 50 a second, and the provider won't raise it before tonight. Over the quota it refuses the call, and your app asks again straight away.

Play this level

Part of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

A quota is someone else's capacity. Past it, the provider refuses the call at once with 429 Too Many Requests, often with a `Retry-After` header that says when to come back. Asking again straight away is one more refused call, and it counts against the same quota.

Run traffic and look at the Maps API: customers need 63 quotes a second, but retries send far more. Those retries go through your own servers too, which now work hard for answers nobody gets.

First, stop hammering. Click **Users** and tick **Back off on retries**: wait longer after each failure, at a random moment, and wait as long as `Retry-After` asks when the provider sends it. Far fewer calls are refused, but some still fail, because 63 still don't fit in 50.

Then decide what the customer sees when the provider says no. Click **Maps API** and tick **Fall back when refused**: show a rough arrival time worked out from the distance, and fetch the exact route later. A slightly worse answer beats an error.

What you have to achieve

7,000 requests a second at the busiest hour, 4% of them writes.

What you learn

The quota held and nobody saw an error: 50 quotes a second got an exact route and the rest got a rough estimate. Backoff kept your servers standing when one of them crashed. The same pair protects any outside API, including a scraper's target: reuse answers you already have, spend the quota on what matters most, and ask for a higher limit long before the busy day, not during it.

Check yourself

The provider answers 429 with Retry-After: 30. What should your client do?

Show the answer

Wait about 30 seconds, plus a little randomness, then try again. The provider has told you when it will have room. Coming back sooner is refused again and uses up more of your quota. The randomness stops every client returning in the same second.

A scraper has 10,000 pages to fetch, and the site allows 10 requests a second. What's the steady way to do it?

Show the answer

Put the pages in a queue and let a worker take 10 a second. A worker that paces itself spends the limit exactly and nothing is refused. It takes 1,000 seconds either way; hammering only adds refusals and can get you blocked.

When is a fallback answer the wrong choice?

Show the answer

When the rough answer could cost someone money, like a payment result. A rough arrival time is harmless. A guessed payment result isn't. Those calls should wait in a queue and be done properly, with an idempotency key.

Try it yourself

More in Keep it running

← Two primaries The PDF that ate the quota →