Sub topic 16
Proxies, load balancers, and CDNs
This chapter assumes you can read HTTP requests and responses, know what Cache-Control and ETag do, and understand how HTTPS protects a connection. Here you will see that the server your browser talks to is often not the server that built the page.
Intermediaries between client and server
An intermediary is a machine that sits on the path between a client and an origin server. The origin server is the machine that holds the real application and data. Intermediaries receive HTTP requests, then answer them or forward them onward. The client usually cannot tell they are there.
There are three common kinds:
- A forward proxy acts for clients. A company network may force all browsing through one to filter sites or log traffic.
- A reverse proxy acts for servers. It accepts requests from the internet and passes them to the real application servers behind it.
- A load balancer is a reverse proxy whose main job is to spread requests across several servers.
A CDN (content delivery network) is a large group of reverse proxies placed in many cities. Each one caches responses and serves them to nearby users.
What a reverse proxy does
A reverse proxy often ends the TLS connection, a step called TLS termination. The browser's encrypted connection stops at the proxy. The proxy then sends plain HTTP, or a new TLS connection, to the origin. This keeps certificate handling in one place.
The origin now sees the proxy's IP address, not the user's. To pass the original address along, proxies add headers such as X-Forwarded-For and X-Forwarded-Proto:
GET /account HTTP/1.1
Host: example.com
X-Forwarded-For: 203.0.113.7
X-Forwarded-Proto: https
Load balancing
A load balancer picks a server for each request. Two simple strategies are round robin, which takes servers in turn, and least connections, which picks the server with the fewest active requests. The balancer also runs health checks. It requests a URL such as /health from each server and stops sending traffic to any server that fails.
If your app keeps session data in one server's memory, a balancer that rotates servers will lose the session. Fix this by storing sessions in a shared store, or by using sticky sessions, where the balancer keeps sending one client to the same server.
How a CDN uses caching
A CDN edge follows the same cache rules you already know. When a request arrives, the edge checks for a stored copy. If it has one that is still fresh, it replies at once. This is a cache hit. If not, it fetches from the origin, stores the response, and replies. This is a cache miss.
The Cache-Control header controls this. max-age applies to every cache. s-maxage applies only to shared caches such as CDNs:
HTTP/1.1 200 OK
Cache-Control: public, max-age=60, s-maxage=3600
Age: 120
The Age header says how many seconds the response has been in a cache. Use private for responses meant for one user, such as an account page, so a shared cache never stores them.
Exercise
A site runs three servers behind a load balancer. Users report being logged out at random. Each server stores login sessions in its own memory. What is the cause, and name one fix.
Show answer
The balancer sends consecutive requests from one user to different servers, and only one of them holds that user's session. Fixes: move sessions to a shared store such as a database or Redis, or enable sticky sessions so each user always reaches the same server.
Exercise
Which header value tells a CDN to cache a response for one hour but tells browsers to cache it for only one minute?
Show answer
Cache-Control: public, max-age=60, s-maxage=3600. The s-maxage value applies to shared caches, and max-age applies to the browser.