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

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...