Load Balancing
The main goal of load balancing is to ensure high availability, reliability, and performance by avoiding overloading a single server and avoiding downtime.
A load balancer reduces the load on individual servers and prevents any one server from becoming a single point of failure1, thus improving overall application availability and responsiveness.
It can be added at 3 places:
- Client and server
- server and server (depends on how many layers)
- server and DB
Uses Load Balancing
- Health Checks
If a server is not working at this moment, we should remove it from server list until it recoveries. - Session Persistence
In most of using session case, we need to make sure requests from the same users will redirect to the same server. - SSL/TLS Termination
We can put SL/TLS decryption in the load balancer layer, it can lower the burden of backend server.
Load Balancing Algorithm
- Round Robin(loop)
- Weighted Round Robin
- Least Connection
- Weighted Least Connection
- Least Response Time
- Random
- IP Hash (maintain session)
- Least Bandwidth
- Least Packets
- Chained Failover
- Dynamic Weight Adjustment
- Least Resource
- Fastest Finger First
Load Balancing Type
- Hardware load balancing
- Software load balancing
- Cloud-based load balancing
- DNS load balancing
- Layer 4 load balancing
It distributes incoming traffic based on information from the TCP or UDP header, such as source and destination IP addresses and port numbers. - Layer 7 load balancing
It takes into account application-specific information, such as HTTP headers, cookies, and URL paths, to make more informed decisions about how to distribute incoming traffic.
Layer4 vs Layer7
Layer 4
Pros
- Better performance
- Support variety of protocols
- simple and eazy to manage
Cons
- Lack of app info, limits its scenarios
- No consideration for server health, response time, or resource utilization
- Can't know about the session
Layer 7
Pros
- Considers application-level information
- Provide session or content-routing mechasism
Cons
- Slower and less efficient than layer4
- May required more specific settings, more complex
Challenges of Load Balancers
- Become single point of failure
- Configuration complexity
- Scalability limitations
- Add latency
- sticky sessions problem
- Cost
- Healthy check and monitor
Ref.
1. Single point of failure: A single point of failure (SPOF) refers to a component, system, or element within a larger system that, if it were to fail, could cause the entire system to fail or significantly degrade in performance.