What is a load balancer, and why is it used?
BeginnerConceptInfrastructure & cloud basics
Answer
In one line: A load balancer sits in front of a group of servers and spreads incoming traffic across them, so no single server is overloaded and users keep being served even if some servers fail.
What a load balancer does:
- Distributes traffic across healthy instances.
- Health checks — regularly calls something like
/healthon each instance and stops sending traffic to unhealthy ones. - High availability — spreads traffic across availability zones.
- Enables scaling — new instances are added to the pool automatically.
- TLS termination — handles HTTPS certificates so apps don't have to.
- Zero-downtime deployments — shifts traffic between old and new versions (rolling, blue/green, canary).
Layer 4 vs Layer 7:
| Layer 4 (transport) | Layer 7 (application) | |
|---|---|---|
| Looks at | IP and TCP/UDP port | HTTP details: path, host, headers, cookies |
Can route /api and /images differently? |
No | Yes |
| Examples | AWS NLB, Azure Load Balancer | AWS ALB, Azure Application Gateway, Nginx, HAProxy, Envoy |
| Strength | Very fast, any TCP protocol | Smart routing, WAF integration |
Common algorithms: round robin, least connections, weighted (useful for canaries), IP hash / sticky sessions.
In Kubernetes:
- A
Serviceof typeLoadBalancercreates a cloud load balancer. - An Ingress controller or Gateway API implementation (for example NGINX, AWS Load Balancer Controller, Envoy Gateway) provides Layer 7 routing to many services through one load balancer.
Example: an ALB receives https://shop.example.com, terminates TLS, sends /api/* to the API target group and /* to the front-end, across three AZs, skipping any instance whose health check fails.
Tip for interviews: explain health checks and L4 vs L7 — those are the usual follow-up questions.
More on Infrastructure & cloud basics
What is the difference between declarative and imperative infrastructure?BeginnerWhat is idempotency, and why is it important in automation?BeginnerWhat is configuration drift, and how do you prevent it?BeginnerWhat is immutable infrastructure?BeginnerWhat does "pets vs cattle" mean?BeginnerWhat is cloud computing, and what are IaaS, PaaS and SaaS?Beginner