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.

Video lesson · Chapter 1 of 5

Tokyo chooses a worker after the region is chosen

0:00 / 4:15

Two steps to a server

1:20Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Global load balancing decides which region or data centre serves a user. It looks at where the user is, which regions are healthy, how fast they respond and how much room they have. It is often called GSLB, global server load balancing.

Local load balancing then decides which server inside that region does the work. It looks at the health and load of servers a few metres apart.

The two steps answer different questions with different tools. The first happens rarely and with rough information. The second happens for every connection or request, with fresh information.

Why it exists

Distance costs time that no faster server can win back. Light in fibre needs about 5 ms for every 1,000 km, one way. A user on another continent waits for every round trip.

A whole region can fail: a power cut, a broken network, a bad configuration. If all servers are in one place, the best local balancer cannot help.

And one step alone is not enough. A global step that had to know every server would always work with old information, and a local balancer cannot shorten the distance.

A short history

  1. November 1993

    RFC 1546 describes anycast: one address, several hosts, and the network picks one.

  2. April 1995

    RFC 1794 describes load sharing with DNS answers.

  3. December 2006

    RFC 4786 documents good practice for operating anycast services.

  4. March 2012

    Amazon Route 53 adds latency-based routing between regions.

  5. May 2016

    RFC 7871 describes EDNS Client Subnet, which lets a resolver pass on part of the client's address.

Words to know

GSLB
Global server load balancing: choosing a site.
GeoDNS
DNS answers that depend on where the question comes from.
Anycast
The same address announced from several places. Routing picks one.
BGP
Border Gateway Protocol: how networks tell each other which addresses they can reach.
RTO
Recovery time objective: how long an outage may last.
RPO
Recovery point objective: how much recent data may be lost.
ECS
EDNS Client Subnet: part of the client's address, sent along with the DNS question.

How it works

Step through the idea, or press Play.

Step 1 of 3

Choose a region. By place, latency, weight and health

Understand it

Start here

Which mountain, then which lift

Imagine a ski trip. First you decide which mountain: the near one takes 10 minutes, the far one 90. Once you are there you decide which lift, by looking at the queues. Two decisions, with different information.

A day on skis
NORTHEASTTwo decisionsWhere do we ski?

The board at the bottom knows mountains. It does not count people at lifts, and it does not need to.

The picture

Two tiers

First a region, then a server

Users reach a global balancer that asks which region. It sends them to the balancer of Europe or of America. Each regional balancer chooses between two servers.
If the global step is DNS, it only hands out an address, and the traffic then flows straight to the region.

Global and local side by side

GlobalLocal
Chooses betweenRegions or data centresServers in one data centre
ToolsGeoDNS, anycast with BGP, global proxiesHAProxy, nginx, Envoy, F5, cloud balancers
Looks atPlace, latency, region health, capacityServer health, open requests, content
DecidesPer lookup or per routePer connection or per request
FailoverSeconds to minutesPassive checks: at once. Probes: seconds
Main goalLow delay, surviving a regionEven load, surviving a server

What distance costs

RTT≥2d200,000 km/sd=9,600 km: 19,200200,000=96 msRTT \ge \frac{2d}{200{,}000\ \text{km/s}} \qquad d = 9{,}600\ \text{km}:\ \frac{19{,}200}{200{,}000} = 96\ \text{ms}
Light in fibre covers about 200,000 km per second. Real paths are longer. A new TCP and TLS 1.3 connection needs two round trips before the request, so the first reply takes at least three: 288 ms here.

Global 1

GeoDNS

The DNS service looks at where a question comes from, using a database of address locations, and returns the address of a suitable region. A user in Paris asks for api.example.com and gets the European address. The basics are in Introduction to DNS.

Pros

  • Simple: no change to the network
  • Works with any backend and protocol
  • Policies by place, latency, weight or health

Cons

  • Caches and TTLs delay failover
  • Some clients cache too long
  • The location database can be wrong, and it sees the resolver, not the user

Global 2

Anycast

Several sites announce the same address with BGP. Each network on the Internet follows its own best route, which usually ends at a nearby site. If a site withdraws its announcement, routing converges and packets go elsewhere, without any DNS change.

Pros

  • No DNS cache in the way of failover
  • Users enter the network close by
  • An attack is spread over many sites

Cons

  • Needs your own address space and BGP skills, or a provider
  • "Best route" is a network opinion and not a latency measurement
  • A route change can break open TCP connections

GeoDNS or anycast?

GeoDNSAnycast
Decides byLocation or latency dataInternet routing
AddressDifferent per regionThe same everywhere
Failover limited byTTL and late clientsRoute convergence
Fine control, such as 70/20/10EasyHard
Open connectionsStay where they areMay break when routes move

Policies

How a region is chosen

PolicyRuleExample
GeolocationWhere the user isEU users to the EU, also for legal reasons
LatencyLowest measured delayUsually near, not always nearest
WeightedFixed shares70% US, 20% EU, 10% Asia
FailoverPrimary unless unhealthyUS primary, EU backup
MultivalueSeveral healthy addressesThe client tries them
These are Route 53's names. Multivalue returns up to eight healthy records. Azure Traffic Manager offers priority, weighted, performance, geographic, multivalue and subnet.

