Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Servers in the middle: proxies and load balancers

After this lesson: I can say what a proxy, a reverse proxy and a load balancer do.

50 min lessonNo commands

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

WordWhat it means
Proxya server that sends requests on behalf of clients
Reverse proxya server that takes requests on behalf of servers
Load balancera reverse proxy that shares requests between servers
Backenda server behind the proxy that does the real work
Health checka regular test that a backend still answers
Round robingiving 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 numberRemainder after dividing by 3Backend
00web-1
11web-2
22web-3
30web-1
41web-2

The cells students fill in are left blank in this preview.

Try it: hand out requests

Code: python
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.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.