Load Balancers
A load balancer is a program that receives each incoming request and passes it to one server behind it. No single server has to take every request.
Passing requests in turn
The balancer sends each request to the next server in its list. The printout shows which server received each request across the run.
servers = ["server A", "server B", "server C"]
requests = ["request 1", "request 2", "request 3", "request 4", "request 5", "request 6"]
for i, request in enumerate(requests):
server = servers[i % len(servers)]
print(request, "goes to", server)What each layer can see
A layer 4 balancer sees the address and the port. A layer 7 balancer also sees the path inside the request.
request = {"address": "203.0.113.10", "port": 80, "path": "/photos"}
print("layer 4 sees:", [request["address"], request["port"]])
print("layer 7 sees:", [request["address"], request["port"], request["path"]])The work spreads across the group
A longer run lets the counts build up. Each server ends with the same count, so no server carries the run alone.
servers = ["server A", "server B", "server C"]
counts = {"server A": 0, "server B": 0, "server C": 0}
for i in range(9):
server = servers[i % len(servers)]
counts[server] += 1
for server in servers:
print(server, "handled", counts[server], "requests")Example
servers = ["server A", "server B", "server C"]
counts = {"server A": 0, "server B": 0, "server C": 0}
paths = ["/photos", "/about", "/photos", "/basket", "/about", "/photos"]
for i, path in enumerate(paths):
server = servers[i % len(servers)]
counts[server] += 1
print("request for", path, "goes to", server)
for server in servers:
print(server, "handled", counts[server], "requests")Key points
- Load balancer: the program that receives requests and passes each one to a server.
- Port: the numbered entry point on a machine for a kind of traffic.
- Layer 4: a balancer that chooses using only the address and the port.
- Layer 7: a balancer that can also read the path inside the request.
In this example, the balancer passes each request to the next server in turn and then prints the counts. Every server ends with the same count, so the work stays shared.
Note
a layer 7 balancer knows more about each request, so it can make finer choices.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which balancer can choose based on the path inside a request?