Load Balancing
Learn how load balancers distribute incoming traffic, perform health checks and provide a stable entry point to horizontally scaled application instances.
Concept Overview
A load balancer distributes incoming traffic across multiple backend instances. Without it, a single server can become a bottleneck and a single point of failure. A load balancer can provide one entry point, route traffic among healthy instances and allow application capacity to grow horizontally. Its routing algorithm and health-check strategy should match the workload and deployment environment.
1. Layer 4 and Layer 7 load balancing
Layer 4 load balancing operates at the transport layer and uses information such as TCP or UDP, IP addresses and ports. Layer 7 load balancing operates at the application layer and can inspect HTTP information such as hostnames, URL paths, headers and cookies. Layer 7 routing can direct different paths to different services, while Layer 4 routing works with lower-level connection information.
2. Understand common distribution algorithms
Round robin sends requests to instances sequentially. Weighted round robin assigns different proportions of traffic to instances with different capacities. Least connections chooses an instance with fewer active connections. IP hash uses a client's IP address to select an instance and can provide affinity, but may cause uneven distribution. No algorithm guarantees uniform work when requests have very different costs or when traffic is uneven.
- Round robin: simple sequential distribution.
- Weighted round robin: account for different instance capacities.
- Least connections: consider active connection counts.
- IP hash: provide deterministic affinity, with a risk of imbalance.
3. Health checks prevent routing to unhealthy instances
A load balancer should avoid sending new traffic to instances that are not ready to serve requests. Active health checks periodically call a health endpoint. Passive checks observe failures in real traffic. In the Task Management API example, an endpoint such as GET /actuator/health can provide health information. A robust deployment should distinguish liveness from readiness where the platform supports those concepts, and should avoid declaring an instance healthy merely because its process exists.
Load Balancer
|-- App 1: HEALTHY -> receive traffic
|-- App 2: HEALTHY -> receive traffic
|-- App 3: FAILED -> remove from rotation4. Reverse proxy responsibilities
A reverse proxy sits between clients and backend servers. In addition to forwarding requests, it can provide routing, TLS termination, load balancing, compression, request filtering and header manipulation. NGINX is one example. TLS termination may happen at the proxy, but whether the internal hop uses HTTP or HTTPS depends on the deployment's security requirements and trust boundaries.
5. Session affinity versus stateless APIs
Session affinity, also called sticky sessions, attempts to route requests from the same client to the same server. It can be useful for systems that retain session state locally, but it reduces routing flexibility and does not solve state loss when an instance fails. The supplied Task Management API design prefers stateless API instances with shared state, so sticky sessions are unnecessary for the intended model.
6. Failure scenario and request flow
If App 1 fails while App 2 and App 3 remain healthy, the load balancer should remove App 1 from rotation and continue routing requests to the healthy instances. This improves resilience at the application tier. It does not guarantee end-to-end availability if PostgreSQL, Redis, DNS, the network or the load balancer itself is unavailable. A complete availability design examines every critical dependency.
Client
|
v
NGINX / Load Balancer
|---- Spring Boot API 1
|---- Spring Boot API 2
|---- Spring Boot API 3
|
+---- Redis
+---- PostgreSQLKey Takeaways
- ✓A load balancer distributes traffic and routes around unhealthy instances.
- ✓Layer 4 and Layer 7 operate with different levels of protocol information.
- ✓Health checks are essential, but dependency failures must also be considered.
- ✓Stateless API instances reduce the need for sticky sessions.