AlgoViz

NGINX

medium 28 min

NGINX (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 hearSayBecause
Two or more app serversA reverse proxy with an upstream groupOne address, failover, TLS in one place
Slow mobile clientsResponse bufferingApp workers are freed in milliseconds
A spike on public pagesMicro-caching with a lockThe backend sees one request per key per second
Abuse of one endpointlimit_req per address or keyRefuse early; say it is per machine
Only AWS, small teamA managed balancerNothing to patch or scale
Hundreds of services that moveEnvoy or a meshDiscovery 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_req with 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_req locally, 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.