Load Balancing is the concept of splitting application or network load between two or more servers, allowing for better performance and longer uptime. AWS’s Load Balancers cost money to run, but you can achieve the same effect for free using Route 53.
How Does DNS Load Balancing Work?
With a normal load balancer, like AWS’s Application Load Balancer, you direct all traffic towards the load balancer, and it handles the job of routing traffic to your instances. Really, all AWS’s load balancers are are instances running a service like HAProxy, and handling a high amount of traffic.
The idea behind DNS load balancing is pretty simple. Even with the regular load balancer, the user needs to make a DNS lookup to know where to send any requests to your website. However, the DNS doesn’t have to be static, and that’s the idea that AWS’s Route 53 DNS service has implemented. Once set up, whenever User A requests the site, he will be sent to Server 1, and when User B requests the site, Route 53 will send them to Server 2 instead.
This can optionally be set up in a different pattern—failover. If your application can run on a single server, but you need 100% uptime, Route 53 can redirect traffic to a standby instance whenever the main instance becomes overloaded or unavailable.
If you want to set either of these patterns up, Route 53 makes it easy. If you’re not already using Route 53 and want to switch, you can read our guide on transferring your domain name to it.
Configuring Route 53
Sign in to the Route 53 management console, and select “Health Checks” in the sidebar, and create a new health check. These will be used to route traffic away from unhealthy instances in the scaling group. These cost $0.50 per month each to run, but they are optional.
Health checks have a few options, but mostly you’re just giving Route 53 an endpoint to monitor. You can use IP address or Domain name, but if you’re monitoring the health of an individual server, you’ll want to use the Elastic IP address for that server.
You’ll want to repeat this process for all of your servers, creating a health check for each.
Click on “Hosted Zones” in the sidebar, and bring up the hosted zone for your domain. “Create” or edit a new A Record, and enter in the IP address of one of your servers. You can also just use Aliases, which map dynamically to an AWS resource.
If you set the routing policy to “Weighted,” you can assign this record a weight. Giving a weight of 1 to multiple A Records will make Route 53 choose from between them equally. For “Set ID,” enter in something unique that’s the same for all records in this weighting group.
Below, click “Yes” for “Associate With Health Check,” and select the health check for this server. If the health check fails, this record won’t be picked.
Repeat this process for each server.
If you want to set up failover, instead set the routing policy to “Failover,” and select either “Primary” or “Secondary,” depending on the server. You’ll also want to link this with a health check.
Once you save the records, Route 53 should start balancing traffic.