Global and local load balancing
medium 25 minTraffic 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
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
November 1993
RFC 1546 describes anycast: one address, several hosts, and the network picks one.
April 1995
RFC 1794 describes load sharing with DNS answers.
December 2006
RFC 4786 documents good practice for operating anycast services.
March 2012
Amazon Route 53 adds latency-based routing between regions.
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.
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.
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
Global and local side by side
| Global | Local | |
|---|---|---|
| Chooses between | Regions or data centres | Servers in one data centre |
| Tools | GeoDNS, anycast with BGP, global proxies | HAProxy, nginx, Envoy, F5, cloud balancers |
| Looks at | Place, latency, region health, capacity | Server health, open requests, content |
| Decides | Per lookup or per route | Per connection or per request |
| Failover | Seconds to minutes | Passive checks: at once. Probes: seconds |
| Main goal | Low delay, surviving a region | Even load, surviving a server |
What distance costs
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?
| GeoDNS | Anycast | |
|---|---|---|
| Decides by | Location or latency data | Internet routing |
| Address | Different per region | The same everywhere |
| Failover limited by | TTL and late clients | Route convergence |
| Fine control, such as 70/20/10 | Easy | Hard |
| Open connections | Stay where they are | May break when routes move |
Policies
How a region is chosen
| Policy | Rule | Example |
|---|---|---|
| Geolocation | Where the user is | EU users to the EU, also for legal reasons |
| Latency | Lowest measured delay | Usually near, not always nearest |
| Weighted | Fixed shares | 70% US, 20% EU, 10% Asia |
| Failover | Primary unless unhealthy | US primary, EU backup |
| Multivalue | Several healthy addresses | The client tries them |
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
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
Architectures
Several regions, three shapes
- 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
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
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
Follow the sun, in UTC
Standby
Hot, warm and cold
How ready is the second region?
| Level | What runs there | RTO (typical order) | Cost |
|---|---|---|---|
| Hot | Everything, with current data | Seconds to minutes | High |
| Warm | A smaller copy that must grow | Minutes | Medium |
| Cold | Backups and scripts, nothing running | Hours | Low |
| DNS switch | Any of these, moved by a record change | Adds TTL and a tail | Low |
Failover time with DNS
Room for the failed region
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?
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.