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

Anycast Routing

Many servers share one IP address, and the network sends you to the nearest.

⏱ 11 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

With anycast, many servers in many places share the same IP address. The internet’s routers send each user to the nearest one.

  • Users get a fast reply from a close server.
  • If one site fails, traffic moves to the next nearest site automatically.
  • It is a big reason why CDNs, public DNS and DDoS protection work so well.
  • Best for short, stateless requests (like DNS). Long connections need care.

02Concept: What is it?

Normally one IP address belongs to one machine. This is unicast. In anycast, the same IP is announced from many places at once. The network (using a protocol called BGP) sends each packet to the closest place, in terms of network path.

Unicast vs Anycast
Unicast: one IP, one site
πŸ§‘Asia
πŸ§‘Europe
πŸ§‘USA
↓all travel far
🏒One site in USA1.2.3.4
Anycast: one IP, many sites
πŸ§‘Asia
πŸ§‘Europe
πŸ§‘USA
↓
↓
↓
🏒Mumbai1.2.3.4
🏒London1.2.3.4
🏒New York1.2.3.4
Unicast: a far user must travel to the one site. Anycast: every user reaches a near site, with the same address.
🧠Easy way to remember
Unicast = one to one. Broadcast = one to all. Multicast = one to a group. Anycast = one to the nearest of a group.

03Real-Life Example

🍡The emergency number
In India you dial 112 for help. There is one number for the whole country, but you do not reach one office. Your call goes to the nearest control room. If that room is busy or down, your call goes to the next one. You never change the number.
Emergency number
Anycast
The number 112
One shared IP address
Control rooms in many cities
Edge sites / data centres
Phone company routes your call
Internet routers (BGP)
Nearest room answers
Nearest site answers
Room is down, next room answers
Site withdraws its route, traffic shifts
Failover without any user change
πŸ§‘User in Paris
→↓to 1.2.3.4
🏒Londondown ❌
🏒Frankfurttakes over βœ…

04Why Do We Need It?

⚑Lower latency
The user talks to a near site, not one across the world.
πŸ›‘οΈFast failover
If a site stops, the network re-routes in seconds. No DNS wait, no TTL.
🌊DDoS defence
Attack traffic is split across many sites instead of hitting one.
πŸ“ˆEasy scale-out
Add a new site that announces the same IP. Traffic near it flows in.
🧾One simple address
Users and configs only need one IP.
Anycast makes β€œnearest and healthy” the job of the network itself.

05Where Do We Use It?

πŸ“–Public DNS
Resolvers like 8.8.8.8 and 1.1.1.1 are anycast. Root DNS servers too.
🌍CDNs
Edge servers share IPs so users hit a close edge.
πŸ›‘οΈDDoS protection
Scrubbing sites absorb attack traffic near its source.
βš–οΈGlobal load balancing
Cloud global LBs give one IP that works worldwide.
πŸ”Edge security and proxies
Web firewalls and gateways at the edge.

06Types / Parts / How It Works

How the network does it

Each site tells its neighbouring networks β€œI can reach 1.2.3.4.” using BGP. Routers collect many such announcements for the same IP. They pick the best path (usually the shortest). So a packet from Paris goes to the site that is closest by network path.

Announce, choose, deliver
🏒Site Aannounces 1.2.3.4
🏒Site Bannounces 1.2.3.4
⇒⇣BGP routes
🌐Routerspick the best path
→↓packets
πŸ§‘User

Failover

If a site fails or is turned off, it stops announcing the IP (it withdraws the route). Routers forget that path and use the next best one. Health software at the site often does this on its own.

Anycast vs the others

TypeWho receives itExample
UnicastOne specific machineYour laptop talking to one web server
BroadcastEveryone on a local networkARP request on your Wi-Fi
MulticastA chosen groupLive video feed to subscribers
AnycastThe nearest of a groupPublic DNS, CDN edge
DNS (geo) load balancing
Different IPs for different users
  • Can be slow to react (TTL)
  • Easy for anyone to set up
  • Choice made at lookup time
Anycast
Same IP for all users
  • Reacts in seconds
  • Needs your own IP block and BGP (or a provider)
  • Choice made by routers per packet
⚠️Nearest on the network is not always nearest on the map
BGP picks the best path, not always the closest city. Sometimes a user is sent to a farther site. Operators tune this.
VisualizerSwitch sites off and watch traffic moveClick any site on the map. Try Unicast too, and compare the delay.
How is the service published?
🌐Everyone connects to the same IP: 198.51.100.1Click a site on the map to switch it off or on
πŸ§‘
Seattle
38 ms
πŸ§‘
New York
54 ms
πŸ§‘
SΓ£o Paulo
95 ms
πŸ§‘
Paris
23 ms
πŸ§‘
Cairo
55 ms
πŸ§‘
Delhi
30 ms
πŸ§‘
Tokyo
60 ms
πŸ§‘
Melbourne
29 ms
Average delay
48 ms
Users re-routed
0
Users failing
0
Anycast: each user reaches the nearest site, even though all sites share one IP. Now click a site to switch it off.

