SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
  • What are Load Balancers?
  • Load Balancing Algorithms
  • DNS Load Balancing
  • Anycast Routing
  • Layer 4 vs Layer 7 Load Balancers
  • Health Checks
  • Sticky Sessions
  • Global Load Balancing
  • Nginx and HAProxy
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Foundations/Load Balancing

DNS Load Balancing

Share traffic by giving different IP addresses to different users.

โฑ 10 min readIntermediateHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

DNS load balancing shares traffic by giving different IP addresses for the same domain name. Each user gets a different answer, so users spread across servers.

  • Very simple and cheap. No extra device is needed.
  • It works before the traffic even reaches your servers.
  • But answers are cached (TTL), so it is uneven and slow to react when a server dies.
  • Big systems use it to pick a data centre or region, and use real load balancers inside.

02Concept: What is it?

DNS turns a name like shop.example.com into an IP address. In DNS load balancing, the same name has many IP addresses. The DNS server picks which to return, or returns them in a rotating order.

One name, many IPs
๐Ÿง‘User 1
๐Ÿ‘ฉUser 2
๐Ÿง”User 3
โ†’โ†“shop.example.com?
๐Ÿ“–DNS serverrotates the answers
โ†’โ†“IP 1, IP 2, IP 3
๐Ÿ–ฅ๏ธ203.0.113.10
๐Ÿ–ฅ๏ธ203.0.113.11
๐Ÿ–ฅ๏ธ203.0.113.12
Three different users ask for the same name and get three different servers.

Main ways to do it

๐Ÿ”„Round-robin DNS
Many A records for one name. The order rotates on each answer.
โš–๏ธWeighted DNS
Some IPs are returned more often, because those servers are stronger.
๐ŸŒGeo / latency DNS
The answer depends on where the user is. A user in India gets the Mumbai IP.

03Real-Life Example

๐ŸตThe phone book with many numbers
A pizza shop has 3 branches but only one name. The phone book lists 3 numbers. Each time someone looks it up, the book shows the numbers in a different order. People call the first number they see, so calls spread over all 3 branches.

There is a catch. People write the number on a sticky note and keep calling it. If a branch closes, those people keep calling the closed branch until they throw away the note. That sticky note is the DNS cache, and its life time is the TTL.

Pizza shop
DNS
Shop name
Domain name (shop.example.com)
Phone book
DNS server
3 branch numbers
3 IP addresses (A records)
Numbers shown in a rotating order
Round-robin DNS
Sticky note
DNS cache in the resolver, OS or browser
How long the note is valid
TTL
A branch closes
A server fails. Cached users still call it
One lookup, step by step
  1. 1
    You type shop.example.com
    Browser checks its own cache first
  2. 2
    Your ISP or company resolver is asked
    It checks its cache. Found? Stop here.
  3. 3
    Authoritative DNS server is asked
    It owns the records for example.com
  4. 4
    It answers with one IP (next in rotation)
    Also sends a TTL, e.g. 60 seconds
  5. 5
    Everyone along the way caches the answer
    For TTL seconds, no new questions
  6. 6
    Your browser connects to that IP

04Why Do We Need It?

๐Ÿ’ธAlmost free
It uses DNS you already have. No device to buy.
๐ŸงฑEasy to start
Add more IP records and you are done.
๐ŸŒPick a region
Send users to the nearest data centre (geo DNS).
๐ŸขSpread load balancers
Even one load balancer can be too small. DNS can share traffic across many LBs.
๐Ÿ›ŸDisaster failover
If a whole data centre dies, change DNS to point to another one.
DNS balancing is the first and cheapest layer of balancing. It points users to the right place. A real load balancer then does fine-grained work.

05Where Do We Use It?

๐ŸŒGlobal traffic management
Pick the region closest to the user.
โš–๏ธIn front of load balancers
Many LBs share one domain name.
๐ŸขMulti-data-centre failover
Move users away from a broken site.
๐ŸŽฌCDNs
Return the nearest edge server for a name.
โ˜ธ๏ธService discovery
Internal names return the IPs of service copies.
๐ŸงชGradual migration
Weighted DNS moves 10% of users to a new data centre.

