SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
  • What are Load Balancers?
  • Load Balancing Algorithms
  • DNS Load Balancing
  • Anycast Routing
  • Layer 4 vs Layer 7 Load Balancers
  • Health Checks
  • Sticky Sessions
  • Global Load Balancing
  • Nginx and HAProxy
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Foundations/Load Balancing

Global Load Balancing

Send users to the best region of the world, and move away from one that fails.

โฑ 13 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Global load balancing (also called GSLB, global server load balancing) sends each user to the best region of the world, not just the best server inside one data centre.

  • It picks a region by distance, speed, health and spare capacity.
  • It is done with DNS answers, anycast routing, or a global proxy at the edge.
  • If a whole region fails, users are moved to another region. This is called regional failover.
  • Inside each region, a normal load balancer still shares the work between servers.

02Concept: What is it?

A normal load balancer lives in one data centre. It shares requests between the servers there. But what if your users are in New York, London and Sydney? Sending everyone to one place makes far-away users slow, and one big outage (a power cut, a bad deploy, a cloud region problem) takes everyone down.

So big systems run in several regions. A global load balancer sits in front of them all and answers one question for every user: "Which region should this user go to?"

Two levels of load balancing
๐Ÿง‘Usersall over the world
โ†’โ†“1. which region?
๐ŸŒGlobal load balancerDNS, anycast or edge
โ†’โ†“2. nearest healthy
๐ŸŒŽUS Eastregional LB + servers
๐ŸŒEU Westregional LB + servers
๐ŸŒAsiaregional LB + servers
The global layer picks a region. The regional layer picks a server.

What it looks at

๐Ÿ“Location
Send the user to the region on the same continent. Uses the user's IP address to guess where they are.
โฑ๏ธLatency
Measure the real round-trip time to each region and pick the fastest. The nearest on the map is not always the fastest.
๐Ÿ’šHealth
Never send users to a region that is failing its health checks.
๐Ÿ“ฆCapacity
Do not overload a region. Shift some traffic away when it is nearly full.
โš–๏ธWeights
Send a chosen percentage to each region: for a slow roll-out, or to use cheaper regions more.
๐Ÿ“œRules
Laws such as data residency may force users of one country to stay in one region.

03Real-Life Example

๐ŸตThe Pizza Chain with Branches in Many Cities
A pizza chain has one phone number for the whole country. When you call, the call centre sees where you are and connects you to the nearest branch. If that branch has a power cut, the call centre sends you to the next nearest. If one branch is very busy, some callers go to a quieter one. Each branch then handles its own cooks and delivery riders. The call centre only chooses the branch.
Pizza chain
Global load balancing
One national phone number
One domain name (shop.example.com)
The call centre
Global load balancer (DNS, anycast or edge)
A branch in each city
A region with its own servers
Choosing the nearest branch
Geo or latency routing
Branch has a power cut
Region failure, health check fails
Sending the call to the next nearest
Regional failover
A branch that is too busy
Region at capacity, traffic shifted
The branch's own cooks and riders
Regional load balancer and servers
Failover in action
๐Ÿง‘User in Dubai
โ†’โ†“asks
๐ŸŒGlobal LBpicks EU West
โ†’โ†“EU West fails
๐ŸŒGlobal LBpicks Asia
โ†’โ†“served
๐ŸŒAsia

04Why Do We Need It?

โšกLower latency
A user in Sydney talks to a nearby region, not one on the other side of the world. Distance adds delay that no code can remove.
๐Ÿ›ก๏ธSurvive a region outage
If an entire region goes dark, traffic moves to the others and the service stays up.
๐Ÿ“ˆSpread the load
Traffic is shared between regions, so no single region has to carry everything.
๐ŸงชSafer releases
Roll out a new version to one region first, with a small weight, and widen it only if it is healthy.
๐Ÿ“œFollow the law
Keep users of some countries in regions that obey local data rules.
๐Ÿ’ธControl cost
Weights let you use cheaper regions more, or shift work to where capacity is available.
One region is a single point of failure for the whole world. Global load balancing lets many regions share users and cover for each other.

05Where Do We Use It?

๐ŸŒGlobal websites and apps
Any service with users on several continents: shops, streaming, social networks, banks.
๐ŸŽž๏ธCDNs
A CDN is a global balancer for static content: it sends users to the nearest edge location.
โ˜๏ธCloud traffic managers
Clouds offer services that route by latency, geography, weight or priority, with health checks.
๐ŸฆDisaster recovery
A primary region with a standby region that takes over if the primary fails.
๐ŸŽฎOnline games
Players are sent to the game servers with the lowest ping.
๐Ÿ”ŒAPIs for partners
A single API address that always points at the healthy and nearest region.

