DEV Community

GaneshMani
GaneshMani

Posted on • Originally published at cloudnweb.dev

A Complete Guide to AWS Elastic Load Balancer using Nodejs

In this article, we will see what is elastic load balancer and how it is used in the application development.A Complete Guide to AWS Elastic Load Balancer using Nodejs.

Recent Article

Introduction to AWS SNS using Nodejs

How to Run MongoDB as a Docker Container in Development

How to Install Nodejs in AWS EC2 in a proper way

What is Elastic Load Balancer

Elastic load balancer is a cloud service that automatically distribute the incoming application traffic into multiple instances.

Let's understand this with an example,

Let's say that the application is running in an instance with certain amount of users.

what happens if there is sudden spike in the user load. how can you manage the load in your application server.

So, solution is to balance the load across different instances of the server. but, how do you manually distribute the load to multiple instances.

There comes the elastic load balancer. it will distribute the load for you without any manual work.

Here, elastic load balancer distribute the load to multiple instances. each client is distributes to different instances.

Key Concepts of ELB

Firstly, There are some key concepts of ELB that you need to understand before getting hands-on on the Load balancer.

  • Elastic Load balancer spreads the load across multiple instances
  • ELB exposes the single point of access for your application. user will have have the url of ELB.
  • it can seamlessly handle failures of down stream instances.
  • ELB does the regular health checks on your application instances, ELB terminates the instance from target group, if it is not healthy.
  • Elastic Load Balancer can provide https(SSL) for your application.
  • ELB can provide stickiness with cookies, which means a particular can always be routed to a particular instance.
  • Load Balancer provide high availability across zones.
  • it can separate public traffic from private traffic.

Types of ELB

  • Classic Load Balancer
  • Application Load Balancer
  • Network Load Balancer

Classic Load balancer is the oldest and kind of deprecated load balancer.

Mainly, Application load balancer(ALB) is the common load balancer nowadays.

ALB handles the traffic of HTTP/HTTPS and websocket protocols.

Network Load balancer(NLB) handles the (Layer 4) TCP traffic. NLB is super high performance among all the load balancers.

Application Load Balancer

Application Load Balancer(ALB) distributes the load across the application instances.

Like i said before, ALB supports the HTTP/HTTPS and websocket protocols.

ALB can enable the stickiness of client to server. stickiness can be enabled at the target group level.

what is Target Group

Internally, load balancer contains the listener which will check for some rule. when the rule is met, it will forward the request to specified target group.

listener rule can be a specific port or something. listener checks if the client request on the specific port(3000). after that, listener forwards the request to the respective target groups.

target group contains the multiple target(EC2 instances).each target group route the request to one or more registered target.

Application Load Balancer(Contd)

mainly, application server don't see the ip of the client that is requesting the resources. all the ec2 instance will get only the ip of elastic load balancer.

However, client ip can be accessed from the header X-Forwarded-For.

Network Load Balancer

Network Load Balancer(NLB) handles the (Layer 4) TCP Traffic. NLB is a super high performance with very low latency of ~100ms.

NLB can handle million request per second.

Enough of the theory part, let's learn how to configure the load balancer for a Nodejs Application.

Implementing ELB for Nodejs App

I assume that you have basic knowledge on setting up an EC2 in AWS. you can refer this article to learn more about EC2 and Nodejs Setup.

Source code for the node application can be found here.

Once you create an EC2 instance, now it's time to setup the load balancer for EC2 instance.

Click on Create Load Balancer button

Like we said before, it contains three types of Load Balancers, select Application Load Balancer here,

Here, we need to give name for the load balancer and scheme. scheme can be internet-facing and internal.

internet-facing means it can be accessed by client. internal can be accessed only by other instances.

After that, add the listener for the load balancers. it can listen for port 80 or some custom port.

Here our load balancer runs on default http port 80. so, add the listener for HTTP Protocol with port 80.

After that, configure the security group for load balancer. security group basically sets the inbound and outbound traffic ports for the load balancer.

Here, allow port 3000 which means the load balancer port 3000 will be outbound port to EC2 instance.

Now, it's time to configure the routing to the specific target group. create a new target group with target type as instance and port 3000.

Also, you can define different route for health check or base route to check the health of an instance.

Above all, we can also configure the advanced health check which have number of checks and time interval between health check etc.

Now, it is time to register the targets to the target groups.

Add the EC2 instance to the registered group and review the complete configuration once.

Once you complete the configuration and create the load balancer.

Summary

This article explains the Elastic Load balancer in detail. To Sum up, let's see some of the important points of ELB. A Complete Guide to AWS Elastic Load Balancer using Nodejs.

  • There are three types of LB. Application,Network and Classic
  • Load Balancer checks the health of an instance periodically.
  • LB can enable the stickiness in the target groups.
  • ALB supports HTTP,HTTPS and websockets protocol.
  • NLB is for TCP Protocol and it is very fast.
  • application instance don't see the client ip address. although, you can access the IP in X-Forwarded-For header.

Top comments (0)