Multiple AWS instances and one database

amazon ec2amazon-web-services

I have a web app that lives on two AWS instances – one instance hosts the site, and the other hosts the actual database (MySQL). The instance hosting the site has an elastic IP pointing to it, which is tied to my domain name.

Obviously, one of the major benefits of AWS is being able to run a bunch of instances at once when things get busy, and AWS even offers automated solutions to do that. Great. But if I have my domain pointing at one IP, and that IP tied to one instance, how do I spread out the traffic among many instances? Do I have the primary machine act as a load balancer? If so, is there a commonly used load balancing solution for use with LAMP?

Best Answer

You can use round-robin DNS, as previously mentioned. And if you're looking for a quick and easy solution, that's probably what I'd recommend as well.

However, EC2 instances aren't durable and you shouldn't treat them as such. Meaning, that they can up and disappear like a fart in the wind and your application has to be tolerant of that. RR DNS could be a source of pain for you because its not smart enough to deal with fault-tolerance/high-availability. If one of your web servers were to die, the user would be stuck trying to connect to the dead machine until the TTL lapsed on the record. The previously mentioned trick of setting a ridiculously low TTL won't work. Downstream caching DNS servers won't honor a TTL that low. You'd be lucky to get away with a TTL of one hour. Many users might be stuck trying to connect to the same dead web server for 24 hours or longer.

I'd recommend that you look into HAProxy. Reddit.com uses HAproxy on EC2 to load-balance about 200 million pageviews/mo. across several dozen web server instances. I'm sure it'll handle your app as well.

Related Topic