Map is a simple sketch, not to scale. Delay is a made-up number based on distance on the map.

07How to Use It / Steps / Implementation

Most teams do not run anycast on their own. They use a provider (a CDN or a cloud global load balancer) that has done it already. If you do it yourself, the steps look like this.

  1. 1
    Get an IP block and an ASN
    You need your own IP range and an autonomous system number, or rent them from a provider.
  2. 2
    Build several sites
    Put identical servers in different regions.
  3. 3
    Announce the same IP from every site
    Each site speaks BGP to its upstream networks.
  4. 4
    Add health checks
    A local program stops the BGP announcement if the service is unhealthy.
  5. 5
    Keep sites stateless
    Any site must be able to answer any request. Share data between sites if needed.
  6. 6
    Watch where traffic goes
    Track users per site and latency. Tune routing if some users go to far sites.
Anycast plus a load balancer in each site
πŸ§‘User
→↓1.2.3.4
🌐Anycastnearest site
→↓
βš–οΈSite load balancer
→↓
πŸ–₯️
πŸ–₯️
Network chooses the site LB chooses the server
Anycast chooses the site. A normal load balancer inside the site chooses the server.

08Key Terms to Know

TermMeaning in easy words
UnicastOne IP belongs to one machine
AnycastOne IP is shared by many machines. Traffic goes to the nearest
BGPThe protocol that networks use to tell each other which IPs they can reach
Route announcementA message saying β€œI can reach this IP range”
Route withdrawalTaking that message back, so no traffic comes (used for failover)
Edge site / PoPA small data centre close to users (Point of Presence)
ASNA number that identifies a network on the internet
LatencyHow long a request takes to travel and return
DDoSAn attack that floods a service with traffic
Route flapA route that keeps changing. Can break long connections

09Pros, Cons and Trade-offs

βœ… Pros
  • Lower latency for users
  • Fast automatic failover
  • Spreads and absorbs DDoS traffic
  • One IP for the whole world
  • Scales by adding sites
⚠️ Cons
  • Complex to set up (BGP, IP blocks)
  • Hard to debug: which site did the user reach?
  • Long connections may break when routes change
  • Network path is not always the closest site
  • No control of load per site (you can only influence it)

Trade-offs

ChoiceWe gainWe lose
Anycast for DNS or short requestsSpeed, simple failoverNeeds operations skill
Anycast for long TCP connectionsNearest siteRisk of resets if routes change
Use a providerNo BGP to manageLess control, lock-in
Many small sitesCloser to users, spread attacksMore places to run and update

10Common Problems and Mistakes

  1. Stateful services. If a user’s route changes in the middle of a session, a different site answers and does not know them. Keep state shared, or keep sessions short.
  2. Long TCP connections. A route change can reset them. Anycast is great for DNS and short HTTP, harder for long streams.
  3. A broken site still announcing. If health checks fail to withdraw the route, users go to a dead site. Automate it.
  4. Uneven load. The network decides, not you. One site can get far more traffic. Plan capacity for failover (when a site dies, the others take its load).
  5. Hard debugging. β€œWhich site served this user?” Add the site name in a response header or logs.
  6. Inconsistent software versions. Different sites run different code and give different answers. Roll out carefully.
🚫Too little spare capacity
If you have 3 sites at 80% load and one fails, the other two must carry 120%. They fall too. Keep enough room.

11Real-World Examples

πŸ“–Google Public DNS and Cloudflare 1.1.1.1
One IP address, answered from data centres around the world.
🌳DNS root servers
The 13 root server addresses are served by hundreds of anycast sites.
πŸ›‘οΈCloudflare and other CDNs
Edge sites share IPs, which also helps absorb attacks.
☁️Cloud global load balancers
A single global IP that routes to the nearest healthy region.
πŸ“Note
Try it: ping 1.1.1.1 or 8.8.8.8 from your computer. The reply time is tiny because a site very close to you answers.

12Related Topics to Read Next

DNS Load Balancing→

Another way to send users to a near site, but slower to react.

What are Load Balancers?β†’

They balance inside each site.

IP Address→

To understand one IP, many machines.

Availability→

Anycast gives quick automatic failover.

Latency vs Throughput vs Bandwidth→

Anycast mainly reduces latency.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”Anycast: same IP in many places. Network sends you to the nearest.
  • βœ”Tool: BGP route announcements. A failed site withdraws its route.
  • βœ”Wins: low latency, fast failover, DDoS spreading.
  • βœ”Best for: DNS, CDN, short stateless requests.
  • βœ”Careful with: long connections and state.
  • βœ”Plan spare capacity so other sites can take over.
  • βœ”One line: β€œAnycast gives one IP to many sites, and the network picks the nearest one.”
← Previous
DNS Load Balancing
Next β†’
Layer 4 vs Layer 7 Load Balancers