Skip to main content

A Complete Guide to AWS Elastic Load Balancer using Nodejs

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.

elb

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 NodejsSetup.

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.

01

Click on Create Load Balancerbutton

02

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

03

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.

04

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.

05

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.

06

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

Once you complete the configuration and create the load balancer.

07

08

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.

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...