NGINX
medium 28 minNGINX (say "engine x") is a web server, reverse proxy, load balancer and response cache.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Two or more app servers | A reverse proxy with an upstream group | One address, failover, TLS in one place |
| Slow mobile clients | Response buffering | App workers are freed in milliseconds |
| A spike on public pages | Micro-caching with a lock | The backend sees one request per key per second |
| Abuse of one endpoint | limit_req per address or key | Refuse early; say it is per machine |
| Only AWS, small team | A managed balancer | Nothing to patch or scale |
| Hundreds of services that move | Envoy or a mesh | Discovery by API instead of files |
Options
Compare your moves
A public product page gets 10,000 requests a second and the app manages 500. What do you do?
BreaksRaise timeouts
- How
- Let requests wait longer in NGINX.
- Limit
- The queue grows by 9,500 a second and everything times out.
- Choose when
- Never for overload.
WorksAdd app servers
- How
- 20 servers at 500 each, plus spare ones.
- Limit
- Expensive, and the database still gets 10,000 reads a second.
- Choose when
- Answers are different for every user.
StrongCache one second with a lock
- How
- About one refresh per key per second and machine, stale copies while updating.
- Limit
- Data is up to a second old; keep private pages out.
- Choose when
- The page is the same for everyone.
How do you enforce 100 requests a minute for each API key over four NGINX machines?
BreaksThe same limit on every machine
- How
limit_reqwith rate=100r/m on each.- Limit
- A key can use 400 a minute.
- Choose when
- Only rough protection is needed.
WorksHash keys to machines
- How
- The balancer in front sends each key to one machine.
- Limit
- A hot key overloads one machine; counters reset when machines change.
- Choose when
- The balancer can hash.
StrongA shared counter, with a local limit in front
- How
- Coarse
limit_reqlocally, the exact count in a shared store in the gateway or app. - Limit
- One more hop; decide what happens if the store is down.
- Choose when
- The number is a promise to customers.