The previous article explores the different ways a Server or Router can be overburdened. Load Balancers to our Rescue.
A Load Balancer distributes Network traffic by the help of either Hardware or Software and acts as a Reverse Proxy.
They distribute traffic using the following algorithms:
1. Round Robin — Server instances are placed in a double-ended queue.
2. Least Connections — Request is sent to the Server instance with least number of current Connections.
3. Least Response Time — Calculates Response time from each instance and finds one with least response time for Client.
4. Source IP Hash — Combine Source and Destination address, create a hash, Hash determines the Server Instance.
A Load Balancer can work on either of 2 Layers of the OSI model:
> Layer 4 (Network Load Balancer) — Looks into the TCP or UDP packets of the incoming request and accordingly routes to the Server Instance.
> Layer 7 (Application Load Balancer) — Looks into the HTTP contents like Cookies and Service Requested and accordingly routes to the Application Instance.
To check if all instances configured under a Load Balancer are active, a Load Balancer sends Health Check pings to all instances. Instances that send a reply are considered Healthy.
Unhealthy instances are removed from the route table till they are fixed and show up as healthy again.
A single Server instance can be overwhelmed easily. Thus, we must have multiple instances of the same Server and a Load Balancer to distribute load among them.
Load Balancer is placed between the Server instances and the Clients.
The Load Balancer may also be coupled with Firewall, IDS, IPS.
A Client establishes a connection with the Load Balancer. Requests are sent to the Load Balancer which forwards it to the Instance.
The Server Instance returns a reply to the Load Balancer.
The Load Balancer returns the Reply to the Client.
The Load Balancer here acts as a Proxy for the Server. This is called Reverse Proxy.
While choosing which Server to send the Request to, a Load Balancer can follow one of the following algorithms or an enhancement of these:
- Round Robin
All available instances are placed in a Double-Ended Queue. Requests are assigned to each element on the list. When the list ends, the pointer goes back to the top.
2. Least Connections
The Request is routed towards the Instance that is currently processing Least number of Active Requests (Connections).
3. Least Response Time
The Load Balancer calculates the Response times from each Instance. The Instance that provides the least Response time is given the Request.
4. Source IP Hash
A Layer 4 Load Balancer is like a normal Load Balancer.
It maintains a Stateful NAT connection between Client and Server Instance.
A large number of NAT connections can be created thus allowing the Load Balancer to serve huge number of Requests.
It simply routes the Requests according to requirement. It does not look into the content of the Request.
It thus stores no cache and requires no decryption as well.
These make it an inexpensive and efficient choice for Load Balancing where Smart Load Balancing and Microservices are Not Required.
A Layer 7 Load Balancer could be a little different.
When a Layer 7 Load Balancer receives a Request from a client, it terminates the connection with the Client.
It then investigates the content of the packet and accordingly routes it to an Instance.
This allows the Load Balancer to support Microservices Instances.
The IP of the Client is written on a HTTP Header called “X-Forwarded-For”.
The Server returns replies to the Load Balancer which in turn reestablishes connection with the Client and sends back the Reply.
Here two connections are required:
Client — Load Balancer | Load Balancer — Server.
Also, as the contents of the packet need to be investigated, the packet must be decrypted first.
To Decrypt the packet, it is required for the Client and Load Balancer to exchange Keys between them.
While this provides for great technical convenience and Smarter load balancing, it is also a huge liability to protect this Load Balancer.
If this is compromised, all data from Clients can be exposed.
While Load Balancers do a great job distributing the traffic, it is also important to note that they are effectively the Bottleneck of the system.
If a Load Balancer fails, the whole system comes crashing down.
While such instances could be extremely rare, they cannot be ruled out of possibility.