System Design: An Introduction to DNS Load Balancing
Learn how the Domain Name System (DNS) can be used as a simple, effective, and cost-efficient method for distributing traffic across multiple servers.
System Design: An Introduction to DNS Load Balancing
When you scale a web service, a single server is rarely enough. You need multiple servers to handle the load and ensure high availability. But with multiple servers, a new question arises: when a user visits yourapp.com, how do you decide which server they should connect to?
This is the job of a load balancer. While dedicated hardware and software load balancers (like HAProxy or Nginx) are common, one of the simplest and most fundamental methods of load balancing happens at a layer you might not expect: the Domain Name System (DNS).
What is DNS? A Quick Refresher
DNS is the phonebook of the internet. Its primary job is to translate human-friendly domain names (like www.google.com) into machine-friendly IP addresses (like 142.250.184.196). When you type a URL into your browser, a DNS resolver queries a series of DNS servers to find the correct IP address for that domain.
The key record type for this is the A record, which maps a domain name to an IPv4 address.
How DNS Load Balancing Works
DNS load balancing leverages a simple but powerful feature: a single domain name can be associated with multiple IP addresses.
When a DNS server receives a query for a domain that has multiple A records, it can return one or all of them. The most common method is Round Robin DNS.
Round Robin DNS
In this configuration, the DNS server responds to queries with a list of IP addresses, rotating the order of the addresses in each response.
Example:
- Your application runs on three servers with IPs:
1.1.1.1,2.2.2.2,3.3.3.3. - You configure three A records for
yourapp.compointing to these IPs.
- First query for
yourapp.com: The DNS server returns[1.1.1.1, 2.2.2.2, 3.3.3.3]. The client's browser typically picks the first one (1.1.1.1). - Second query: The DNS server rotates the list and returns
[2.2.2.2, 3.3.3.3, 1.1.1.1]. The next user is directed to2.2.2.2. - Third query: The DNS server returns
[3.3.3.3, 1.1.1.1, 2.2.2.2]. The third user is directed to3.3.3.3. - Fourth query: The cycle repeats.
This effectively distributes the traffic among the three servers in a simple, rotating fashion.
Advantages of DNS Load Balancing
- Simplicity: It's incredibly easy to set up. You just need to add multiple A records in your DNS configuration. No special software or hardware is required.
- Cost-Effective: It's often free or included with your domain registrar's service.
- Global Distribution: It works naturally for distributing traffic across geographically separate data centers, forming the basis of Global Server Load Balancing (GSLB).
- Resilience: If one server goes down, clients can potentially try other IP addresses from the list, providing a basic level of failover.
The Major Drawbacks: Why DNS Isn't a Perfect Solution
Despite its simplicity, DNS load balancing has significant limitations that make it unsuitable as a standalone solution for many modern applications.
1. The Caching Problem (Time-to-Live - TTL)
DNS responses are heavily cached at multiple levels (in your browser, your operating system, your ISP's resolver) to reduce latency and load on DNS servers. The duration for which a response is cached is determined by its Time-to-Live (TTL) value.
- High TTL (e.g., 24 hours): If you need to take a server out of rotation (for maintenance or because it failed), it could take up to 24 hours for the cached DNS records to expire across the internet. During this time, users will still be directed to the dead server.
- Low TTL (e.g., 60 seconds): This makes changes propagate faster, but it increases the number of DNS queries, putting more load on your DNS provider and potentially increasing costs and latency for users.
2. It's Not "Smart" - No Health Checks
A DNS server has no idea about the health or current load of your application servers. It just blindly hands out IP addresses.
- If a server crashes, DNS will keep sending traffic to it until the record is manually removed and the TTL expires.
- If one server is overloaded and responding slowly, DNS will continue to send new users its way, making the problem worse.
Modern DNS providers offer "smart" DNS with health checks, but this is an advanced feature and moves beyond simple round-robin.
3. Uneven Traffic Distribution
DNS load balancing doesn't guarantee an even distribution of traffic. Caching at large ISPs can cause a huge number of users behind that ISP to get the same IP address, leading to one of your servers getting a disproportionate amount of traffic.
Go Example: DNS Lookup
You can't implement a DNS server's round-robin logic in your application, but you can see how DNS resolution works using Go's net package. This demonstrates how a client would see the multiple IP addresses.
package main
import (
"fmt"
"log"
"net"
)
func main() {
// We'll look up a domain known to use DNS load balancing, like google.com.
domain := "google.com"
fmt.Printf("Looking up IP addresses for %s...\n", domain)
// net.LookupIP performs a DNS lookup and returns all IPs for the host.
ips, err := net.LookupIP(domain)
if err != nil {
log.Fatalf("Could not get IPs: %v\n", err)
}
if len(ips) == 0 {
fmt.Println("No IP addresses found.")
return
}
fmt.Printf("Found %d IP addresses:\n", len(ips))
for _, ip := range ips {
// We only care about IPv4 for this example
if ip.To4() != nil {
fmt.Println(ip)
}
}
fmt.Println("\nNote: Running this multiple times may show different IPs or a different order due to DNS load balancing.")
}
Running this program multiple times might show you a different list or order of IPs, demonstrating DNS load balancing in action.
Conclusion
DNS load balancing is a powerful and simple tool for distributing traffic, especially across geographically diverse locations. Its main strengths are its simplicity and low cost. However, its effectiveness is severely limited by the issues of DNS caching and its inability to perform health checks.
In a modern system design, DNS load balancing is rarely used on its own for application servers. Instead, it's typically used as the first layer of a multi-layered load balancing strategy:
- DNS Load Balancing directs users to the nearest data center.
- A Hardware/Software Load Balancer within that data center then intelligently distributes the traffic among a pool of healthy application servers.