Load balancers spread requests across backend servers and remove unhealthy ones. Round-robin and least-connections are common. For stateful apps, use sticky sessions or move state to a shared store.
How it works
- Distribution: LB forwards each request to one of the backends (app servers).
- Health checks: LB probes backends (HTTP, TCP) and stops sending traffic to failed or slow ones.
- Single entry point: Clients talk to the LB; LB hides the number and identity of backends.
Algorithms
- Round-robin: Rotate through backends in order; simple and fair for similar workloads.
- Least connections: Send to the backend with fewest active connections; good for variable request duration.
- Weighted: Assign more traffic to stronger backends; useful when nodes differ in capacity.
Stateful apps
- Sticky sessions: LB sends the same client to the same backend (by cookie or IP). Simple but ties client to one node.
- Shared state: Store session in Redis or DB so any backend can serve any user; better for scaling and failover.
- Stateless: Best option; no server-side session; scale and replace nodes freely.
Summary
Use a load balancer with health checks; choose round-robin, least-connections, or weighted. Prefer stateless apps or shared state over sticky sessions for scalability and resilience.




