spring-cloud-commons icon indicating copy to clipboard operation
spring-cloud-commons copied to clipboard

Suggestion: Create distinct positions by serviceId on RoundRobinLoadBalancer

Open thomasdac opened this issue 4 years ago • 0 comments

Well basically I have a gateway that contains 2 routes that use load balancer URIs:

route_1 -> Service_A (1 instance)

route_2 -> Service_B (2 instances)

I noticed a strange behavior in route_2, where all requests were always sent to the same instance, ignoring the other.

The problem occurred because I was loading a page that calls both routes and the position counter is shared between all services:

(Page loading)
1 - Call route_1 -> position 0 go to 1 -> position % 1 (Service_A) = 0 (Single instance)
2 - Call route_2 -> position 1 go to 2 -> position % 2 (Service_B) = 0 (First instance)
(Page reloading)
3 - Call route_1 -> position 2 go to 3 -> position % 1 (Service_A) = 0 (Single instance)
4 - Call route_2 -> position 3 go to 4 -> position % 2 (Service_B) = 0 (First instance)

I'm using Spring Boot 2.5.4, Spring Cloud 2020.0.3 and Eureka as Service Discovery.

It would be nice if the position counter were by serviceId. This can be done by replacing the counter with a map, such as Guava's AtomicLongMap.

thomasdac avatar Sep 19 '21 00:09 thomasdac