One address, three servers
An online shop starts its winter sale at 9 o'clock. Ten times the usual number of customers arrive at once.
The shop has three web servers, but customers only ever type one address. Something stands in front of the servers and shares the work out. In this lesson you meet the boxes that sit between a client and a server.
By the end of this lesson
Note
I can say what a proxy, a reverse proxy and a load balancer do.
Key words
| Word | What it means |
|---|---|
| Proxy | a server that sends requests on behalf of clients |
| Reverse proxy | a server that takes requests on behalf of servers |
| Load balancer | a reverse proxy that shares requests between servers |
| Backend | a server behind the proxy that does the real work |
| Health check | a regular test that a backend still answers |
| Round robin | giving each backend a turn in order |
The cells students fill in are left blank in this preview.
A proxy acts for the clients
Many companies send all staff web traffic through a proxy. Your laptop asks the proxy, and the proxy asks the website for you.
The website sees the proxy's address, not your laptop's. The company can block some sites, keep a log, and scan downloads for viruses. If a site works at home but not in the office, the proxy is a good suspect.
A reverse proxy acts for the servers
A reverse proxy stands in front of servers instead. Customers connect to it, and it passes each request to a backend.
It often holds the TLS certificate, so the backends do not need one. It can keep copies of common pages, which saves the backends work. nginx is a reverse proxy you will see in many job adverts.
A common mistake
Warning
Many people think a proxy and a reverse proxy are the same thing. Both pass requests on, but for different sides. A proxy acts for the clients and hides them. A reverse proxy acts for the servers and hides them. Ask whose side it is on.
A load balancer shares the work
A load balancer is a reverse proxy with several backends. The simplest rule is round robin: each backend takes the next request in turn.
It also runs a health check, perhaps every ten seconds. A backend that stops answering gets no more requests until it recovers. So one server can fail and customers notice nothing.
Worked example: round robin over three
| Request number | Remainder after dividing by 3 | Backend |
|---|---|---|
| 0 | 0 | web-1 |
| 1 | 1 | web-2 |
| 2 | 2 | web-3 |
| 3 | 0 | web-1 |
| 4 | 1 | web-2 |
The cells students fill in are left blank in this preview.
Try it: hand out requests
servers = ["web-1", "web-2", "web-3"]
# Each request goes to the next server in turn.
# % gives the remainder, so the count wraps round to 0.
for request in range(7):
choice = request % len(servers)
print("Request", request, "goes to", servers[choice])
# Add "web-4" to the list, then press Run.