AlgoViz

Global and local load balancing

medium 25 min

Traffic is steered twice: a global step picks the region with DNS or anycast, and a local step picks a server inside it.

See it run

Follow one request through both steps, compare policies, and watch a region fail with DNS and with anycast.

Operations

One request, two steps

A user in Tokyo. GeoDNS picks the region, the local balancer picks a server.

Step 1 of 6

A request starts

Our systemUser in TokyoGeoDNSTokyo balancerServer 1Server 2Server 3

Which region?

The user opens api.example.com.

Step 1: A request starts

def handle(user, request):    region = dns(request.host, near=user)   # global, cached    return region.balancer.forward(request)def forward(request):                        # local, every time    pool = [s for s in servers if s.healthy]    return min(pool, key=open_requests).handle(request)
Failover
Server: at once
Decided by
Place, then load
Example regions
Tokyo, Singapore; EU West, EU North; US East, US West

The global step is cached. The local step happens every time.

In the real world

The same global and local load balancing doing real jobs. Pick one and step through it.

Weights move with the working day, in UTC.

Step 1 of 3

02:00

Global balancerAsia-Pacificweight 70Europeweight 10Americasweight 20
Asia-Pacific
70%
Europe
10%
Americas
20%

Mostly Asia-Pacific

Asia-Pacific is at work and carries most of the traffic.

Step 1: 02:00

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • No room in the other region

    critical

    The failover succeeds and the second region falls over.

    Wrong
    two regions at 70%
    Right
    two regions at 40%shed low-priority work first
  • Geolocation without a default

    major

    Users whose place is unknown get no answer.

    Wrong
    EU -> eu, NA -> us
    Right
    EU -> eu, NA -> useveryone else -> us
  • Promising instant failover with DNS

    major

    Detection and caches take time, and some clients never ask again.

    Wrong
    RTO: 5 s, TTL: 300 s
    Right
    10 * 3 + 60 = 90 splus a tail
  • Moving traffic before data

    critical

    The new region shows old data, or two regions accept the same write.

    Wrong
    route(user, B)
    Right
    stop_writes(A)wait_for_replication(B)route(user, B)

Interview tips

  • How to explain it

    • Global: which region.
    • Local: which server.
    • Different information, different speed.
  • Memory trick

    • First the mountain, then the lift.
    • Booking picks the airport, staff pick the counter.
    • Health filters, policy chooses.
  • Numbers to say

    • Interval × failures + TTL.
    • Use at most (n-1)/n of the limit.
    • About 5 ms per 1,000 km, one way.
  • Be careful with

    • Data that has not arrived.
    • Resolvers far from users.
    • Untested standby regions.