Sub topic 13
Keeping the proxy tier available and scalable
This chapter assumes you know how a reverse proxy load-balances requests and how health checks remove failed backends. It also assumes you are comfortable with IP addresses, ports and DNS. A reverse proxy protects the backends from failure. The proxy itself can still fail, so it needs the same care.
The proxy as a single point of failure
Every request passes through the proxy. If you run one proxy instance and it crashes, the whole site is unreachable, even though every backend is healthy. A single proxy also has fixed capacity for CPU, memory, network bandwidth and open connections. Availability and scaling both lead to the same design: run several proxy instances and spread traffic across them.
Running several proxy instances
Two common patterns put more than one proxy in front of your backends.
- Active-passive. One proxy serves traffic. A standby takes over its address if it fails. This is simple, but the standby does no work until a failure happens.
- Active-active. Several proxies serve traffic at the same time. This adds capacity and tolerates failure, but you need something in front that spreads traffic across them.
Moving an address between proxies
In active-passive setups, the proxies share a virtual IP address. A tool such as keepalived uses VRRP (Virtual Router Redundancy Protocol). The active proxy announces that it owns the address. If the announcements stop, the standby claims the address. Clients keep using the same IP and notice only a short interruption.
vrrp_instance proxy_vip {
state MASTER
interface eth0
virtual_router_id 51
priority 100
virtual_ipaddress {
10.0.0.100
}
}On the standby, set state BACKUP and a lower priority, for example 90.
Spreading traffic across active proxies
For active-active, there are three usual options.
| Option | How it works | Main limit |
|---|---|---|
| DNS with several A records | The domain name returns the address of each proxy | Clients and resolvers cache answers, so a dead proxy keeps receiving traffic until the cache expires |
| A layer 4 load balancer | A TCP-level balancer sits in front of the proxies | It is one more component that must itself be redundant |
| Anycast or ECMP routing | The network routes each client to a nearby or equal-cost proxy | Needs control of the network or a provider that offers it |
Cloud providers offer managed load balancers that are already redundant across zones. These often act as the first tier, with your own proxies behind them.
Keeping proxies interchangeable
Scaling out works best when any proxy can handle any request. Keep each proxy stateless where you can.
- Configuration. Deploy the same configuration to every instance from version control or a configuration tool. Drift between instances causes failures that appear only for some users.
- Cache. Each proxy has its own local cache. Two proxies may each fetch the same object from the backend, which lowers the hit rate and raises backend load. A shared cache tier or consistent hashing on the URL reduces this.
- Sessions. If the proxy pins a user to one backend, for example by client IP, each proxy must make the same choice. Otherwise the user moves between backends. Prefer storing session data in a shared store so the choice does not matter.
- TLS certificates. Every instance needs the certificate and private key. Distribute and renew them automatically.
Scaling the proxy tier
Scale out by adding instances. Scale up by adding CPU and memory to existing ones. Measure before you choose. TLS handshakes use CPU, so TLS-heavy traffic often needs more cores. Large responses and caching use network bandwidth and disk. Many idle keep-alive connections use memory and file descriptors.
Watch these signals:
- CPU use, especially during TLS handshakes.
- Open connections compared with the configured worker connection limit.
- Request latency added by the proxy, measured separately from backend latency.
- The rate of 502 and 504 errors, which point at backends or at proxy timeouts.
Failure and deployment practices
Plan for the proxy tier to fail, and for changes to it to go wrong.
- Spread instances across availability zones so one zone outage does not remove every proxy.
- Check proxy health from the tier in front of it, the same way the proxy checks its backends.
- Test a configuration before loading it. With nginx, run
nginx -t, thennginx -s reload, which starts new workers and lets old workers finish their open requests. - Roll out changes to one instance first. A bad configuration then affects a fraction of traffic.
- Keep spare capacity so that the remaining instances can carry the load when one fails. With two proxies, each must be able to handle all the traffic.
Exercise
You run two active-active proxies behind DNS with two A records. One proxy crashes, and some users keep seeing errors for several minutes. Why does this happen, and what change would shorten the outage?
Show answer
Resolvers and clients cache the DNS answer, so some of them keep sending requests to the dead proxy until the record's TTL expires. Fix it by placing a health-checked layer 4 load balancer in front of the proxies, or by using a floating virtual IP that moves to a healthy proxy. Lowering the TTL helps a little, but cached answers can still outlast it.