🎓 A1Learner
🔍
0 of 47 lessons completed

Load Balancer Algorithms

Load balancer algorithms are the rules a balancer follows to pick the next server. Different rules suit different shapes of traffic.

Round robin over ten requests

Ten requests pass to three servers in turn. The final counts show how the turns landed.

servers = ["server A", "server B", "server C"]
counts = {"server A": 0, "server B": 0, "server C": 0}

for i in range(10):
    counts[servers[i % len(servers)]] += 1

for server in servers:
    print(server, "handled", counts[server], "requests")

Least connections with uneven lengths

The first request lasts many steps, and the rest are short. While one server stays busy, the rule steers new requests toward the freer servers.

active = [[], [], []]
totals = [0, 0, 0]
names = ["server A", "server B", "server C"]
lengths = [9, 2, 2, 2, 2, 2, 2, 2, 2, 2]

for length in lengths:
    loads = [len(a) for a in active]
    choice = loads.index(min(loads))
    active[choice].append(length)
    totals[choice] += 1
    print("request lasting", length, "steps goes to", names[choice])
    for server_active in active:
        server_active[:] = [d - 1 for d in server_active if d - 1 > 0]

print("totals:", totals)

Example

lengths = [9, 2, 2, 2, 2, 2, 2, 2, 2, 2]

def round_robin(lengths):
    totals = [0, 0, 0]
    for i in range(len(lengths)):
        totals[i % 3] += 1
    return totals

def least_connections(lengths):
    active = [[], [], []]
    totals = [0, 0, 0]
    for length in lengths:
        loads = [len(a) for a in active]
        choice = loads.index(min(loads))
        active[choice].append(length)
        totals[choice] += 1
        for server_active in active:
            server_active[:] = [d - 1 for d in server_active if d - 1 > 0]
    return totals

print("round robin totals:", round_robin(lengths))
print("least connections totals:", least_connections(lengths))

Key points

  • Round robin: passing requests to servers in turn, then starting again at the first.
  • Least connections: sending the next request to the server with the fewest active requests.
  • Active request: a request a server has received and not yet finished.

In this example, both rules face the same uneven list of request lengths. Round robin keeps its turn order, while least connections leaves the busy server out until it frees up, so the totals differ.

Note

round robin ignores how busy each server is, and least connections checks the load first.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

One server is stuck with a very long request. Which rule stops sending it more requests for a while?