Anycast = the same IP address announced from multiple locations. BGP routes traffic to whichever location is topologically closest. Cloudflare's 1.1.1.1, Google's 8.8.8.8, root DNS servers — all use anycast. For DNS specifically, anycast turns a global service into a low-latency one.
How it works
Servers in N locations announce the same IP prefix via BGP. Each ISP's router picks the 'best' (shortest AS path) advertisement. Users hit the closest location — usually also geographically closest. No DNS-level redirection needed.
Why DNS loves anycast
DNS queries are tiny + stateless. UDP makes anycast trivial (no connection state to worry about). DNS resolution latency directly hits page load — anycast cuts it from 50ms to 5ms for users in well-served regions.
Failover
If a location goes down, it stops announcing the prefix. BGP withdraws within seconds. Traffic shifts to the next-closest. No human intervention; convergence in 10-60 seconds depending on AS topology.
TCP and anycast
Anycast for TCP is harder — if a connection mid-flight gets routed to a different location, state is lost. Possible with careful ECMP hash + connection-stickiness via cookies. Cloudflare and Fastly do this; rolling your own is hard.
Self-hosting anycast
Requires your own AS number + multiple BGP peerings. Not feasible for most teams. Use a managed service (Route 53 with health checks ≈ DNS anycast, Cloudflare for app-layer anycast).