06Types / Parts / How It Works

Three ways to do it

DNS, anycast and global proxy
DNS based
๐Ÿ“–DNS answers differentlyby place or latency
โณCached for the TTLslow to fail over
Anycast
๐Ÿ“กSame IP in many regionsthe network picks the nearest
โšกNo DNS cache to wait forfast failover
Global proxy / edge
๐ŸงฑEdge proxy near the userterminates the connection
๐Ÿ›ฐ๏ธForwards over a fast backbonesmart routing per request
All three answer the same question, but they act at different places.
MethodHow it worksGood and bad
DNS based (GeoDNS, latency routing)The DNS server returns the IP of the best region for the user's resolverSimple and cheap. Answers are cached, so failover waits for the TTL. Sees the resolver's location, not always the user's.
AnycastEvery region announces the same IP. The internet routes each user to the nearest oneFast failover, no DNS cache. Less control: routing follows network paths, not your rules. See Anycast Routing.
Global proxy / edgeA network of edge proxies accepts the connection near the user and forwards it to a chosen regionFine control per request, fast failover, can add security and caching. A provider or a large setup is needed.
Client-sideThe app itself holds a list of regions and picks one, with retriesFull control, works for mobile apps and SDKs. Every client must be updated to change the rules.

Routing policies

PolicyIdeaUse it for
GeographicA table maps country or area to a regionData rules, simple setups. Can be wrong when the map is not the network.
Latency basedPick the region with the lowest measured delayFastest experience for users.
WeightedSend fixed percentages to each regionGradual roll-outs, cost balancing.
Failover (primary and backup)Use the primary until it is unhealthy, then the backupDisaster recovery.
Capacity awareShift traffic away from regions near their limitPeak traffic, uneven demand.
VisualizerWhere does this user go, and what happens when a region dies?Pick a city and a policy, then make a region fail. Compare DNS answers (cached for the TTL) with anycast or a global proxy. Raise the normal load and watch the survivors run out of room.
Where is the user?
Routing policy
How is it done?
Which region fails?
Share of requests from New York that each region receives (latency in ms is illustrative)
๐ŸŒŽ US East 20 ms
Before failover
100%
After failover
100%
๐ŸŒ EU West 80 ms
Before failover
0%
After failover
0%
๐ŸŒ Asia 220 ms
Before failover
0%
After failover
0%
Requests failing
โ€“
until failover completes
Up to
โ€“
30 s detect + 60 s TTL
Latency after
20 ms
average for this user
Survivors' load
60%
if traffic was even
All regions are healthy. Latency routing: measure the round trip to each region and pick the lowest. Follows the real network, not the map.

A teaching model with made-up latencies and timings. The survivors' load assumes three regions with equal traffic, so one failure raises each survivor by half (60% becomes 90%). Real systems differ.

07How to Use It / Steps / Implementation

Designing global load balancing

  1. 1
    Choose your regions
    Put regions near your users and in separate failure zones (not all in one cloud region or one provider network).
  2. 2
    Pick a routing policy
    Latency based for speed, geographic where law requires it, weighted for roll-outs, failover for a standby region.
  3. 3
    Add health checks per region
    Check the real service (not only the machine), from several places. Remove a region only after a few failed checks, to avoid flapping.
  4. 4
    Choose the mechanism
    DNS is the easiest start. Add anycast or an edge proxy when you need faster failover and finer control.
  5. 5
    Set the TTL on purpose
    A short TTL reacts faster but costs more DNS queries and still cannot beat resolvers that ignore it.
  6. 6
    Keep spare capacity
    If two regions share the load, each must be able to carry the other's traffic during a failure. Plan the headroom.
  7. 7
    Plan the data
    Decide how data is replicated between regions, and what happens to writes during a failover (see replication and consistency).
  8. 8
    Test it
    Take a whole region out of rotation in a test, and check how long users are affected and whether the others cope.
โš ๏ธFailover needs headroom
If three regions each run at 80% and one fails, the other two must absorb its work. They would need 120% capacity. Failover without spare room turns one outage into three.
๐Ÿ’กData is the hard part
Sending users to another region is easy. Having their data there is not. Decide early whether each region has a full copy, whether writes go to one primary region, and how stale a copy may be.

08Key Terms to Know

TermMeaning in easy words
GSLBGlobal server load balancing: choosing among data centres or regions
RegionA separate geographic location with its own data centres
GeoDNSDNS that answers differently based on where the asker seems to be
Latency-based routingChoosing the region with the lowest measured round-trip time
TTLHow long a DNS answer may be cached before asking again
AnycastThe same IP announced from many places, with the network choosing the nearest
Edge location / PoPA small site close to users that terminates connections or caches content
Regional failoverMoving traffic from a failed region to a healthy one
Active-activeSeveral regions serve users at the same time
Active-passiveOne region serves users and another waits as a standby
HeadroomSpare capacity kept so other regions can absorb a failed region's traffic

