Practice this topic in a realistic system design interview
Anycast routing is a technique that allows servers in different parts of the world to share the same IP address.
In this chapter, we will look at how anycast works with internet routing, what happens when a location fails, where it's used, its limitations, and how it compares with DNS load balancing.
Suppose you run a service in three locations: Tokyo, Frankfurt, and Virginia. Ideally, users in Japan should connect to Tokyo, users in Germany to Frankfurt, and users in New York to Virginia.
One way to do this is through DNS-based routing. Your DNS provider uses the location associated with a DNS query to return the IP address of a nearby region.
This works, but it has a couple of limitations.
First, DNS responses are cached. If the Tokyo region goes down, some users might keep connecting to its old IP address until their cached response expires.
Second, DNS often sees the location of the user's recursive resolver rather than the user. If that resolver is far away, the user might end up connecting to a region that isn't actually the closest.
Anycast solves this problem differently. Instead of giving each location a separate IP address and relying on DNS to choose between them, all three locations advertise the same IP address. The internet's routing system then determines where each user's traffic goes.
To understand how this works, let's first compare anycast with other ways of routing IP packets.
Most IP traffic on the internet uses unicast. With unicast, an IP address identifies a particular network endpoint, and packets sent to that address are routed toward it.
Broadcast works differently. It sends a packet to every device on a local network. For example, when a device first joins a network, it might use broadcast to discover a DHCP server.
Then there's multicast, which delivers packets to a group of receivers that have joined a particular group. It's used in some video distribution systems and financial market data networks.
Anycast takes a different approach. Multiple servers in different locations share the same IP address, but each packet is routed to just one of those locations, based on the network's routing decisions.
Think of an emergency number like 911. Everyone dials the same number, but the phone network directs each call to the emergency center responsible for that area. You don't need to know the number of your local emergency center.
Anycast works in a similar way. Users connect to the same IP address, but their traffic can reach different locations depending on where they're connecting from and how the network is routed.
| Model | Delivery Pattern | Common Use |
|---|---|---|
| Unicast | One address to one endpoint | Most internet traffic: web, APIs, databases |
| Broadcast | One sender to every device on a local network | DHCP discovery |
| Multicast | One sender to receivers that joined a group | Video distribution, market data |
| Anycast | One address shared by many locations, each packet reaches one | DNS, CDN edges, DDoS protection, global entry points |
So how does the internet decide which location receives the traffic? That's where BGP comes in.