DNS load balancing spreads traffic by answering the same name with different IP addresses. A rider asks for api.bustrack.example, the authoritative DNS server returns one or several server addresses, and the rider connects to one of them. It needs no extra hardware and works across regions, but every answer is cached by resolvers you do not control, so it is coarse: slow to react, uneven in share and unable to see the health of a server unless you add health checks.
We continue with BusTrack, the Hyderabad bus tracking app from Load Balancing: Introduction. Its servers are hyd-api-1 to hyd-api-3. Six runnable programs in Java, with Python and C++ versions, measure what DNS really does: caching resolvers, TTL and failover, weights, GeoDNS, health checks, and adding or draining a server.
Key takeaways
With six caching resolvers and a 300 second TTL, the busiest of three servers received 172% of its fair share. With TTL 0 the share was exactly even, but the authoritative server answered 1,000 queries per second.
When one server crashed, a 300 second TTL meant 188,570 failed requests. A 5 second TTL cut that to 32,160 but cost 60 queries per second.
Weights of 5:3:2 missed their target by 4.2 points with 6 resolvers and by 0.5 points with 3,000.
GeoDNS lowered the average round trip from 30.3 ms to 13.7 ms, but only when the resolver passed the client subnet. A public resolver in Mumbai made it 17.5 ms.
Probing every 10 seconds and removing a record after one bad probe wrongly removed a healthy server 20 times in an hour. Three bad probes in a row removed it zero times.
A new server needed about 300 seconds to reach its share with a 300 second TTL. A load balancer gives it 25% at once.
When 15% of resolvers ignored the TTL, 6.0% of traffic still reached a drained server 60 seconds later.
1. What Is DNS Load Balancing?
The Domain Name System turns a name into addresses, as described in the DNS lesson. If the name has several A records, the authoritative server can return all of them, rotate their order, or return a different subset to each asker. That is the whole trick: the choice is made once, when the name is resolved, not for each request.
The rider never talks to the authoritative server directly; a resolver does it on the rider's behalf.
This has three consequences that shape everything below.
The decision is cached. The answer is reused until its TTL (time to live) ends, by the resolver, by the operating system and sometimes by the app.
The decision is made per resolver, not per rider. One ISP resolver can stand for thousands of riders.
The authoritative server cannot see load. It knows only what you tell it: health checks, weights and the asker's location.
2. Why Caching Makes Round Robin DNS Uneven
The simplest setup lists three records and rotates them. If every rider resolved independently this would be a perfect round robin. In practice riders sit behind a few large resolvers. The first program has 10,000 riders behind six resolvers of very different size, each rider sending one request every 10 seconds, and rotates the answer on every authoritative query.
One cached answer is shared by everyone behind the resolver.
Output
api.bustrack.example has 3 A records: hyd-api-1, hyd-api-2, hyd-api-3
10000 riders behind 6 resolvers (4000, 2500, 1500, 1000, 700, 300), 1800 s, answers rotate round robin
TTL(s) authoritative queries/s hyd-api-1 hyd-api-2 hyd-api-3 busiest server vs fair share
300 0.0 22.7% 57.4% 19.8% 172%
60 0.1 34.9% 42.5% 22.4% 127%
5 1.2 22.0% 49.9% 28.0% 149%
0 1000.0 33.3% 33.3% 33.3% 100%
With a 300 second TTL the three servers received 22.7%, 57.4% and 19.8% of the requests. The busiest was 172% of a fair share, and the authoritative server saw almost no traffic.
A 60 second TTL helped a little (127%) and 5 seconds gave 149%. More refreshes do not guarantee balance, because each refresh by a big resolver still moves thousands of riders at once.
TTL 0 made every request ask again, so the shares were exactly 33.3% each. The price was 1,000 queries per second, and many resolvers ignore a TTL of 0 anyway.
Balance and query load pull in opposite directions.
3. TTL, Failover and the Cost of Waiting
The TTL is also your failover speed. If a server dies, DNS cannot tell riders until their caches expire. The second program has 300 resolvers sending 3,000 requests per second. At t = 100 s hyd-api-2 crashes, and a health check removes its record at t = 130 s.
Failover is detection time plus the time for every cache to expire.
Output
hyd-api-2 crashes at t=100 s. The health check removes its DNS record at t=130 s.
300 resolvers, 10 requests per second each (3000 requests/s in total), 900 s
TTL(s) failed requests last failure at seconds with >1% failing authoritative queries/s
300 188570 427 s 319 1.0
60 60330 188 s 88 5.0
30 45210 158 s 59 10.0
5 32160 133 s 34 60.0
TTL 300 s: 188,570 requests failed and the last failure was at 427 s. More than 1% of requests failed for 319 seconds.
TTL 60 s: 60,330 failed requests and the last one at 188 s.
TTL 5 s: 32,160 failed requests, nearly all of them in the 30 seconds before the health check removed the record.
The cost of the short TTL is 60 authoritative queries per second against 1 for 300 s. A short TTL helps only after detection; it cannot fix a slow health check.
A short TTL buys faster failover with more query traffic.
4. Weighted DNS
If servers differ in size you can return the big server's record more often. Say hyd-api-1 has weight 5, hyd-api-2 weight 3 and hyd-api-3 weight 2, so the target is 50%, 30% and 20%. The authoritative server draws a weighted random answer for every resolver that asks.
Weights only average out when there are many independent resolvers.
The third program has 60,000 riders and a 60 second TTL. The resolver sizes follow a skewed pattern: the biggest resolver is twice the size of the second, three times the third and so on. It runs for 30 minutes.
Output
Weighted DNS: hyd-api-1 weight 5, hyd-api-2 weight 3, hyd-api-3 weight 2 (target 50% / 30% / 20%)
60000 riders, TTL 60 s, 30 minutes. Each resolver caches one weighted random answer.
resolvers hyd-api-1 hyd-api-2 hyd-api-3 worst miss from target
6 (skewed) 51.8% 25.8% 22.3% 4.2 points
60 (skewed) 47.5% 32.4% 19.9% 2.5 points
600 (skewed) 49.7% 29.4% 20.7% 0.7 points
3000 (skewed) 49.9% 30.5% 19.5% 0.5 points
600 (all the same size) 49.7% 30.0% 20.1% 0.3 points
With 6 skewed resolvers the shares were 51.8%, 25.8% and 22.3%, a miss of 4.2 points from the target.
The miss shrank to 2.5 points with 60 resolvers, 0.7 with 600 and 0.5 with 3,000.
Equal-sized resolvers needed only 600 to reach a 0.3 point miss. The skew in resolver size, not the weights, is the real source of error.
Treat DNS weights as a rough guide, good for sending a 10% canary to a new version or draining a region slowly. For an exact 5:3:2 split use a load balancer, as in Load Balancing Algorithms.
5. GeoDNS and Latency-Based Answers
BusTrack runs in three regions: Hyderabad, Mumbai and Delhi. GeoDNS answers with the region nearest to the asker. The catch is who the authoritative server believes is asking. It sees the resolver's address, not the rider's. If a rider in Chennai uses a public resolver located in Mumbai, DNS thinks the rider is in Mumbai. The EDNS Client Subnet (ECS) extension lets a resolver pass a truncated client address so that the answer can be based on the rider.
The resolver's location is not the rider's location.
The fourth program has 100,000 requests from five cities (Hyderabad 40%, Chennai 20%, Bengaluru 15%, Mumbai 15%, Delhi 10%) and a fixed round trip time between each city and each region. 25% of riders use a public resolver that the authoritative server sees in Mumbai.
Output
BusTrack has 3 regions: Hyderabad, Mumbai, Delhi. Riders: Hyderabad 40%, Chennai 20%, Bengaluru 15%, Mumbai 15%, Delhi 10%.
25% of riders use a public DNS resolver that the authoritative server sees in Mumbai. 100000 requests.
policy avg rtt p95 rtt over 30 ms failed
round robin over 3 regions 30.3 ms 53 ms 56.4% 0.0%
GeoDNS by resolver location 17.5 ms 36 ms 14.4% 0.0%
GeoDNS with client subnet (ECS) 13.7 ms 25 ms 0.0% 0.0%
ECS, Mumbai down, health check 17.2 ms 33 ms 11.8% 0.0%
ECS, Mumbai down, no health check 14.7 ms 25 ms 0.0% 14.7%
Plain round robin over the regions averaged 30.3 ms, and 56.4% of requests took more than 30 ms.
GeoDNS by resolver location cut the average to 17.5 ms, but 14.4% of requests were still slow because a quarter of the riders were mapped to Mumbai.
With ECS the average was 13.7 ms, p95 25 ms and none of the requests were slow.
When the Mumbai region failed, the health-checked setup rerouted its riders to the next nearest region: average 17.2 ms and no failures. Without a health check, 14.7% of requests failed, although the average of the successful ones looked fine at 14.7 ms. Always count the failures next to the latency.
6. Health Checks for DNS Records
A DNS record has no idea whether the server behind it works. The DNS provider probes your servers and removes a record after N bad probes in a row, then adds it back after N good probes in a row. The fifth program has hyd-api-2 really down from 300 s to 420 s and hyd-api-3 healthy but with 5% of its probes timing out, for one hour.
A record leaves DNS after N bad probes in a row and returns after N good ones.
Output
hyd-api-2 is really down from t=300 s to t=420 s. hyd-api-3 is healthy but 5% of its probes time out.
One hour. A server leaves DNS after N bad probes in a row and comes back after N good probes in a row.
probe every bad probes TTL detect api-2 riders all moved probes/hour api-3 wrongly removed minutes api-3 was out
30 s 3 300 60 s 360 s 120 0 0.0
10 s 3 60 20 s 80 s 360 0 0.0
10 s 1 60 0 s 60 s 360 20 3.5
5 s 2 30 5 s 35 s 720 2 0.3
2 s 3 20 4 s 24 s 1800 0 0.0
Probing every 30 seconds with 3 bad probes and a 300 second TTL took 60 seconds to detect the crash, and 360 seconds until every rider had moved.
Probing every 10 seconds with 3 bad probes and a 60 second TTL: detection in 20 seconds and everyone moved after 80 seconds, with zero false removals.
The same probe rate with only 1 bad probe needed 0 seconds to detect, but removed the healthy
hyd-api-320 times in the hour, a total of 3.5 minutes out of the pool.5 seconds with 2 probes still had 2 false removals. 2 seconds with 3 probes moved riders after 24 seconds, at the cost of 1,800 probes per hour for each server.
Failing fast and failing falsely trade against each other. Require more than one bad probe, and probe from more than one location so that a network problem at the probe does not remove your whole fleet.
7. Adding and Draining Servers
DNS changes take effect slowly, and this matters in both directions. The sixth program has two parts. First, hyd-api-4 is added to the three-server pool at t = 0.
New servers wait for caches to expire before they receive any real traffic.
Second, hyd-api-3 is removed for maintenance. Some resolvers do not honour the TTL: they apply their own minimum, often 10 minutes or more. The program compares a world where every resolver obeys a 60 second TTL with worlds where 5% or 15% of resolvers keep answers for 600 seconds.
Some caches outlive the TTL, so draining by DNS needs a long wait.
Output
Part 1: hyd-api-4 is added to a pool of 3 at t=0. Share of requests it receives (a balancer would give 25% at once).
TTL t=0 t=30s t=60s t=120s t=300s t=600s t=900s
300 s 0.0% 4.2% 7.2% 12.5% 28.2% 22.2% 26.7%
60 s 0.0% 17.0% 28.2% 22.2% 27.2% 25.7% 27.7%
Part 2: hyd-api-3 is removed from DNS at t=0 for maintenance. Share of requests that still arrive (a balancer drains to 0).
resolvers t=0 t=30s t=60s t=120s t=300s t=600s t=900s
all obey the 60 s TTL 36.0% 15.5% 0.0% 0.0% 0.0% 0.0% 0.0%
5% keep answers for 600 s 36.0% 16.2% 1.0% 0.7% 0.2% 0.0% 0.0%
15% keep answers for 600 s 36.0% 19.2% 6.0% 5.2% 2.7% 0.0% 0.0%
With TTL 300 s the new server received 4.2% of traffic after 30 s, 12.5% after 120 s and about 28% after 300 s. With TTL 60 s it reached the neighbourhood of its 25% share after 60 s.
When draining with obedient resolvers, no request arrived after 60 s.
With 5% stubborn resolvers, 1.0% of traffic still arrived at 60 s and 0.2% at 300 s.
With 15% stubborn resolvers, 6.0% arrived at 60 s, 2.7% at 300 s and none after 600 s.
Remove the record, wait longer than the longest TTL you have seen in practice (10 minutes is a safe habit), and only then stop the server. Better still, drain with a load balancer, which controls the connection itself. This is the draining idea from Load Balancing: Introduction.
8. DNS in Front of Load Balancers
The best use of DNS is not to name your individual servers but to name your load balancers. DNS chooses the region or the balancer pair. The balancer picks the server, runs fast health checks, drains connections and applies the algorithms from the previous lesson.
DNS picks the region; the balancer picks the server.
That is how most large services are built. DNS is slow but global; a balancer is fast but local. Each covers the other's weakness.
Choosing
A short decision path for DNS and balancers.
| Need | Use | Watch out for |
|---|---|---|
| One region, many servers | A load balancer; DNS names the balancer | Do not publish server addresses |
| Several regions | GeoDNS with ECS plus a balancer in each region | Public resolvers hide the rider |
| Fast failover | Short TTL, 30 to 60 s, with health checks | Query cost and stubborn resolvers |
| Exact shares | A load balancer with weights | DNS weights are only a rough guide |
| Canary of 5 to 10% | Weighted DNS records | Skew in resolver sizes |
| Planned maintenance | Remove the record, then wait 10 minutes | Caches that ignore the TTL |
9. Common Mistakes
Publishing individual server addresses and expecting an even split. Resolver caching decides the split.
Setting a long TTL and expecting quick failover.
Setting a tiny TTL everywhere. The authoritative server then carries every request's lookup and many resolvers ignore it.
Using GeoDNS without ECS and not noticing that public resolvers hide the rider.
Removing a record on one failed probe, which makes a healthy server flap in and out.
Stopping a server the moment its record is removed.
10. Best Practices
Let DNS pick regions and load balancers pick servers.
Use a TTL of 30 to 60 seconds for records that need failover, and longer for records that never change.
Health check every record with at least 2 or 3 bad probes in a row, from more than one location.
Plan for a few minutes of traffic arriving after you remove a record.
Measure failures and latency together when you evaluate GeoDNS.
Keep one stable fallback record, for example a static page in another region.
11. Practice Problems
In the first program, double the size of the largest resolver. Which TTL gives the worst imbalance now?
In the second program, make the health check remove the record at 190 s instead of 130 s. How many requests fail with each TTL?
In the third program, change the weights to 4:4:2. Which resolver counts still miss the target by more than 1 point?
In the fourth program, add a Hyderabad outage instead of a Mumbai one. Which city suffers the most?
In the fifth program, probe every 10 seconds with 2 bad probes. Predict the false removals before you run it.
In the sixth program, raise the stubborn minimum from 600 to 3,600 seconds. How long should you wait before maintenance?
12. Interview Questions
Why is round robin DNS uneven? Answers are cached per resolver and a few resolvers serve many users, so one cached answer moves a large share of traffic.
How does the TTL affect failover? Riders keep using a dead record until their cache expires, so the worst case is detection time plus the TTL.
What does ECS fix in GeoDNS? It passes a truncated client subnet so DNS can see the rider, not only the public resolver.
When would you use weighted DNS? For rough splits such as a canary, not for exact shares.
Why put a load balancer behind DNS? A balancer gives fast health checks, exact weights and connection draining that DNS cannot.
Summary
DNS load balancing chooses once per resolver, and caches make that choice sticky and uneven.
TTL sets failover speed; short TTLs help only after a prompt health check.
Weights and shares are approximate; a few big resolvers distort them.
GeoDNS is only as good as the location DNS can see, so use ECS.
Health checks need several bad probes in a row to avoid flapping.
Use DNS for regions and a load balancer for servers.
Conclusion
DNS is the front door of load balancing: global, cheap and slow. Use it to send riders to the right region and to the right balancer, then let the balancer do the quick work. The next lesson explains how anycast gets traffic to the nearest location without relying on DNS answers at all.
Previous topic: Load Balancing Algorithms. Next topic: Anycast Routing.