Bugpit

Lessons › From one user to millions

Say no to a flood

Tickets go on sale: 9,000 req/s hit your 2 app servers, and the budget can't buy a third. You can't answer everyone, so make sure the people you do answer get a real response.

Play this level

Free with an account. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

An overloaded server doesn't just answer fewer people. Requests queue up, time out and get retried, so it spends its time on work nobody is waiting for. Press Run first: of 9,000 req/s, only about 1,600 get an answer, and it takes 6 seconds.

An API gateway is one front door for your servers. It can apply a rate limit: requests over the limit get an error straight away, which costs your servers nothing.

Set the limit to what your servers can really carry. Too high and they drown anyway. Too low and you turn away people you could have served.

Real gateways do more: they check who's calling and limit each customer separately, so one abuser can't use up everyone's share. Here, one limit covers all users.

What you have to achieve

9,000 requests a second at the busiest hour, 10% of them writes.

What you learn

The same 2 servers now answer almost twice as many people, and quickly, by refusing the rest. Most of the flood still failed: a rate limit protects what you have, it doesn't add capacity. If the rush is going to happen again, you still need more servers.

Check yourself

Why did refusing some requests help more people get an answer?

Show the answer

Refused requests cost the servers nothing, so they stop drowning. An overloaded server wastes time on requests that time out. A rate limit keeps it working on ones it can finish.

Where should you set the rate limit?

Show the answer

At what your servers can really carry. Too high and the servers drown anyway. Too low and you turn away people you could have served.

Try it yourself

More in From one user to millions

← Survive a crash Split the database →