grpc-spring icon indicating copy to clipboard operation
grpc-spring copied to clipboard

A question about Grpc load balancing and long connections

Open JanYork opened this issue 2 years ago • 4 comments

When I used Nacos+Grpc to perform multiple service load balancing tests, a question arose: the Grpc client initiated a request for the first time, and Grpc service A1 responded to the request. However, the GRPC client initiated a request for the second time, and Grpc service A2 responded. , the interval between the two requests is only 1.5s, but the responding servers are different. However, the bottom layer of Grpc uses netty for long-term connection communication, but two requests from the same client within 1.5 seconds are responded to by different servers. Does this mean that the Grpc client and Grpc server have long connections in a load-balanced environment? advantages will be lost?

JanYork avatar Jan 18 '24 06:01 JanYork

I think the default load balancing policy is round robin. You can overwrite that with the following config https://github.com/grpc-ecosystem/grpc-spring/blob/f72eda3b6434f5737786bb66f373e8f58ce7a56e/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java#L147

Should we use a different default? 🤔

ST-DDT avatar Jan 18 '24 08:01 ST-DDT

I think the default load balancing policy is round robin. You can overwrite that with the following config

https://github.com/grpc-ecosystem/grpc-spring/blob/f72eda3b6434f5737786bb66f373e8f58ce7a56e/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java#L147

Should we use a different default? 🤔

Thank you, but if I rewrite the load balancing algorithm, what kind of load balancing algorithm can retain the advantages of Netty's long connection?

JanYork avatar Jan 18 '24 10:01 JanYork

Pick first is pretty close, but it will (by default) only ever pick the first server in the list, instead of a random one. There now is an option to shuffle it first, but I'm not sure how to configure that.

  • https://github.com/grpc/grpc-java/pull/10110
  • https://github.com/grpc/grpc-java/blob/005f8c006d68ee8c305ed5292a8a6de04319bea1/core/src/main/java/io/grpc/internal/PickFirstLoadBalancer.java#L65

Maybe copy the file and just enable it by default.

ST-DDT avatar Jan 18 '24 11:01 ST-DDT