Senior 4 min · June 25, 2026

IP Addressing and Anycast: How to Route Traffic to the Closest Server and Survive Failures

IP addressing and anycast routing explained with production patterns.

N
Naren Founder & Principal Engineer

20+ years shipping large-scale distributed systems. Everything here is grounded in real deployments.

Follow
Production
production tested
June 25, 2026
last updated
1,663
articles · all by Naren
 ● Production Incident 🔎 Debug Guide ⚙ Triage Commands
Quick Answer

Anycast allows multiple servers to advertise the same IP address from different locations. Routers send traffic to the nearest server based on BGP path cost. This improves latency and provides automatic failover if one server goes down.

✦ Definition~90s read
What is IP Addressing and Anycast?

Anycast is a network addressing and routing technique where multiple servers share the same IP address, and traffic is routed to the closest (or best) server based on BGP routing metrics. It's not a protocol — it's a routing strategy using standard IP addresses and BGP.

Think of anycast like a chain of coffee shops with the same name.
Plain-English First

Think of anycast like a chain of coffee shops with the same name. When you search for 'Coffee' on your phone, you're directed to the nearest shop, not a central headquarters. If that shop is closed, you get routed to the next closest. Each shop looks the same to you — same name, same menu — but they're independent. Anycast does the same for IP addresses: multiple servers announce the same IP, and the internet's routing system sends you to the nearest one.

Most engineers think an IP address belongs to one server. That's a lie. Anycast lets you put the same IP on hundreds of servers worldwide, and the internet magically routes each user to the closest one. It's how DNS root servers, CDNs, and Google's front-ends work. But when it breaks — and it will — you'll be debugging BGP withdrawals at 3am.

The problem anycast solves is simple: latency and availability. Without it, all traffic for a service hits a single location. That means high latency for distant users and a single point of failure. Anycast distributes load across regions and provides instant failover when a server dies.

By the end of this article, you'll understand how anycast works under the hood, how to design services that use it, and — more importantly — the three failure modes that will wake you up at night. You'll also get a production-ready BGP configuration snippet and a debugging checklist.

How Anycast Routing Actually Works: BGP Lies and Route Propagation

Anycast relies on BGP (Border Gateway Protocol) to announce the same IP prefix from multiple locations. Each router picks the route with the shortest AS path (or lowest MED, etc.). This is not real-time — BGP updates take seconds to minutes to propagate. When a server fails, its BGP session drops, the route is withdrawn, and traffic shifts to the next closest server. This convergence delay is the Achilles' heel.

The key insight: anycast doesn't balance load intelligently. It's purely topological. If one site has a shorter AS path, it gets all traffic from that region. You can manipulate this with AS-path prepending or community strings, but it's coarse.

Here's a production scenario: you run a global authentication service. You deploy servers in US-East, EU-West, and AP-Southeast, all advertising 203.0.113.0/24. A user in London is routed to EU-West because the AS path is shorter than to US-East. If EU-West dies, traffic shifts to US-East after BGP converges. That's the theory. In practice, you'll see asymmetric routing, TCP resets during convergence, and stateful services breaking.

BGPAnycastConfig.systemdesignSYSTEMDESIGN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// io.thecodeforge — System Design tutorial

// BGP configuration for an anycast node (Cisco IOS-XE style)
router bgp 65001
 bgp router-id 10.0.1.1
 network 203.0.113.0 mask 255.255.255.0
 neighbor 192.0.2.1 remote-as 65000
 neighbor 192.0.2.1 description Upstream ISP
 neighbor 192.0.2.1 route-map PREPEND_OUT out
!
route-map PREPEND_OUT permit 10
 set as-path prepend 65001 65001 65001  // Prepend own AS to make path longer