09Pros, Cons and Trade-offs

โœ… Pros
  • Lower latency for users around the world
  • Survives the loss of a whole region
  • Spreads load and cost across regions
  • Enables safe, gradual roll-outs
  • Helps follow data location rules
โš ๏ธ Cons
  • Much more complex to build and operate
  • Costs more: several full regions and spare capacity
  • DNS caching makes failover slow
  • Data replication and consistency across regions is hard
  • Routing by location can be wrong (VPNs, public resolvers)
  • A bad global rule can send everyone to the wrong place at once

Trade-offs

ChoiceWe gainWe lose
Active-active regionsBest latency, no idle hardware, quick failoverHard data consistency, higher complexity
Active-passive regionsSimpler data storyPaid-for idle capacity, slower recovery, far users stay slow
Short DNS TTLFaster failoverMore DNS traffic, extra lookup delay, some resolvers ignore it
Long DNS TTLFewer lookups, cheaperUsers stay on a dead region longer
Anycast / edge proxyFast failover and finer controlCost, vendor dependence, less transparent routing
More spare capacitySafe failoversHigher cost every day

10Common Problems and Mistakes

Common mistakes

  1. No headroom. The surviving regions are overloaded by failover traffic and fail too (a cascading failure).
  2. Trusting DNS for fast failover. Caches in resolvers, operating systems and browsers keep old answers past the TTL.
  3. Health checks that only ping a port. A region can be "up" but unable to serve real requests.
  4. Flapping. A region that goes in and out of rotation every few seconds causes more harm than good. Require several failures and successes.
  5. Ignoring the data. Users arrive in a new region, and their data is missing or old.
  6. Judging location by the DNS resolver. A user on a distant public resolver may be placed in the wrong region.
  7. Never testing failover. The first real test is during a real outage.

Things to watch

๐ŸŒช๏ธCascading failure
Failover traffic overloads the next region, which fails, and so on.
๐Ÿ•ณ๏ธSplit brain
Two regions both believe they are the primary and accept writes.
๐Ÿ•ฐ๏ธReplication lag
After failover, recent writes may be missing in the new region.
๐ŸงญWrong routing
VPN and public-resolver users can be sent far from where they are.

11Real-World Examples

โ˜๏ธCloud traffic services
Major clouds offer DNS traffic routing (latency, geo, weighted, failover) and global load balancers with anycast front ends.
๐ŸŽž๏ธCDNs
Content delivery networks are global proxy layers that send each user to a nearby edge location.
๐Ÿ“บStreaming services
Video services direct players to nearby servers and move them when a site has problems.
๐ŸฆBanks and payments
Often active-passive between regions for simpler, safer data, with tested failover plans.
๐ŸŽฎOnline games
Matchmaking chooses a region by player latency.
๐Ÿ›’Global shops
Run in several regions, shifting traffic to quieter regions at peak times.
๐Ÿ“Note
Product features and defaults change over time. Check your provider's current documentation before you rely on a setting.
Mini case: losing a region
๐ŸŒŽUS EastDOWN
โ†’โ†“health check fails
๐ŸŒGlobal LBremoves US East
โ†’โ†“shifts users
๐ŸŒEU Westtakes some
๐ŸŒAsiatakes some
Failed Decision Healthy
Traffic from the dead region moves to the others. They need spare room to take it.

12Related Topics to Read Next

DNS Load Balancingโ†’

The simplest way to route users to different regions.

Anycast Routingโ†’

The same IP in many places, with the network choosing the nearest.

CDNโ†’

A global layer that serves content from places near users.

Health Checksโ†’

How a region is found to be sick and removed.

Failoverโ†’

Switching to a backup when the main system fails.

Replicationโ†’

How data gets to each region.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”What: pick the best region for each user. A regional balancer then picks the server.
  • โœ”Looks at: location, latency, health, capacity, rules.
  • โœ”Ways: DNS (cheap, cached), anycast (fast failover), global proxy (fine control).
  • โœ”Policies: geo, latency, weighted, failover.
  • โœ”DNS trap: failover waits for the TTL and for caches that ignore it.
  • โœ”Headroom: survivors must carry the failed region's load.
  • โœ”Data: replication and consistency across regions is the hard part.
  • โœ”One line: โ€œSend each user to the nearest healthy region, and make sure the others can catch them.โ€
โ† Previous
Sticky Sessions
Next โ†’
Nginx and HAProxy