What is a load balancer and why is it essential for cloud deployments?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
A load balancer receives traffic on a frontend address and selects one of the configured backend targets. With three Spring Boot instances, for example, clients call api.example.com; the balancer routes requests according to its algorithm and target health.
Health checks help stop new traffic going to an instance that repeatedly fails its configured probe. When instances are registered and drained correctly, the same mechanism supports scaling and rolling replacement. A layer-7 balancer can also route /orders and /users to different target groups, while a layer-4 balancer routes using transport-level information.
The balancer does not create high availability on its own. There must be multiple healthy targets, enough remaining capacity, resilient dependencies, and sensible timeout/retry behavior. Detection is not instantaneous, so some in-flight requests can still fail. I would call it important for multi-instance traffic management, not essential for every cloud application or a guarantee of zero downtime.
What is a load balancer and why is it essential for cloud deployments
Direct Answer
A load balancer accepts traffic on a stable endpoint and selects a healthy backend according to its routing policy. It becomes important when an application has multiple instances, needs failover, TLS termination, or traffic controls. It is not required for every small cloud deployment, and health checks and multiple backends are needed before it can improve availability.
Code Example
// Health check endpoint that load balancer probes
@RestController
public class HealthController {
private final DataSource dataSource;
public HealthController(DataSource dataSource) {
this.dataSource = dataSource;
}
@GetMapping("/health")
public ResponseEntity<Map<String, String>> health() {
try {
// Verify database connectivity
dataSource.getConnection().close();
return ResponseEntity.ok(
Map.of("status", "healthy", "database", "connected")
);
} catch (Exception e) {
// Load balancer sees 503 and stops routing traffic here
return ResponseEntity.status(503)
.body(Map.of("status", "unhealthy", "error", e.getMessage()));
}
}
}
Common Mistakes & Pitfalls
- Treating health checks as instant failover: tune intervals and thresholds and leave spare capacity.
- Making a deep dependency check the only liveness signal: distinguish process liveness from readiness to receive traffic.
- Publishing backend addresses as client contracts: use the intended service/frontend name.
- Using sticky sessions as a substitute for state design: understand failure and rebalance behavior first.
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.