Anycast routing gives one IP address to several servers in different places. Every location announces the same address to the internet, and the routers on the way send each packet to one of those locations. You do not choose the location with a DNS answer. The internet's routing decides, and it can move traffic away from a dead location in seconds.
We continue with BusTrack, the Hyderabad bus tracking app from Load Balancing: Introduction. Its API now has four points of presence (PoPs): hyd-pop, mum-pop, chn-pop and del-pop. All four announce the same prefix, 198.51.100.0/24 (a range reserved for documentation, so you can try the ideas without touching real addresses). Six runnable programs in Java, with Python and C++ versions, measure how the nearest location is chosen, what happens when one fails, why long connections suffer, how to move load, and how a flood is absorbed. The numbers come from models with assumed delays and topologies, so read them as a way to compare designs, not as a forecast.
Key takeaways
BGP picks the route with the shortest AS path, not the shortest distance. In our model only 66.6% of riders reached the nearest PoP. The average round trip was 12.7 ms against 7.5 ms if everyone had gone to the nearest one, and 23.2 ms for a random PoP.
If the app dies but the router keeps announcing the prefix, packets are swallowed. Waiting for an operator at 300 seconds failed 1,269,300 requests. A health script that withdraws the route after 6 seconds failed 116,820, and BFD (about 1 second) failed 97,220.
A TCP connection lives on one PoP. With 4 route changes in an hour, 4.4% of live tracking sockets were reset, and 23.7% of 20 to 60 minute sockets. Short requests lost 0.1%.
Anycast does not balance load.
hyd-popsat at 106.8% of capacity. One AS-path prepend toward every neighbour over-corrected and overloaded two other PoPs. Prepending toward 10% to 30% of neighbours fixed it.A 60 Gbit/s flood took a single 20 Gbit/s site down to serving 32.6% of real riders. Four anycast PoPs served 76.8%, and blocking the 40 noisiest networks lifted that to 93.1%.
GeoDNS was slightly faster when everything was healthy (11.0 ms against 12.6 ms) but anycast recovered from a PoP failure in 41 seconds instead of 90.
1. What Is Anycast Routing?
Most addresses name one machine. An anycast address names a service that runs in several places. The same 198.51.100.0/24 is announced from every PoP using BGP, the protocol that networks (called autonomous systems, or AS) use to tell each other which addresses they can reach. A rider's access network hears the announcement from several directions, keeps the best route, and forwards the rider's packets along it.
Four PoPs announce one prefix; each access network keeps the best route it hears.
Compare this with DNS load balancing, where the answer to a question names one location and is cached. Anycast moves the choice from a cached answer to the routing table, which updates when the network changes. Three things follow.
The rider needs no special software. The same address works everywhere.
The choice is made by routers you do not control, once per packet path, based on route attributes and not on server load.
Anycast works best for short, independent exchanges, such as a DNS query or a single HTTPS request, because each packet may be routed on its own.
2. How BGP Picks a PoP
BGP does not know kilometres or milliseconds. Among the routes it hears, a router first prefers the highest local preference that its operator set, and then the shortest AS path, meaning the fewest networks to cross. Distance only matters indirectly: nearby networks tend to be a few hops apart, but a network that happens to peer directly with a faraway PoP can see a shorter path to it than to the nearby one.
The shorter AS path wins, even when the other PoP is the nearer city.
The first program builds 600 access networks in eight cities and gives each a path length to each PoP: a base of 2, plus a small part from distance, plus 0 to 2 for the peering the network happens to have. Each network keeps the shortest path (a tie goes to the lowest router id). Then 100,000 riders pick a network at random.
Output
BusTrack announces 198.51.100.0/24 from 4 PoPs: hyd-pop, mum-pop, chn-pop, del-pop. 600 access networks, 100000 riders.
rider city share reaches the nearest PoP
Hyderabad 28.0% 74.4%
Chennai 9.6% 72.2%
Bengaluru 18.0% 45.0%
Mumbai 15.4% 69.4%
Pune 7.1% 73.9%
Delhi 11.8% 64.6%
Kolkata 5.5% 57.9%
Ahmedabad 4.2% 88.1%
riders sent to the nearest PoP: 66.6%
average rtt, always the nearest PoP: 7.5 ms
average rtt, anycast (BGP decides): 12.7 ms
average rtt, a random PoP: 23.2 ms
extra rtt vs the nearest PoP: median 0 ms, p95 21 ms, worst 29 ms
catchment (share of riders each PoP receives)
hyd-pop 35.6%
mum-pop 32.7%
chn-pop 21.6%
del-pop 9.9%
66.6% of riders were sent to the nearest PoP. Bengaluru was the worst at 45.0% because its networks often see a shorter path to
hyd-popormum-popthan tochn-pop. Ahmedabad was the best at 88.1%.Average round trip: 7.5 ms if every rider reached the nearest PoP, 12.7 ms with anycast, 23.2 ms for a random PoP. The median extra delay was 0 ms but the p95 was 21 ms and the worst case 29 ms.
The catchments were uneven:
hyd-popreceived 35.6% of riders anddel-poponly 9.9%, although Delhi riders alone are 11.8% of the audience. We use this imbalance in section 5.
Anycast is good on average and sometimes wrong for a particular network.
Anycast is therefore "usually near", not "always nearest". More PoPs and good peering raise the share. If you need the exact nearest location, you add a layer that knows the rider, such as GeoDNS with the client subnet (section 7).
3. When a PoP Fails: Withdraw the Route
The strength of anycast is failover without any rider doing anything. When mum-pop stops announcing the prefix, networks that used it drop the route and switch to their next best one. The difficulty is making the PoP stop announcing when it is broken. A crashed application does not stop the BGP process on the same machine or router, so the prefix is still announced and packets are delivered to a PoP that answers nobody. This is called a blackhole.
A broken app can keep its route; something must withdraw it.
Four things can notice the failure and withdraw the route, each with a different delay.
A person who sees an alert and logs in.
The BGP hold timer: if the neighbour sends nothing for the hold time (RFC 4271 suggests 90 seconds, and many routers use longer) the session is declared dead. This catches a dead box, not a dead application.
BFD (bidirectional forwarding detection, RFC 5880) sends very small packets every few hundred milliseconds. Three misses at 300 ms mean about a second to notice a dead router or link. It does not see application failures either.
A health script on the PoP that probes the real service and tells the BGP daemon to withdraw the prefix after several failed probes.
The second program fails mum-pop, which serves 196 access networks sending 20 requests per second each (3,920 requests per second). After the failure is noticed, the withdrawal takes 5 to 40 seconds to spread across the internet. The DNS rows use the same networks with a cached answer instead of a route.
Output
mum-pop serves 196 access networks, 3920 requests/s. It fails at t = 0.
recovery mechanism detect last rider back failed requests still failing at 30 s at 60 s
GeoDNS, TTL 300 s, 3 health checks x 10 s 30 s 328 s 728820 99.4% 89.2%
GeoDNS, TTL 60 s, 3 health checks x 10 s 30 s 89 s 238260 97.9% 54.0%
anycast, app dead but BGP keeps announcing 300 s 340 s 1269300 100.0% 100.0%
anycast, BGP hold timer (box dies) 90 s 130 s 446100 100.0% 100.0%
anycast, BFD 3 x 300 ms (router dies) 1 s 41 s 97220 35.2% 0.0%
anycast, health script 3 x 2 s withdraws route 6 s 46 s 116820 50.5% 0.0%
When the app was dead but BGP kept announcing, the outage lasted until an operator acted at 300 seconds. The last rider returned at 340 seconds, with 1,269,300 failed requests.
The hold timer needed 90 seconds and failed 446,100 requests.
A health script with 3 probes 2 seconds apart (6 seconds) failed 116,820, and BFD failed 97,220. Even with instant detection, the last rider came back at 41 seconds, because the withdrawal still has to spread.
GeoDNS with a 60 second TTL failed 238,260 requests and with a 300 second TTL 728,820, as in the DNS lesson.
Detection speed decides the damage; propagation sets a floor.
Use both: BFD for router and link failures, and a health script for application failures. Always include a way to stop announcing when the PoP is unhealthy, and an automatic way to make sure that script is not the thing that fails, for example by withdrawing the route when the script cannot run.
4. Long Connections and Route Changes
Anycast sends each packet along whatever route is best at that moment. If the best route changes in the middle of a TCP connection, the next packets arrive at a different PoP. That PoP has no record of the connection, so it answers with a reset. The same happens when a PoP is added, removed or its path length changes.
The connection state lives on one PoP; the route can change without it.
The third program opens 50,000 sockets spread across one hour. Each route change moves 15% of the access networks to another PoP. A socket is reset if its network moves during its life.
Output
A TCP connection lives on the PoP where it started. If BGP moves its access network to another PoP, the packets reach a PoP
that has never heard of the connection and answers with a reset. 50000 live tracking sockets (30 s to 8 min) in one hour.
Each route change moves 15% of the access networks.
route changes per hour sockets reset share of sockets worst single change
0 0 0.0% 0 sockets
1 494 0.9% 494 sockets
4 2010 4.0% 551 sockets
12 6358 12.7% 596 sockets
60 22232 44.4% 482 sockets
same hour with 4 route changes, different connection lengths
connection style sockets reset share of sockets
new connection per poll (5-15 s) 85 0.1%
live tracking socket (30 s - 8 min) 2203 4.4%
all-day socket (20 - 60 min) 11884 23.7%
With no route changes nothing is reset. One change reset 0.9% of live tracking sockets, four reset 4.0%, twelve reset 12.7%, and sixty reset 44.4%.
Connection length matters as much as route stability. With 4 changes an hour, polling with a new short connection each time lost 0.1%. A live tracking socket (30 seconds to 8 minutes) lost 4.4%. An all-day socket (20 to 60 minutes) lost 23.7%.
Fewer route changes and shorter connections both cut resets.
What to do about it: keep routes stable (avoid announcing and withdrawing for small reasons), keep connections short or easy to resume, and make the app reconnect with a resume token so a reset costs a moment and not a login. Some large operators also share connection state between PoPs or steer long connections to a single regional address, but that adds complexity you should only accept when the numbers justify it.
5. Uneven Load and AS-Path Prepending
Anycast does not look at load. A network keeps sending to its chosen PoP even if that PoP is full. In the first program hyd-pop collected 35.6% of the riders. At BusTrack's peak of 12,000 requests per second, with capacities of 4,000, 4,500, 3,000 and 2,500 requests per second, that is 106.8% of what hyd-pop can serve.
The classic lever is AS-path prepending: announce the prefix from hyd-pop with your own AS number repeated, so the path looks longer and some networks prefer another PoP.
A longer-looking path pushes some networks to another PoP.
The fourth program tries prepending toward every neighbour, and then toward only a share of the neighbours.
Output
BusTrack peak is 12000 requests/s. Capacity: hyd-pop 4000, mum-pop 4500, chn-pop 3000, del-pop 2500.
To move load away from hyd-pop we make its route look longer by adding our own AS number to the path (AS-path prepending).
prepends on hyd-pop, all neighbours hyd-pop mum-pop chn-pop del-pop overloaded avg rtt
0 extra hops 106.8% 87.3% 86.5% 47.9% 1 PoP 12.7 ms
1 extra hop 50.5% 112.2% 116.9% 56.5% 2 PoP 12.9 ms
2 extra hops 13.8% 131.5% 135.1% 58.8% 2 PoP 14.0 ms
3 extra hops 0.4% 139.6% 140.7% 58.8% 2 PoP 14.7 ms
1 extra hop, only to some neighbours hyd-pop mum-pop chn-pop del-pop overloaded avg rtt
1 extra hop to 5% of neighbours 104.3% 87.7% 89.2% 47.9% 1 PoP 12.7 ms
1 extra hop to 10% of neighbours 99.8% 89.9% 91.3% 48.8% none 12.7 ms
1 extra hop to 15% of neighbours 98.2% 89.9% 93.3% 48.8% none 12.7 ms
1 extra hop to 20% of neighbours 96.8% 90.7% 94.0% 48.8% none 12.6 ms
1 extra hop to 30% of neighbours 90.2% 94.2% 97.6% 48.8% none 12.8 ms
1 extra hop to 50% of neighbours 78.3% 99.1% 104.9% 50.2% 1 PoP 12.8 ms
With no prepending
hyd-popwas at 106.8% and the others between 47.9% and 87.3%.One extra hop toward every neighbour was too much:
hyd-popfell to 50.5%, andmum-pop(112.2%) andchn-pop(116.9%) were overloaded. Prepending is a blunt tool, because small differences in path length are common.One extra hop toward only 10% to 30% of the neighbours was enough: all PoPs stayed at or below 98%, and the average round trip stayed at 12.6 to 12.8 ms. Going to 50% overloaded
chn-popat 104.9%.
Move a little at a time; one big change can overload the next PoP.
Change one thing, wait for the routes to settle, measure, and repeat. Also keep headroom: a design where every PoP can survive the loss of another is far safer than one that relies on prepending.
6. Absorbing a DDoS Flood
A denial of service flood tries to fill a link or exhaust a server. With a single location, the whole flood arrives at one place. With anycast, each bot's traffic goes to the PoP its own network picks, so the flood is split by the same catchments as real riders. Each PoP only has to survive its share.
The fifth program sends 60 Gbit/s of junk, about 5% of the networks hosting most of the bots, together with 1.2 Gbit/s of real riders. A router that is over capacity drops packets at random, so real traffic suffers the same loss rate as junk. Uplinks are 10, 20, 10 and 10 Gbit/s.
Output
A botnet sends 60.0 Gbit/s of junk at BusTrack's address while real riders send 1.2 Gbit/s.
A router that is over capacity drops packets at random, so real and junk packets suffer equally.
one site, mum-pop alone (20.0 Gbit/s): real riders served 32.6%
anycast, same flood spread by BGP
PoP capacity flood in riders in link load real riders served
hyd-pop 10.0 Gbit/s 19.2 Gbit/s 427 Mbit/s 196.9% 50.7%
mum-pop 20.0 Gbit/s 20.1 Gbit/s 393 Mbit/s 102.6% 97.4%
chn-pop 10.0 Gbit/s 12.5 Gbit/s 259 Mbit/s 128.5% 77.7%
del-pop 10.0 Gbit/s 8.0 Gbit/s 119 Mbit/s 81.2% 100.0%
all four PoPs: real riders served 76.8%
anycast, then our edge drops traffic from the N networks that send the most flood (a remote blackhole at the upstream)
networks blocked flood left real riders lost to the block real riders served
0 59.9 Gbit/s 0.0% 76.8%
5 49.2 Gbit/s 0.8% 80.2%
10 39.6 Gbit/s 1.6% 85.2%
20 24.0 Gbit/s 3.4% 89.8%
40 10.7 Gbit/s 6.8% 93.1%
A single 20 Gbit/s site (
mum-popalone) served only 32.6% of the real riders.Four anycast PoPs served 76.8%. The split was not even:
hyd-poptook 19.2 Gbit/s of flood on a 10 Gbit/s link (196.9%) and served half its riders, whiledel-popstayed at 81.2% and lost none.Blocking the noisiest networks at the edge (a remote blackhole at the upstream provider, or a flow filter) raised the served share to 80.2% with 5 networks blocked and 93.1% with 40. The price is the real riders inside the blocked networks: 0.8% lost with 5 blocked and 6.8% with 40.
Anycast spreads a flood; blocking trades a few real riders for a calmer network.
Real services add scrubbing centres and rate limits too. The lesson from the numbers is that spreading the flood buys time, and that you should put capacity where the flood is likely to land.
7. Anycast or GeoDNS?
The sixth program compares the two ways to send riders to a PoP, with 25% of the riders behind a public DNS resolver that the authoritative server sees in Mumbai. GeoDNS chooses by the resolver's location; anycast chooses by BGP. Then mum-pop fails.
Output
BusTrack, 100000 riders, 25% behind a public DNS resolver that sits in Mumbai. Peak 12000 requests/s.
all four PoPs healthy
policy avg rtt p95 rtt riders on mum-pop
GeoDNS by resolver location 11.0 ms 26 ms 45.3%
anycast (BGP decides) 12.6 ms 26 ms 32.5%
mum-pop fails in the middle of the day
policy riders hit failed requests longest outage rtt of hit riders afterwards
GeoDNS, TTL 60 s, 3 checks x 10 s 45310 326371 90 s 20.8 ms
anycast, BFD (1 s) 32545 90009 41 s 19.4 ms
anycast, health script (6 s) 32545 109536 46 s 19.4 ms
When everything is healthy GeoDNS was faster: 11.0 ms on average against 12.6 ms for anycast, with the same p95 of 26 ms. It also put 45.3% of the riders on
mum-pop(the public resolver effect) against 32.5% for anycast.When
mum-popfailed, GeoDNS affected more riders (45,310 against 32,545), failed 326,371 requests against 90,009 for anycast with BFD, and the longest outage was 90 seconds against 41.A health script instead of BFD cost a little more: 109,536 failed requests and 46 seconds.
So GeoDNS can pick a slightly better PoP, and anycast recovers sooner. Large services use both: DNS to choose a region with knowledge of the rider, and anycast within the region for fast failover.
8. Anycast in Front of Load Balancers
Anycast gets a rider to a PoP. It does not choose a server. Inside the PoP, the router spreads flows over several machines with ECMP (equal cost multipath), usually by hashing the connection's addresses and ports. Those machines are layer 4 load balancers that pass each connection to a server such as hyd-api-1. If a load balancer machine is added or removed the hash can move established flows, which is why these balancers use consistent hashing and a shared connection table. We build one in Design Load Balancer.
Anycast chooses the PoP, ECMP chooses the balancer, the balancer chooses the server.
Choosing
Anycast for short stateless exchanges; regional addresses where connections are long.
| Need | Use | Watch out for |
|---|---|---|
| DNS, HTTPS APIs, CDN edges | Anycast | Uneven catchments |
| Long TCP or WebSocket sessions | Anycast to a regional entry, then regional addresses | Resets on route change |
| Exact load shares | A load balancer behind anycast | Anycast cannot see load |
| Fast failover | BFD plus a health script that withdraws routes | Propagation takes 5 to 40 s |
| Flood protection | Many PoPs, spare uplink capacity, edge blocking | Collateral loss from blocking |
| Best PoP for a particular rider | GeoDNS with client subnet on top | Public resolvers hide the rider |
9. Common Mistakes
Announcing the prefix from a PoP whose application is down, so packets are swallowed.
Assuming BGP picks the nearest PoP. It picks the shortest AS path.
Using anycast for long TCP sessions with unstable routes and no resume logic.
Prepending on every neighbour and overloading the next PoP.
Sizing each PoP for its average share and not for a neighbour's failure.
Testing from one location only. Catchments differ by network, so probe from many.
10. Best Practices
Withdraw the route automatically when the service on a PoP fails its health checks.
Run BFD on router sessions so a dead link or box is noticed within a second or two.
Keep every PoP able to take a failed neighbour's load, or know exactly which load you will shed.
Change announcements slowly and one at a time, and measure after each change.
Keep connections short or resumable.
Measure catchments from real riders, not from a few test machines.
11. Practice Problems
In the first program, change the random part of the path length from 0 to 2 to 0 to 1. How does the share that reaches the nearest PoP change?
In the second program, make the withdrawal take 5 to 90 seconds. Which mechanism is hurt most?
In the third program, make 30% of the networks move on each route change. Predict the reset share for 12 changes before you run it.
In the fourth program, prepend on
mum-popinstead ofhyd-pop. Which PoPs overload?In the fifth program, double the capacity of
hyd-pop. How many networks do you still need to block to reach 90% served?In the sixth program, set the share of public resolvers to 0. Which policy is faster now?
12. Interview Questions
How does anycast pick a location? Each network keeps the best BGP route it hears, usually the shortest AS path, so the choice follows routing and not distance or load.
What happens if the application on one PoP crashes? If the PoP keeps announcing the prefix, traffic is blackholed. A health script must withdraw the route.
Why are long TCP connections a problem? A route change can send packets to a PoP that has no state for the connection, which answers with a reset.
How do you shift load between anycast PoPs? Change how attractive a route looks, for example by AS-path prepending toward some neighbours, in small steps.
When would you choose GeoDNS over anycast? When you need the best PoP for a particular rider and can accept slower failover. Many systems use both.
Summary
Anycast announces one address from many places and lets BGP choose, usually by the shortest AS path.
It is near on average, not always nearest, and it ignores load.
Failover is only as fast as the detection that withdraws the route plus the time it takes to spread.
Route changes reset long TCP connections, so keep them short or resumable.
Prepending moves load but in big steps; use it selectively.
Spreading a flood across PoPs raises the share of real riders served.
Conclusion
Anycast is the fast, global front door: one address, many locations, and failover that does not wait for a cache. It is a poor way to balance load or to carry long connections on its own, so pair it with load balancers inside each PoP. That is the next step: how to design the balancer itself.
Previous topic: DNS Load Balancing. Next topic: Design Load Balancer.