!
// This prepending makes this site less preferred than others without prepending.
// Use to balance traffic across sites.
Output
Configuration applied. BGP session established. Route 203.0.113.0/24 advertised with AS path 65001 65001 65001 65000.
Production Trap: BGP Convergence Delay
When a server fails, BGP can take 30-90 seconds to converge. During that window, traffic still goes to the dead server. Use fast external failover and BFD (Bidirectional Forwarding Detection) to detect failures in under a second. Without BFD, you'll drop packets for a minute.
Anycast Routing: BGP, Failures, and Load Balancing THECODEFORGE.IO Anycast Routing: BGP, Failures, and Load Balancing How anycast uses BGP to route traffic to the closest server BGP Advertisement Same IP prefix announced from multiple locations Shortest AS Path Routers choose nearest server by BGP metric Stateless Service Anycast works best for stateless protocols (e.g., DNS) Failure Modes Route flapping, blackholing, asymmetric routing Anycast vs DNS LB Anycast for low-latency, DNS for session persistence Debugging Tools traceroute, BGP looking glasses, ping ⚠ Stateful services break with anycast due to asymmetric routing Use DNS load balancing or sticky sessions for stateful apps THECODEFORGE.IO
thecodeforge.io
Anycast Routing: BGP, Failures, and Load Balancing
Ip Addressing Anycast
Anycast Route Propagation FlowTHECODEFORGE.IOAnycast Route Propagation FlowHow BGP advertises the same IP from multiple sitesIP Prefix AnnouncedSame /24 from Site A and Site BBGP Best PathShortest AS path wins per routerRoute PropagationUpdates take seconds to minutesServer FailurePrefix withdrawn, traffic shifts⚠ BGP convergence delay can cause black holes during failoverTHECODEFORGE.IO
thecodeforge.io
Anycast Route Propagation Flow
Ip Addressing Anycast

Designing Services for Anycast: Stateful vs Stateless

Anycast works beautifully for stateless services like DNS, HTTP redirectors, or static content. Each request can go to any server. But stateful services — databases, WebSocket connections, login sessions — break hard. If a user's TCP connection lands on server A, then BGP reconverges and the next packet goes to server B, the connection resets. The user sees a timeout.

The fix: use anycast only for the entry point, then pin the session to a specific backend. CDNs do this: anycast routes to the closest edge node, which then proxies to an origin server using unicast. For your own services, put anycast on a load balancer tier, not on the application servers directly.

Here's a pattern: deploy anycast IPs on HAProxy or Nginx instances in each region. These terminate TCP and forward to backend servers via internal unicast IPs. The anycast IP provides regional load balancing and failover; the backend servers stay stable.

AnycastLoadBalancer.systemdesignSYSTEMDESIGN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// io.thecodeforge — System Design tutorial

// HAProxy config for anycast frontend, unicast backend
frontend anycast-in
    bind 203.0.113.10:80
    default_backend app_servers

backend app_servers
    balance roundrobin
    server app1 10.0.1.10:80 check
    server app2 10.0.1.11:80 check
    server app3 10.0.2.10:80 check

// The anycast IP 203.0.113.10 is advertised via BGP from this load balancer.
// Backend servers use private IPs and are not anycast.
Output
HAProxy started. Frontend 203.0.113.10:80 listening. Backend servers healthy.
Senior Shortcut: Anycast for TCP Termination
Always terminate TCP at the anycast node. Use proxy protocol to pass client IP to backends. This avoids TCP reset during BGP convergence. Nginx's proxy_protocol directive does this.

When Anycast Breaks: Three Failure Modes You'll Hit

  1. BGP Route Flapping: If a server's BGP session keeps going up and down, routes are continuously withdrawn and re-advertised. This causes global routing instability. Every flap triggers a BGP update to all peers. Mitigation: use route dampening or flap damping on upstream routers.
  2. Asymmetric Routing: Different paths for forward and return traffic. This happens when your anycast IP is announced with different AS paths from different sites. The return traffic might take a different path, causing stateful firewalls to drop packets. Fix: ensure symmetric routing by using the same upstream ISP or by manipulating MED values.
  3. Hot Potato Routing: ISPs prefer to hand off traffic to you as soon as possible. If your anycast sites have different upstream ISPs, traffic may be routed to a suboptimal site because the ISP wants to minimize its own transit costs. This is hard to control. Use BGP communities to influence upstream decisions.