Health-based choice is not a policy of its own: it filters. First remove unhealthy regions, then apply the policy to those that are left. Dedicated global balancers (GSLB appliances and services) combine DNS answers with health and load in this way.

Local

Inside one data centre

Layer by layer

Traffic enters one data centre through edge routers, then a layer 4 balancer that looks at addresses and ports, then a layer 7 balancer that looks at paths and spreads requests over a web pool of 4 machines, an API pool of 6 and a worker pool of 3.
Layer 4 spreads connections over the layer 7 balancers. Layer 7 ends TLS and reads requests.
  • Layer 4

    TCP and UDP, by connection, very fast. HAProxy, nginx stream, F5 LTM, cloud network balancers.

  • Layer 7

    HTTP, by content, with TLS. nginx, Envoy, Traefik, Kong.

  • Health

    TCP connect, HTTP /health, content. Active probes and passive watching of real traffic.

  • Persistence

    By source address, cookie, URL or header. None at all gives the best spread.

Algorithms and health checks are explained in load balancing, the layers in layer 4 and layer 7 load balancers and persistence in session persistence.

One request from Tokyo

The global step happened once. The local step happens for every request.

Architectures

Several regions, three shapes

  1. 1

    Active-active

    All regions serve traffic, each for its nearest users, and data is replicated between them. Best for low delay and high availability with users everywhere. The price is conflicting writes and more complexity.

  2. 2

    Active-passive

    One region serves everything and replicates to a standby that takes over after a failure. Best for cheaper disaster recovery and simple data. The price is an idle region and a switch that must be rehearsed.

  3. 3

    Follow the sun

    Weights move during the day, towards regions in their business hours: Asia-Pacific, then Europe, then the Americas. Best when staff or capacity follow the working day.

Active-passive

Users reach a global balancer that checks both regions. It sends all traffic to the primary region, whose database replicates to the replica in the standby region. The standby path is dashed and used as a backup.
Replication to the standby is usually asynchronous, so the newest writes can be lost. That is the RPO.

Follow the sun, in UTC

Only the weights change. Every region keeps some traffic, so none is cold.

Standby

Hot, warm and cold

How ready is the second region?

LevelWhat runs thereRTO (typical order)Cost
HotEverything, with current dataSeconds to minutesHigh
WarmA smaller copy that must growMinutesMedium
ColdBackups and scripts, nothing runningHoursLow
DNS switchAny of these, moved by a record changeAdds TTL and a tailLow
"Hot" does not mean instant: someone must still notice, decide and move the traffic.

Failover time with DNS

t≈I⋅F+TTL+ttail10×3+60=90 s, plus late clientst \approx I \cdot F + TTL + t_{tail} \qquad 10 \times 3 + 60 = 90\ \text{s, plus late clients}
I is the check interval and F the failures needed. Route 53 checks every 30 s, or every 10 s with fast checks, and its failure threshold is 3 by default.

Room for the failed region

uafter=n un−1n=3, u=60%: 1802=90%u≤n−1n umaxu_{after} = \frac{n\,u}{n-1} \qquad n=3,\ u=60\%:\ \tfrac{180}{2}=90\% \qquad u \le \tfrac{n-1}{n}\,u_{max}
Assumes equal regions and an even spread of the failed one's load. To stay at 80% with three regions, run each at no more than 53%.

Data

Traffic moves quickly, data does not

A region can answer reads from its own copy. Writes are the question. Either one region owns all writes, or each user belongs to a home region, or several regions accept writes and conflicts are resolved. Sending a user elsewhere before that user's data has arrived shows an older version.

Who accepts the write?

See high availability for what failover does to your promises.

Operate

Make failover boring

Do

  • Check a region from outside, along a real user path
  • Require several observers before failing over
  • Rehearse: move real traffic away and back
  • Move back slowly, after the data has caught up
  • Keep the switch itself outside the failing region

Avoid

  • Failing over on one missed probe
  • A standby that has never served traffic
  • Running hot with no headroom
  • Health checks that stay green when the database is gone
  • Two regions that both believe they are primary

Summary

The whole topic in one minute.

Think in two steps. Global: which region, by place, latency, weight or failover, with GeoDNS, anycast or a global proxy. Local: which server, with layer 4 and layer 7 balancers.

Failover time is detection plus the time until clients use the new answer. With DNS that is a TTL and a tail. With anycast it is the time routing needs to settle.

The hard part is data and spare capacity: a region can only take over if it has the data and the room.

  • Global chooses a region, local chooses a server.
  • GeoDNS is simple and limited by caches. Anycast avoids DNS but follows Internet routing.
  • Filter by health first, then apply geography, latency, weights or failover.
  • Failover time is detection plus TTL plus a tail.
  • Leave room: with n equal regions, run at most (n-1)/n of your limit.
  • Data decides what a region can really take over.

Sources