What are the common load-balancing algorithms, and when would you use each?
MediumConceptLoad Balancing
Answer
Load-balancing algorithms are either static, ignoring current server load, or dynamic, using live metrics. The right choice depends on whether servers and requests are uniform.
- Round robin: sends requests to servers in turn; works well when servers are identical and requests cost about the same. Weighted round robin gives more powerful servers a larger share.
- Least connections: picks the server with the fewest active connections; better for long-lived or uneven requests such as WebSockets.
- Least response time: considers measured latency as well as load.
- Hash-based (client IP or request key): sends the same client or key to the same server, which helps session affinity and cache locality but can create hot spots.
- Power of two choices: samples two servers at random and picks the less loaded one.
More in Load Balancing
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is a load balancer, and why is it used? | Concept | Easy | Read the answer to What is a load balancer, and why is it used? | |||
| 2 | What are the common load-balancing algorithms, and when would you use each? | Concept | Medium | Read the answer to What are the common load-balancing algorithms, and when would you use each? | |||
| 3 | What is the difference between Layer 4 and Layer 7 load balancing? | Concept | Medium | Read the answer to What is the difference between Layer 4 and Layer 7 load balancing? | |||
| 4 | What is consistent hashing, and why is it used? | Concept | Hard | Read the answer to What is consistent hashing, and why is it used? |