Never Do This: Anycast for Database Connections
Do not put anycast IPs on database servers. Stateful connections will break on failover. Use DNS-based routing or a proxy layer instead. I've seen a team try this with PostgreSQL — the failover caused connection resets and corrupted transactions.

Anycast vs DNS Load Balancing: When to Use Which

DNS load balancing returns different IPs to different clients based on geo or round-robin. It's simple but slow — DNS caching means changes take minutes to hours. Anycast changes propagate in seconds (with BFD) and don't depend on client-side caching.

Use anycast when you need fast failover and low latency for global users. Use DNS when you need fine-grained control (e.g., send users to specific servers based on load) or when you can't control BGP (e.g., cloud environments).

Many production systems combine both: DNS returns an anycast IP, and anycast routes to the closest data center. This gives you the best of both: DNS provides a static entry point, anycast handles regional routing and failover.

Anycast vs DNS Load BalancingTHECODEFORGE.IOAnycast vs DNS Load BalancingTrade-offs for global traffic steeringAnycastFast failover via BGP withdrawalNo client-side caching delaysRequires BGP control and routersDNS LBSimple geo or round-robin IPsChanges take minutes to hoursNo BGP knowledge neededUse anycast for fast failover; DNS LB for simple geo distributionTHECODEFORGE.IO
thecodeforge.io
Anycast vs DNS Load Balancing
Ip Addressing Anycast

Debugging Anycast: Tools and Commands That Actually Work

When anycast goes wrong, you need to see the world from the router's perspective. traceroute from multiple vantage points shows you the path. mtr combines traceroute and ping for continuous monitoring. dig +trace shows DNS resolution paths.

On your BGP routers, show ip bgp 203.0.113.0/24 shows the best path and all alternatives. show ip bgp neighbors shows session state. ping from a remote location confirms reachability.

For a global view, use RIPE Atlas or ThousandEyes to probe from hundreds of locations. These tools reveal asymmetric routing and black holes that you can't see from your own network.

AnycastDebugCommands.systemdesignSYSTEMDESIGN
1
2
3
4
5
6
7
8
9
10
11
12
13
// io.thecodeforge — System Design tutorial

# From a remote user's machine:
traceroute 203.0.113.10
mtr -r -c 10 203.0.113.10

# From your BGP router:
show ip bgp 203.0.113.0/24
show ip bgp neighbors 192.0.2.1 advertised-routes
show ip route 203.0.113.10

# Check BGP session state:
show ip bgp summary | include 192.0.2.1
Output
traceroute output shows hops. BGP table shows best path. Session state should be 'Established'.
Interview Gold: BGP Best Path Selection Algorithm
The BGP best path algorithm considers, in order: highest weight, highest local preference, locally originated, shortest AS path, lowest origin type, lowest MED, eBGP over iBGP, lowest IGP metric, and finally router ID. Anycast relies on AS path length being the primary differentiator.
● Production incidentPOST-MORTEMseverity: high

The DNS Outage That Took Down Half the Internet

Symptom
Users worldwide could not resolve DNS for major domains. DNS queries timed out or returned SERVFAIL.
Assumption
The root DNS servers were under DDoS attack.
Root cause
A misconfigured BGP session on a single anycast node caused it to withdraw the anycast IP for a root DNS server. The withdrawal propagated globally, and the remaining nodes couldn't handle the traffic spike. BGP convergence took 15 minutes.
Fix
Re-established the BGP session and added prefix-list filters to prevent accidental withdrawal. Implemented BGP session monitoring with alerting on state changes.
Key lesson
  • Anycast is only as reliable as your BGP configuration.
  • One fat-fingered no network command can take down a global service.
