What is Load Balancing in Microservices?

Load balancing means distributing incoming requests across multiple instances of a service to:
-
Improve performance
-
Increase availability
-
Avoid overloading a single instance
π Example:
Order Service has 3 instances β requests should be distributed among them.
πΉ Why Load Balancing is Needed?
Without load balancing:
-
One instance gets overloaded
-
Others remain idle
-
High latency & failures
πΉ Types of Load Balancing
1οΈβ£ Server-side Load Balancing
-
Load balancer sits before services
-
Example: Nginx, AWS ELB
2οΈβ£ Client-side Load Balancing (Spring approach)
-
Client decides which service instance to call
-
Uses service discovery (Eureka/Consul)
π Spring Cloud LoadBalancer = Client-side
πΉ What is Spring Cloud LoadBalancer?
It is the replacement of Netflix Ribbon.
It:
-
Works with service discovery
-
Chooses a service instance
-
Integrates with WebClient & Feign
πΉ How It Works (Flow)
-
Service registers with Eureka
-
Client asks Eureka for available instances
-
LoadBalancer selects one instance
-
Request is sent to that instance
πΉ Load Balancing Strategies
-
Round Robin (default)
-
Random
-
Custom (based on metadata, zone, etc.)
πΉ Example Configuration
1οΈβ£ Dependency
2οΈβ£ Using WebClient
3οΈβ£ With Feign Client
πΉ Real-World Example
π³ Payment Service has 5 instances
-
LoadBalancer distributes traffic
-
If one instance goes down β others continue
πΉ Spring Cloud LoadBalancer vs Ribbon
| Feature | Ribbon | Spring Cloud LoadBalancer |
|---|---|---|
| Status | Deprecated | Active |
| Reactive Support | Limited | Native |
| Spring Native | β | β |
π₯ Interview One-Liner
βSpring Cloud LoadBalancer provides client-side load balancing by selecting service instances dynamically using service discovery.β