How Anycast Works
Normally one IP address belongs to one place. With anycast, an operator puts servers in several data centers and announces the same IP prefix from each of them with BGP. Every network on the internet then learns several paths to that prefix and, following its own routing policy, picks one, often the one with the shortest AS path. Traffic from a user lands at whichever location the user's network prefers. How networks exchange and choose routes is explained in what is BGP.
Nearest means nearest by routing, not by map. A user can be sent to a location farther away when their provider has a better connection there.
Unicast, Anycast, and the Others
| Type | Who receives a packet |
|---|---|
| Unicast | One address, one host |
| Anycast | One address, many hosts; the nearest one |
| Multicast | Every host that joined a group |
| Broadcast | Every host on the local network (IPv4) |
An anycast address looks like any other address. Nothing in the number tells you it is anycast; the difference lies only in how it is announced.
Where Anycast Is Used
- DNS: the root servers, many top-level domain servers, large public resolvers, and hosted DNS services use anycast, so a lookup is answered close to the user. See what is DNS.
- CDNs: many content delivery networks send visitors to the nearest edge location through anycast addresses. See what is a CDN.
- Other short exchanges: services where each request is small and the server keeps little state per user, so it does not matter which location answers.
What It Means for Latency
Anycast shortens the distance to the first server a user reaches, which lowers round-trip time for users far from any single data center. Why that distance matters is explained in latency vs bandwidth. It does not make the far end of a request faster: if the edge location has to fetch data from one central server, that leg still takes as long as before.
What It Means for Failover
Anycast also fails over through routing. When one location goes offline, its routers stop announcing the prefix, and other networks move to the next-best path as BGP converges, which can take from seconds to minutes depending on the networks involved. Users of the other locations notice nothing. The weak point is a location that fails but keeps announcing the prefix: its users are sent to a dead site. Operators therefore tie the announcement to health checks, so a broken site withdraws its route.
Anycast and TCP Connections
DNS over UDP is a natural fit: one query, one answer. A TCP connection, such as an HTTPS session, needs every packet to reach the same server. Routes usually stay stable for the life of a connection, so this works in practice, but a route change in the middle can send packets to another location that knows nothing of the session, and the connection resets. Long-lived connections are where anycast designs need the most care.
Why traceroute Differs by Location
Run traceroute to an anycast address from two places and you get two different paths that end at two different servers, with very different times. Both are correct. To see which location you reach, compare the last hops, or look for a location code in the reverse DNS names of the final routers. A looking glass lets you run the same test from another network, and traceroute and mtr explains how to read the output.
Can You Run Anycast Yourself?
Anycast needs your own IP prefix and the right to announce it with BGP from several locations. In practice that means an autonomous system number, a prefix of at least /24 for IPv4 or /48 for IPv6, since many networks do not accept smaller ones, and upstream providers in each location who accept your BGP session. For most projects the practical choice is to use anycast through a DNS host or a CDN rather than to build it.