Overview
You can use this document to learn how to implement a DNS-based load balancer.
To have DNS-based load balancing, you must configure your DNS records so that different end users are routed to different servers.
With StackPath DNS, you can load balance traffic towards your website, mail server, application, API, or any other kind of publicly accessible network.
How DNS load balancing works
The main idea behind DNS based load balancing is that the client will target the first IP it receives for a given hostname. When resolving a hostname on StackPath DNS, the list of IP addresses will be returned in a different order each time the new client makes a DNS query. The list of IP addresses for a given DNS record is rotated on a round-robin basis. That kind of setup makes different end-users routed to different machines, effectively creating a load balancing configuration at a DNS level.
Advantages of DNS load balancing solution The main advantage of DNS load balancing is the ease of configuration. Essentially, the only thing required is multiple DNS records for the same hostname pointed to different IPs of all the machines serving requests for a given web service. Another advantage is the fact there is no new software to install and no additional server configuration changes to make - the traffic is routed at a DNS level.
Drawbacks of DNS load balancing solution
While DNS based load balancing is fairly easy to set up, it comes with a few inherent drawbacks. DNS itself is not capable of monitoring the servers for potential load issues or any kind of network outages. If one of the servers gets overloaded or becomes inaccessible due to a network outage, it won’t be automatically taken out of the rotation and the end users would still be routed to it since there’s no way for the DNS to know the machine is experiencing issues. Once the issue is discovered, taking the machine in question out of the rotation might take a while since DNS changes are not always instantaneous. That problem can be partially tackled by setting up lower TTL values for the DNS records, but the clients and intermediate DNS resolvers might still have old DNS records cached for extended periods of time.
Example: Setting up DNS load balancing for a web service
In order to implement DNS load balancing for a web service (e.g. a website or an API endpoint), the A records for the hostname in question need to be pointed to all IPs of the machines serving requests for a given web service. For example, if requests for api.mywebsite.com hostname are served by 3 different machines, each having their own IP, three separate A records for api.mywebsite.com need to be set up in StackPath portal, where each A record is pointed to a different IP address of the target machines. Once the DNS changes are propagated, every new end user will be routed to a different IP address and the DNS load balancing setup is complete at that point.
For example, one client will get a response for 1.2.3.4, 1.2.3.5 and 1.2.3.6 IPs and will be routed to the first IP. The second client will get a response for 1.2.3.5, 1.2.3.6 and 1.2.3.4 IPs (in that order) and will be routed to 1.2.3.5 IP address instead. However, it is important to keep in mind that not all operating systems will have the same behavior and some clients might ignore the first
IP in the response they receive and route the request to a second or a third IP address instead. Generally, that shouldn’t be a concern since the majority of end-users will exercise the standard behavior of targeting the first IP in the response.
Example: Setting up DNS load balancing for a mail server
StackPath DNS can also be used for load balancing traffic for a mail server. There are 2 approaches to implementing DNS load balancing for a mail server. The first and most common approach is to assign the same priority (usually a priority of 10) to all MX records for a given domain. That way, the SMTP server resolving to the domain in question will get all MX records in a different order every time a request is made, and most SMTP servers will target the first record in a response, similar to the scenario described in a previous section.
For example, there are 3 MX records for mail.mywebsite.com, each pointed to a different mail server:
The second option is to create multiple A records for the same mail server hostname and point each one of them to different IPs of the target mail server machines. Afterward, a single MX record with a priority of 10 (which is the most common priority assigned to MX records) is created and pointed to the IPs of the target mail server.
For example: