Category: spring_boot
Load balancing in microservices
Published on 22 Aug 2026
Explanation
Load balancing distributes incoming requests across multiple instances of the same microservice. This helps improve performance, availability, and scalability.
Code:
Client
↓
API Gateway
↓
┌───────────────┐
│ Load Balancer │
└───────┬───────┘
│
┌────┼────┐
↓ ↓ ↓
User User User
:8081 :8082 :8083
``
Explanation
Without load balancing, all requests may go to a single service instance. If that instance becomes overloaded or unavailable, the application can be affected.
Code:
Client ↓ User Service :8081 ↑ All Requests Problem: Single instance can become overloaded.
Explanation
With Spring Cloud LoadBalancer, requests can be distributed across multiple instances of a service. Eureka can provide the available service instances.
Code:
Eureka
│
┌───────────┼───────────┐
▼ ▼ ▼
USER-SERVICE USER-SERVICE USER-SERVICE
:8081 :8082 :8083
\ │ /
└────── Load Balancer ──────┘
↑
│
Gateway
Explanation
Spring Cloud Gateway can use the lb:// prefix to route requests through Spring Cloud LoadBalancer. The service name is resolved through service discovery such as Eureka.
Code:
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/api/users/**
Explanation
When multiple instances of USER-SERVICE are registered, the load balancer selects an available instance for each request. This allows the service to scale horizontally.
Code:
Request 1 → USER-SERVICE :8081
Request 2 → USER-SERVICE :8082
Request 3 → USER-SERVICE :8083
Request 4 → USER-SERVICE :8081
Multiple instances
↓
Better scalability
Explanation
Load balancing improves availability because requests can be sent to other healthy instances when one instance becomes unavailable. Service discovery and load balancing work together in a typical Spring Cloud architecture.
Code:
React ↓ API Gateway ↓ Load Balancer ↓ Eureka ↓ ┌────────┬────────┬────────┐ │ :8081 │ :8082 │ :8083 │ │ UP ✓ │ DOWN ✗ │ UP ✓ │ └────────┴────────┴────────┘ Requests → Healthy instances