Anycast Explained: DNS, CDN, and Multi-Site BGP on VDS
Anycast is a routing technique where the same IP prefix is announced via BGP from several locations, and each router on the internet sends traffic to the topologically closest one. You can build it yourself with your own ASN, your own IP block, and a BGP-capable VDS in each location, and it is the standard way to run resilient DNS and CDN edges.
This guide covers how anycast works, why it fits DNS better than most protocols, how CDNs use it, and how to announce one prefix from multiple VDS instances with health-checked withdrawal.
How Anycast Works
With unicast, one IP address lives in one place. With anycast, many servers in different places share the same address, and BGP decides who gets which client.
Each site announces the same prefix, for example 192.0.2.0/24, with the same origin ASN (AS64496 in our examples). Every network on the internet picks its best path using normal BGP rules: local preference, AS-path length, origin, MED, and so on. A client in Frankfurt usually ends up at the Frankfurt site, and a client in California at the California site. This is not geographic. It is "closest in BGP terms," which is usually, but not always, close in latency.
Key consequences:
- Failover is automatic if a site withdraws its route. Traffic converges to the next-best site.
- Load is spread by where clients sit in the routing topology, not by a load balancer.
- Attack traffic is spread too, because each bot reaches its nearest site. This helps absorb volume but does not replace filtering.
Why Anycast Fits DNS
DNS over UDP is mostly one query and one response. There is no long-lived connection to break if the best path changes in the middle. That makes it nearly ideal for anycast.
Practical benefits for authoritative DNS:
- Lower query latency because resolvers reach a nearby site.
- Resilience: if one site dies or is under attack, others keep answering.
- A single pair of NS addresses can cover the world.
Design tips:
- Announce at least two separate prefixes if you can, and put different NS records in different prefixes. That way a routing mistake in one prefix does not take down every nameserver.
- Keep zone data identical on every node. Use primary/secondary with AXFR/IXFR and NOTIFY, or a database-backed setup. Monitor the SOA serial on every node.
- Large responses and TCP fallback still work: DNS over TCP is short-lived and generally fine. See our DNS records guide for record basics if you are new to NS design.
How CDNs Use Anycast
CDNs use anycast in two common ways:
- Anycast IPs for edge nodes. Users connect to the same IP everywhere and land on the nearest edge. Simple, and failover is fast.
- Anycast for DNS only, unicast for HTTP. DNS answers steer users to a specific unicast edge IP based on location or load. This avoids TCP session issues entirely but depends on DNS TTLs for failover.
Many operators run a hybrid. Anycast HTTP works well for short requests and QUIC/HTTP/3 with connection migration, but long downloads, WebSockets, and stateful sessions are more sensitive to path changes. If routing shifts mid-connection, the packets land on a different server that knows nothing about the TCP session, and the client sees a reset.
What You Need Before Building It
- Your own ASN and an IP block. For IPv4, the smallest prefix generally accepted in the global table is a /24. For IPv6, a /48.
- A RPKI ROA for the prefix and origin ASN, plus correct IRR route objects. Without them, many networks will drop your announcement.
- A BGP session at each site. On a BGP-enabled VDS you announce your prefix from the VDS over a BGP session, and ownership of the ASN and prefix is validated against RPKI ROAs and RIR/IRR records.
- A health-check mechanism that stops announcing when the local service is broken.
If you need the basics first, see our earlier guides on BIRD 2 configuration, RPKI/ROA, and IRR route objects.
Step-by-Step: One Prefix from Multiple VDS Instances
The example below uses documentation addresses. Replace them with your real prefix, neighbor, and ASN.
1. Put the service address on the loopback
Each node must accept traffic for the anycast address locally:
ip addr add 192.0.2.53/32 dev lo
Make it persistent with your distribution's network config (netplan, systemd-networkd, or an interface file). Bind your DNS or web daemon to this address.
2. Configure BIRD 2 to announce the prefix
The same config goes on every site, except for the neighbor address and any per-site policy:
router id 198.51.100.10;
protocol device { }
protocol static anycast4 {
ipv4;
route 192.0.2.0/24 blackhole;
}
filter export_anycast {
if net = 192.0.2.0/24 then accept;
reject;
}
protocol bgp upstream {
local as 64496;
neighbor 198.51.100.1 as 64511;
ipv4 {
import none; # or accept only what you need
export filter export_anycast;
};
}
The blackhole static route only exists so BIRD has something to export. Actual delivery happens through the /32 on lo. Using import none is fine if you only need to announce. If you want to receive routes, see our full table versus default route guide.
3. Add a health check that withdraws the route
This is the most important part. An anycast node that is up in BGP but broken at the application layer is a black hole for every client that routes to it.
#!/bin/sh
# /usr/local/bin/anycast-check.sh
if dig +time=1 +tries=1 @192.0.2.53 example.com SOA >/dev/null 2>&1; then
birdc enable anycast4 >/dev/null
else
birdc disable anycast4 >/dev/null
fi
Run it every few seconds from a systemd timer. Add hysteresis, for example require three failures before disabling and three successes before enabling, so a flapping service does not cause route flapping. Some operators prefer ExaBGP or a dedicated health-check daemon, which does the same thing.
4. Verify propagation
After the sessions come up:
birdc show route export upstream
birdc show protocols all upstream
Then check from outside. A public looking glass lets you see how the prefix is visible and which path it takes. You can use the Nixcon looking glass to run BGP route, ping, and traceroute queries and confirm your prefix is reachable and which site answers.
For the VDS side, Nixcon's BGP-enabled VDS lets you announce your own ASN and IP block from a BGP session on the server, with options for announcing without DDoS protection, with DDoS protection, and with a full routing table or only Nixcon routes.
Controlling Where Traffic Goes
Pure shortest-AS-path routing can give uneven results. A site with many peers may attract traffic from far away. You can tune this:
- AS-path prepending at a site to make it less attractive.
- BGP communities offered by your upstream to adjust local preference or limit propagation by region.
- Different announcements per site, such as announcing a more specific route from only one site for testing.
Change one thing at a time and re-measure from several vantage points.
Common Mistakes and Fixes
| Problem | Cause | Fix |
|---|---|---|
| Route is announced but nobody gets answers | Service down, BGP still up | Add health check that withdraws the route |
| Users flip between sites and see TCP resets | ECMP or path changes in the middle of a session | Use anycast for short or stateless traffic; use unicast for sessions |
| Prefix not visible in some regions | Missing or wrong ROA, no IRR object, too specific prefix | Create ROA with correct origin and max-length; use /24 or shorter |
| One site gets almost all the traffic | Path length or peering imbalance | Prepend or use communities at the busy site |
| Zones differ between nodes | Replication broken | Monitor SOA serial on all nodes, alert on drift |
| Cannot reach the server for management | Management is on the anycast IP | Keep a separate unicast address for SSH and monitoring |
Also remember the MTU: if you tunnel or use GRE for protected transport, adjust MSS accordingly.
Anycast and DDoS
Anycast spreads volumetric attacks across sites, which helps, but each site still has to survive its share, and application-layer floods still need filtering. If you announce through a protected path, combine anycast with upstream filtering. Nixcon's DDoS protection and firewall service is designed for this kind of setup, and flow-based monitoring helps you see which site is receiving an attack.
Operational Checklist
- ROA and IRR objects match every site's origin ASN.
- Identical service configuration and data on all nodes.
- Health check with hysteresis on every node.
- Separate unicast management addresses.
- Monitoring from outside, not only from inside each site.
- A tested procedure to drain a site:
birdc disable anycast4, wait for convergence, then do maintenance.
Start with two sites, run DNS first, measure catchment with probes and the looking glass, and only then move on to HTTP.
Frequently asked questions
What is anycast in simple terms?
Anycast is when several servers in different locations share the same IP address, announced via BGP from each location. Routers send each client to the closest announcing site according to BGP path selection.
Do I need my own ASN and IP block for anycast?
Yes, for a true anycast deployment you need your own ASN and a prefix, generally at least a /24 for IPv4 or a /48 for IPv6, so the same prefix can be announced from multiple sites.
Is anycast safe for TCP and HTTP traffic?
It can be, but routing changes can move a client to a different site mid-connection and reset it. Short requests and HTTP/3 tolerate this better than long downloads or WebSockets, so many operators use anycast for DNS and short-lived traffic and unicast for stateful sessions.
What happens if one anycast site fails?
If the site withdraws its route, BGP converges and traffic moves to the next-best site. You need a health check that withdraws the announcement when the service fails, because BGP alone does not know the application is broken.