06Types / Parts / How It Works

1. Round-robin DNS

The DNS zone has many A records for one name. Each answer lists them in a different order.

shop.example.com.   60  IN  A   203.0.113.10
shop.example.com.   60  IN  A   203.0.113.11
shop.example.com.   60  IN  A   203.0.113.12
;                   ^ TTL in seconds

2. Weighted DNS

Managed DNS services (like AWS Route 53 or Cloudflare) let you give each record a weight. A record with weight 70 is returned 70% of the time.

๐Ÿ–ฅ๏ธNew siteweight 10
๐Ÿ–ฅ๏ธOld siteweight 90

3. Geo and latency based DNS

The DNS server looks at where the question came from and returns the nearest or fastest server.

Geo DNS
๐Ÿ‡ฎ๐Ÿ‡ณUser in India
๐Ÿ‡บ๐Ÿ‡ธUser in USA
๐Ÿ‡ฉ๐Ÿ‡ชUser in Germany
โ†’โ†“
๐Ÿ“–Geo DNS
โ†’โ†“
๐ŸขMumbai DC
๐ŸขVirginia DC
๐ŸขFrankfurt DC

4. DNS with health checks (failover)

Managed DNS can check each server. If one fails, it stops returning that IP. This is better, but the TTL still delays the effect.

The big limits

๐Ÿ—‚๏ธCaching (TTL)
Users keep old answers. A dead server is still used until the TTL ends.
๐ŸขBig resolvers
A whole office or ISP shares one resolver, so thousands of users all go to the same IP.
๐Ÿ‘€No view of load
DNS does not know how busy a server is, or which request is heavy.
๐Ÿ™ˆClients choose
Some clients ignore the order and pick the first IP, or retry badly.
VisualizerDNS rotation, TTL and a dead serverPress Play. Look at the Office. Then kill a server and change the TTL.
DNS health checks
๐Ÿ“–DNS: shop.example.comgives the next IP in turn ยท 0 lookups so far
โ†”only when the saved answer has expired
๐ŸขOffice6 req/s, shared resolver
nothing saved yet
๐Ÿ Home 11 req/s
nothing saved yet
๐Ÿ“ฑPhone1 req/s
nothing saved yet
๐Ÿ Home 22 req/s
nothing saved yet
โ†“
๐Ÿ–ฅ๏ธ203.0.113.100 requests
๐Ÿ–ฅ๏ธ203.0.113.110 requests
๐Ÿ–ฅ๏ธ203.0.113.120 requests
...100
...110
...120
DNS lookups
0
Failed requests
0
Users on dead server
0
Look at the Office: it uses one saved answer for all its heavy traffic, so one server gets much more load. Try a very low TTL, then a high one.

07How to Use It / Steps / Implementation

  1. 1
    Pick the goal
    Spread across servers? Pick the nearest region? Fail over to another data centre?
  2. 2
    Add several records
    Create A (IPv4) or AAAA (IPv6) records for the same name.
  3. 3
    Choose a TTL
    Short TTL (30โ€“60 s) reacts fast but causes more DNS lookups. Long TTL (hours) is cheap but slow to change.
  4. 4
    Turn on health checks
    Use a managed DNS that removes a failed IP from the answers.
  5. 5
    Prefer sending to load balancers
    Point records at LB addresses, not at single app servers. The LB does the smart work.
  6. 6
    Lower the TTL before a planned change
    Do it a day earlier, so old answers expire in time.
  7. 7
    Test from many places
    Use tools like dig or nslookup from different networks.
Typical two-level setup
๐Ÿง‘User
โ†’โ†“
๐Ÿ“–DNSpicks a region
โ†’โ†“
โš–๏ธLB in Mumbai
โš–๏ธLB in Frankfurt
โ†’โ†“
๐Ÿ–ฅ๏ธ
๐Ÿ–ฅ๏ธ
Coarse choice (DNS) Fine choice (LB)
DNS picks the region. A load balancer in that region picks the server.
$ dig +short shop.example.com
203.0.113.11
203.0.113.12
203.0.113.10      # run again: the order rotates

