spring-cloud / spring-cloud/spring-cloud-commons

Suggestion: Create distinct positions by serviceId on RoundRobinLoadBalancer

Open
#1,013 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
751
Forks
744
Avg merge
1d 14h
Merged PRs (30d)
9

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/RoundRobinLoadBalancer.java at the linked position counter and trace how serviceId and instance selection are handled. Review nearby load-balancer tests if present. Done means separate position state is used per serviceId so repeated calls to Service_B can reach both instances while single-instance services remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend, distributed-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.