wasAsked.

What is load balancing and which load balancing algorithms are common?

Frequently Asked (All Companies) · Role not specified · system-design

Published

Year not specified · System Design

Community answers

Posted by wasAsked editorial collection

Load balancing distributes incoming requests across multiple servers or instances so that no single server is overwhelmed. It improves availability (traffic avoids failed instances), scalability (add instances to handle more load) and performance (lower response times). Where load balancers sit - Between clients and web servers, between services, and in front of databases or caches (for read replicas). - Implementations: hardware appliances, software (NGINX, HAProxy, Envoy), cloud services (Azure Load Balancer, Azure Application Gateway, Azure Front Door, AWS ALB and NLB, Google Cloud Load Balancing), DNS-based and client-side load balancing, and Kubernetes Services and ingress controllers. Layer 4 vs Layer 7 - Layer 4 (transport): routes TCP or UDP connections by IP and port; very fast; does not inspect HTTP content. - Layer 7 (application): understands HTTP; can route by path, host or headers, terminate TLS, rewrite requests, use cookies for session affinity and apply a WAF. Common load balancing algorithms Static algorithms (do not consider current server load): 1. Round robin: requests go to servers in turn (1, 2, 3, 1, 2, 3...). Simple and good when servers are similar and requests cost about the same. 2. Weighted round robin: servers with more capacity get proportionally more requests (weights 3:1). Useful for mixed hardware or canary releases (send 5% to a new version). 3. IP hash / source hash: the client's IP determines the server, giving simple stickiness. Uneven when many clients share one IP (corporate proxies). 4. Consistent hashing: maps keys (user ID, cache key) to servers on a hash ring, so adding or removing a server moves only a small fraction of keys. Widely used for distributed caches and sharded systems. 5. Random: picks a server at random; works surprisingly well at scale. Dynamic algorithms (consider current state): 6. Least connections: sends new requests to the server with the fewest active connections. Good when request durations vary (long-lived connections, WebSockets). 7. Weighted least connections: least connections adjusted for server capacity. 8. Least response time / latency-based: chooses the fastest-responding server; global load balancers also route users to the nearest healthy region. 9. Power of two choices: pick two servers at random and send the request to the less loaded one — near-optimal balance with very little overhead (used by Envoy and others). 10. Resource-based: uses CPU or memory metrics reported by servers. Essential features - Health checks: probe endpoints (for example /health/ready) and remove unhealthy instances automatically. - Session persistence (sticky sessions): routes a user to the same server, needed only for stateful apps — prefer stateless services with shared state in Redis or a database. - Connection draining: let in-flight requests finish before removing an instance during deployments. - TLS termination and certificate management. - Autoscaling integration: register new instances automatically. Global load balancing - DNS-based or anycast services (Azure Front Door, AWS Route 53 and Global Accelerator, Cloudflare) route users to the nearest or healthiest region for low latency and disaster recovery. Interview summary: "Load balancing spreads traffic across instances for availability and scale. Common algorithms are round robin, weighted round robin, least connections, IP and consistent hashing, least response time and power of two choices. Health checks, connection draining and stateless services are what make it work reliably."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics