Skip to main content

Load Balancing with Amazon EC2 and Elastic Load Balancing

 

Hosting your applications in the cloud yields many benefits, however it can be at times a very scary place when thing’s go wrong, and they always do.

If you love your customers then you owe it to them to make sure you have in place redundancy and ensure you provide a high level of availability.

The Amazon EC2 Infrastructure provides a really easy way to set this up using the Elastic Load Balancing feature.

Before starting you need the basic concepts of how the EC2 infrastructure works and how it can help you in your quest for high availability.

Regions


Regions in EC2 are geographical locations where the Data Centres reside and at the time of writing there are four Regions:

  • US East – North Virginia
  • US West – California
  • EU – Ireland
  • APAC – Singapore

It goes without saying that the Region you select should be always be as close to your customers as possible.

Availability Zones

Each Region has 2 or more Availability Zones. These are important to understand as they are independent from each other in that a failure of one zone does not affect the other.

Network Diagram

This is a fairly typical approach to load balancing a web application but it’s important to note that the level of redundancy is achieved by putting different instances in different Availability Zones.

LoadBalancingEC2 

The key thing to note here is that the instances you load balance should be in separate Availability Zones.

This post will be covering the Load Balancing aspect as I will leave the database redundancy to another post.

Creating The Load Balancer

Creating the Load Balancer is easy using the AWS Console

Under Networking & Security > Load Balancers you can “Create Load Balancer”.

load_balancer

This should be fairly self explanatory.

First we are assigning a name to our Load Balancer and then we are setting up the Ports the Load Balancer should listen on and forward to.

If you want to use HTTPS then you will need to do Port Forwarding as show in the highlighted image.

Configure Health Check

Next you need to setup your Health Check, this is how the Load Balancer decides if your Instance can have requests forwarded to it.

You can do this by setting the Ping Path which is an HTTP resource that is polled at an interval you define.

If it returns an HTTP result code other than 200 it will cause your instance to be deemed unhealthy and subsequently removed from the Load Balancer.

load_balancer_check

The Ping Path can be a static HTML resource, however it’s a good idea to run other any system checks and so I like to use a .ASPX file with Code-Behind instead.

Adding Instances

Here you can select Instances from all Availability Zones in your Region, as I mentioned before you should select Instances in different Availability Zones to provide a higher level of availability. Although you can have a single instance, it goes without saying you should have at least two.

load_balancer_add_instances

 

DNS

When a load balancer is created it automatically gets assigned a DNS Name. To point your website address to your load balancer all you have to do is create a CNAME record for it.

load_balancer_dns

 

Managing The Load Balancer

Now that you have setup your load balancer you can add more instances easily and get visibility of the Health of each instances by selecting the Load Balancer in the AWS Console.

Healthy

load_balancer_healthy 

Unhealthy

And this is what it looks like if you have an unhealthy instance.

load_balancer_unhealthy

 

 

Key Issues

Although EC2 alleviates lot of pain out of setting up a load balanced solution there are a few issues you need to be aware of before diving headfirst into this solution.

Connection Timeout Limit

At present the load balancer does not hold connections open for more than 60 seconds by design. So if you have any requests which take longer than a minute you are going run into problems. If you do find yourself in this position you should really be looking at how to reduce the Response times by using one-way Messaging or reducing the operation.

Static IP Address Support

Unfortunately there is currently no support for a Static IP address for your load balancer. So if you are integrating with third parties who have strict firewall policies then you may have problems. I’m hoping though that Amazon add this feature in the future as it is needed in a lot of scenarios.

SSL Support

To enable HTTPS you have to use port forwarding at the load balancer level, which can be achieved by listening on 443 and then forwarding to another port for instance 8443.

In IIS you then need to change the port in the Site Bindings, and don’t forget to open 8443 on your Firewall.

load_balancing_iss_bindings

 

Sticky Sessions

The Elastic Load Balancer does support Sticky Sessions via Cookies or QueryString however it is only works for Port 80 traffic.  
So if you’re using InProcess Session State you will need to move to the SQL based provider.

Cache 

If you’re using System.Web.Caching.Cache to cache objects then you’re also going to run into issues because of the Sticky Session problem on HTTPS. If Caching is a critical factor in your applications performance then you will need to consider a Distributed Cache solution like Memcache or Shared Cache.

 

Well that’s about all there is to it. I hope this helps someone.

Till next time.

Popular posts from this blog

Freeing Disk Space on C:\ Windows Server 2008

  I just spent the last little while trying to clear space on our servers in order to install .NET 4.5 . Decided to post so my future self can find the information when I next have to do this. I performed all the usual tasks: Deleting any files/folders from C:\windows\temp and C:\Users\%UserName%\AppData\Local\Temp Delete all EventViewer logs Save to another Disk if you want to keep them Remove any unused programs, e.g. Firefox Remove anything in C:\inetpub\logs Remove any file/folders C:\Windows\System32\LogFiles Remove any file/folders from C:\Users\%UserName%\Downloads Remove any file/folders able to be removed from C:\Users\%UserName%\Desktop Remove any file/folders able to be removed from C:\Users\%UserName%\My Documents Stop Windows Update service and remove all files/folders from C:\Windows\SoftwareDistribution Deleting an Event Logs Run COMPCLN.exe Move the Virtual Memory file to another disk However this wasn’t enough & I found the most space was

3 Reasons Why Progressive Web Apps (PWAs) Won’t Replace Native Apps

Many people believe Progressive Web Apps (PWAs) are the future of the mobile web, but in my opinion, PWAs are not a replacement for native mobile apps. Here are three reasons why: 1. Native mobile apps provide a smoother & faster experience  Mobile websites, progressive or otherwise are slower and not as smooth. 90% of the time spent is spent using apps vs the browser . The single most significant contributing factor to a smooth experience on mobile is the speed of the network and latency of the data downloaded and uploaded. When you visit websites on desktop or mobile, there is a lot of third-party code/data that gets downloaded to your device, which more often than not has zero impact on the user experience. This includes: CSS (Cascading Style Sheets) JavaScript Ad network code Facebook tracking code Google tracking code The median number of requests a mobile website makes is a shocking  69 . On the other hand, native apps only get the data that is requi

Unit Testing Workflow Activities in .NET 4.0

  Recently during a catch up with my buddy Keith Patton I was (as I tend to do) singing the praises of the Workflow in .NET 4.0. The all important question about Unit Testing support was raised, I tried as best as I could to explain the new In and Out Arguments but I didn’t feel I was convincing enough, so I though I would clarify with a blog post. If you developed Workflows in .NET 3.5 then you will be well aware of the lack of Unit Test support which was due to many reasons but mostly in part to the complex Workflow hosting environment. When moving to .NET 4.0 Workflow Foundation from .NET 3.5 it pays to be conscious of that fact that there is no longer a distinction between Activities and Workflows. Everything derives from the System.Activities.Activity class. So the definition of a Workflow is just a collection of of 1 or more Activities. I have designed a very simple Rental Car Activity which takes the an Applicants age as the input and outputs a True or False depending o