A quick guide to load-balancing your applications
So you have completed the development of your application and now you are ready to make it live. You choose to host your application on AWS and your thoughts naturally move towards the scalability of your app. Thinking of which you ask yourself, “If my app is going to be popular, there are going to be thousands or even tens of thousands of requests. How can I prevent overwhelming my servers?”
Let’s understand the capabilities AWS offers through Elastic Load Balancing (ELB) and the various types of load-balancing.
What exactly is ELB?
Before we dive into the capabilities, let’s explore what is ELB in the first place. AWS ELB is a way to automatically distribute incoming traffic to your apps. The traffic gets distributed to various targets such as EC2 instances, containers, Lambda functions, etc. In this way, ELB helps us to handle the incoming load to ensure high availability, automatic scaling and helps to ensure fault tolerance in our apps.
ELB also offers health-check monitoring of the targets. This means that ELB is aware of the health status of each target and only directs traffic to the healthy ones.
There are 4 types of ELB that AWS offers:
Application Load Balancer (ALB)
Network Load Balancer (NLB)
Gateway Load Balancer (GLB)
Classic Load Balancer (CLB)
Each of the load balancers has a dedicated purpose and naturally, it is important to understand these purpose and choose the most applicable type for your application.
Which Load Balancer Should I Use?
Each of the load balancers offered has its advantages, but most often you will be needing either the ALB or NLB. Both types utilise the concept of target groups. A listener first receives the incoming requests and then decides depending on the configured rules which target group will receive the request. The target group will then direct the request to an instance, container or IP address.
Application Load Balancer
Introduction
As the name suggests, the ALB works at Layer 7 (HTTP). ALB comes with a number of options for routing the incoming traffic. For example, you can route based on the hostname, the HTTP method used, the headers, etc.
ALB can also be used to return fixed responses or redirection and it supports HTTP 2 and WebSockets.
Thinking of authentication, ALB supports a wide range of authentication mechanisms including the standard and popular ones such as SAML, LDAP, and Microsoft AD. Social login through Facebook or Google is also supported. This means that your app can leave the authentication layer to the load balancer instead of handling that itself.
When to Use ALB?
The most common use case for ALB is for web apps. A typical web app built on the microservices framework can use ALB as the load balancer before incoming traffic reaches your EC2 instances or the containers hosted for a service.
Network Load Balancer
Introduction
The NLB works at Layer 4 (Network). It handles both TCP and UDP protocols. NLB offers great performance. It can handle millions of requests per second and offers low latency. NLB, by default, stores the source IP address in the network packets (for both TCP and UDP).
When to Use ALB?
NLBs cover the remaining scenarios that ALBs do not. For example, the app depends on a protocol apart from HTTP. Or, the app is time-sensitive. The data flow is real-time. Apps dependent on streaming audio, video, currency quotes, etc will benefit from using ALB.
Classic Load Balancer
You would also see the option for the CLB on AWS. CLB provides basic load balancing functionalities across EC2 instances. However, this is meant for more legacy applications that are still dependent on the EC2-Classic network and will be less applicable to developers who are working with the newer instances/networks offered by AWS.
Some Key Terms
Target Groups
A target group refers to a group of resources that our load balancer routes the request to. An ELB can support various target groups and each target group will have a unique listener configuration and associated rules.
Each target group will then route the request to targets such as EC2 instances or Lambda functions. A target can be mapped to multiple target groups as well.
Listeners
ELB listeners help to check for incoming requests to our application. We can configure a listener based on the protocol (HTTP or HTTPS) and the port (1–65535).
Each listener will also have rules which determine how the incoming connections will be routed to target groups. Each load balancer will need to have at least one listener.
Health Check
Health check is a way to understand the availability of the targets within the target group. To ensure that the targets are healthy, the load balancer will “ping” the target and send requests to ensure they are up and running. These activities and monitoring are termed as health checks.
Putting It Together
Putting it all together, our entire load balancing flow and architecture can be summarised by the above diagram. We have a load balancer that has listeners. The incoming requests reach our listeners and depending on the configured rules, they are directed next to the target group. Within the target group, we have multiple targets configured and a healthy target will be chosen to eventually receive and process the requests.
In case you are wondering about session management, both ALB and NLB support sticky sessions as well. Stick sessions refer to the mechanism whereby requests from the same client will always map to the same target. For example, in ALB, both duration-based and application-based cookies are supported. This can be configured in the target group settings. Similarly, with an NLB, based on the source IP, the request can be mapped to the same target.
Conclusion
As your application scales to more and more users, load balancers are an integral part of the application architecture to ensure high availability and uptime. AWS offers great load balancing capabilities and four types of load balancers. Most often you will be needing either the Application Load Balancer (ALB) or the Network Load Balancer (NLB). Choosing between the two really depends on the type of application. For most use cases I have seen, AWS ALB suits really well and is capable of handling the load/traffic well.
Do leave your experiences with AWS load balancing features in the comments!
Happy coding! 💻
More content at plainenglish.io