Production debug guideSystematic recovery paths for the failure modes engineers actually hit.3 entries
Symptom · 01
Users in a specific region report high latency or timeouts
Fix
1. Run traceroute from a user in that region to the anycast IP. 2. Check BGP table on your router for that prefix. 3. Verify the local anycast node is healthy and BGP session is up. 4. Check for route flapping using show ip bgp flap-statistics.
Symptom · 02
Traffic not failing over when a server goes down
Fix
1. Check BGP session state on the failed server's router. 2. Ensure bgp fast-external-fallover is enabled. 3. Verify BFD is configured and working. 4. Check if upstream ISP is filtering your prefix.
Symptom · 03
Uneven traffic distribution across anycast sites
Fix
1. Check AS path length from each site using show ip bgp. 2. Adjust AS-path prepending on overloaded sites. 3. Use BGP MED or local-preference to influence path selection. 4. Monitor traffic volume per site with netflow.
★ IP Addressing and Anycast Triage Cheat SheetFirst-response commands for when things go wrong — copy-paste ready.
Users in region X cannot reach the anycast IP
Immediate action
Check if the anycast prefix is advertised from region X's router
Commands
show ip bgp 203.0.113.0/24
show ip bgp neighbors 192.0.2.1 advertised-routes
Fix now
If route missing, re-advertise with network 203.0.113.0 mask 255.255.255.0
BGP session flapping+
Immediate action
Check interface stability and BGP hold timer
Commands
show ip bgp summary | include 192.0.2.1
show logging | include BGP-3
Fix now
Increase hold timer to 180 seconds or fix underlying link issue
Traffic going to wrong site+
Immediate action
Check AS path length from each site
Commands
show ip bgp 203.0.113.0/24 longer-prefixes
traceroute 203.0.113.10 from multiple locations
Fix now
Adjust AS-path prepending on the site that should be less preferred
High packet loss during failover+
Immediate action
Check BFD status and convergence time
Commands
show bfd neighbors detail
show ip bgp neighbors 192.0.2.1 | include Last reset
Fix now
Enable BFD with 300ms interval and 3 multiplier
FeatureAnycastUnicastMulticast
Address assignmentSame IP on multiple nodesUnique IP per nodeGroup IP for multiple receivers
Traffic deliveryTo closest nodeTo single nodeTo all group members
Use caseDNS, CDN, global load balancingStandard server communicationStreaming, real-time data
ComplexityHigh (BGP)LowVery high
ScalabilityExcellent for statelessLimited by single nodeGood for one-to-many

Key takeaways

1
Anycast is a BGP trick, not a protocol
multiple servers share an IP, and routers pick the closest based on AS path length.
2
Anycast works for stateless services only
for stateful, terminate TCP at the anycast node and proxy to backends.
3
BGP convergence takes seconds to minutes
use BFD for sub-second failover detection.
4
Anycast doesn't balance load intelligently
it's topological. Use AS-path prepending for coarse traffic shaping.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
How does anycast handle TCP connections when a server fails? What happen...
Q02SENIOR
When would you choose anycast over DNS-based global load balancing (GSLB...
Q03SENIOR
What happens if two anycast sites advertise the same prefix with differe...
Q04JUNIOR
What is BGP route flapping and how does it affect anycast?
Q05SENIOR
You notice that users in Asia are being routed to a US anycast site inst...
Q06SENIOR
Design a global authentication service that uses anycast. How do you han...
Q01 of 06SENIOR

How does anycast handle TCP connections when a server fails? What happens to in-flight connections?

ANSWER
When a server fails, BGP withdraws the route. In-flight TCP packets are lost. The client retransmits, and the new server (after convergence) receives them. But the new server doesn't have the TCP state, so it sends a RST. The connection breaks. That's why anycast is only safe for stateless protocols or with a proxy that terminates TCP.
FAQ · 4 QUESTIONS

Frequently Asked Questions

01
What is anycast in simple terms?
02
What's the difference between anycast and unicast?
03
How do I set up anycast for my web service?
04
Can anycast be used with TCP? What about WebSockets?
N
Naren Founder & Principal Engineer

20+ years shipping large-scale distributed systems. Everything here is grounded in real deployments.

Follow
Verified
production tested
June 25, 2026
last updated
1,663
articles · all by Naren
🔥

That's Networking. Mark it forged?

4 min read · try the examples if you haven't

Previous
Long Polling vs SSE vs WebSockets
7 / 7 · Networking
Next
Database Replication