08Key Terms to Know

TermMeaning in easy words
DNSThe system that turns a domain name into an IP address
A / AAAA recordA DNS entry that maps a name to an IPv4 / IPv6 address
TTL (time to live)How many seconds an answer may be cached
ResolverThe DNS server of your ISP, company or Google (8.8.8.8) that asks on your behalf
Authoritative serverThe DNS server that owns the real records for a domain
Round-robin DNSRotating the order of the IPs on each answer
Geo DNSDifferent answers by the user location
Health-checked DNSDNS that stops returning IPs of failed servers
DNS failoverChanging the answers to point to a backup site
PropagationThe time it takes for a DNS change to reach every cache

09Pros, Cons and Trade-offs

โœ… Pros
  • Simple and cheap
  • Works at global scale
  • Can pick the nearest region
  • No extra hardware in the data path
  • Good first layer for failover
โš ๏ธ Cons
  • Caching makes failover slow
  • Uneven load (big resolvers, caches)
  • DNS cannot see server load
  • Clients may not follow the order
  • Not good for fine control, like per request balancing

Trade-offs

ChoiceWe gainWe lose
Short TTLFast failoverMore DNS queries, a bit slower first load
Long TTLFewer lookups, steadySlow to react to failure
Round robin onlyVery simpleNo health awareness
Managed DNS + health checksAuto removal of dead IPsCost, still bound by TTL
DNS balancing
Coarse, before the traffic
  • Cheap, global
  • Slow to react
  • No view of load
Load balancer
Fine, in the traffic path
  • Per request decisions
  • Instant health checks
  • Needs a device or service

10Common Problems and Mistakes

Common mistakes

  1. Expecting instant failover. Cached answers keep users on a dead server until the TTL ends.
  2. Pointing DNS straight at fragile single servers. Use highly available load balancers instead.
  3. A very high TTL. You cannot move traffic quickly in an emergency.
  4. A very low TTL everywhere. The DNS provider gets heavy traffic and users wait for more lookups.
  5. Assuming an even split. A few big resolvers can send most traffic to one IP.
  6. Changing records without lowering the TTL first. A planned move takes hours.
  7. Forgetting that some software ignores TTL. Some apps cache DNS for a long time.
โš ๏ธCareful
If you need to react in seconds, put a load balancer or anycast in front. DNS is not made for that.

11Real-World Examples

โ˜๏ธAWS Route 53
Weighted, latency, geo and failover routing with health checks.
๐Ÿ›ก๏ธCloudflare
DNS plus global load balancing and health checks.
๐ŸŒGoogle and Azure
Cloud DNS and Traffic Manager route users to regions.
๐ŸŽฌCDNs
Use DNS answers to point you to a nearby edge server.
๐ŸŒPublic websites
Many big sites list several IPs for their name. Try dig on your favourite.
๐Ÿ“Note
Each company has its own mix of DNS, anycast and load balancers. These are the general patterns.

12Related Topics to Read Next

DNSโ†’

How DNS works, step by step.

What are Load Balancers?โ†’

The finer tool inside each region.

Anycast Routingโ†’

Another way to send users to the nearest site, with fast failover.

Load Balancing Algorithmsโ†’

Round robin and weights are also used by DNS.

Availabilityโ†’

DNS failover is one way to keep a site up.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”What: one name, many IPs. DNS gives different ones to different users.
  • โœ”Kinds: round robin, weighted, geo / latency, health-checked.
  • โœ”Good for: picking a region or data centre, spreading across many LBs.
  • โœ”Weak at: fast failover and exact balance, because of TTL caching.
  • โœ”TTL: short = fast change, more lookups. Long = slow change, fewer lookups.
  • โœ”Best practice: DNS picks the region, a load balancer picks the server.
  • โœ”One line: โ€œDNS load balancing shares traffic by handing out different IPs, but cached answers make it slow to react.โ€
โ† Previous
Load Balancing Algorithms
Next โ†’
Anycast Routing