Load Balance

# Load Balance

# What is a load balancer?

A Load Balancer (LB) is a device or software application that distributes incoming network or application traffic across multiple servers. By doing so, a load balancer aims to optimize resource use, maximize throughput, minimize response time, and avoid overloading a single server.

# Types of load balancer?

  1. Layer 4 Load Balancers: Operate at the transport layer (TCP/UDP). They route traffic based on the source and destination IP addresses and ports.
  2. Layer 7 Load Balancers: Operate at the application layer (HTTP/HTTPS). They can make more intelligent routing decisions based on the content of the request (i.e. HTTP request header, url information, etc.).

For example, something you could say in a system design interview:

"I would place a Layer 7 load balancer in front of the web servers. This allows us to route traffic intelligently, perhaps directing API calls to a specific set of servers optimized for those operations. The load balancer would perform health checks to ensure traffic is only directed to healthy servers. To avoid it becoming a single point of failure, we could use a secondary load balancer as a fallback."

# Load balancing algorithms

Overview

  • Static
    • Random
    • Round-robin
    • Sticky round-robin
    • Weighted round-robin
    • IP hashing
    • Consistent hashing
  • Dynamic
    • Least connections
    • Least response time

Load balancing algorithms overview

Static algorithms: Distribute requests without taking into account real-time server state, conditions, and performance.

# Round robin

Round-robin

# Sticky round robin

Sticky round-robin

# Weighted round robin

Weighted round-robin

# IP hashing

Hashing


Dynamic algorithms

# Least connections

Least connections

# Least response time

Least response time

# Load balancer solutions

Layer 4: Linux Virtual Server, HAProxy, Nginx Layer 7: HAProxy, Nginx