Load Balancing
Load balancing is the process of distributing network traffic across multiple servers. This ensures that no single server bears too much demand. By spreading the work evenly, load balancing improves application responsiveness and increases availability.
1. How it Works
A load balancer sits between client devices (users) and backend servers. It receives incoming requests and routes them to a capable server based on a configured algorithm.
- Traffic Cop: It acts like a traffic cop, preventing traffic jams on any single server.
- Health Monitoring: If a server goes down, the load balancer stops sending traffic to it.
2. Load Balancing Algorithms
Different strategies are used to decide which server gets the next request.
- Round Robin: Requests are distributed sequentially (Server A -> Server B -> Server C -> Server A). Simple but assumes all servers are equally powerful.
- Least Connections: Sends the request to the server with the fewest active connections. Good for long-lived sessions.
- IP Hash: Uses the client's IP address to determine which server receives the request. Ensures a user always connects to the same server (Session Persistence).
3. Types of Load Balancers
Layer 4 (Transport Layer)
- Operates at the TCP/UDP level.
- Routing decisions are based on IP address and port (e.g.,
192.168.0.1:80). - Pros: Very fast, low overhead.
- Cons: No visibility into the actual content of the message.
Layer 7 (Application Layer)
- Operates at the HTTP/HTTPS level.
- Routing decisions can be based on URL, headers, cookies, or message content (e.g., route
/apito Backend A and/imagesto Backend B). - Pros: Smarter routing decisions.
- Cons: More computationally expensive (needs to decrypt HTTPS).
4. Health Checks
Load balancers regularly check the status of backend servers to ensure they are available.
- Active Checks: The load balancer pings the server (e.g.,
GET /health). If it failsntimes, the server is marked "Unhealthy". - Passive Checks: The load balancer observes real traffic. If users get 500 errors from a server, it stops sending traffic there.
programming/distributed-systems programming/microservices-architecture programming/cloud-computing-basics