0

Is it possible to implement gRPC client sharding on balancer level?

Let's say I want to shard my requests to service foosvc, which I name resolve beforehand to 3 servers (foosvc1, foosvc2, foosvc3) and I want client1 to always connect to server foosvc1, client2 to foosvc2, etc. I tried creating a custom balancer, but I do not see any way to access the request's metadata inside of the balancer.

Can I create a custom balancer (base on the balancer package) to do this or do I need to have multiple grpc clients and solve this in my calling logic? Or perhaps this is some sort of anti-pattern and there is a better solution?

sveatlo
  • 543
  • 9
  • 27

1 Answers1

1

For anyone looking at this. The trick is to pass the "sharding key" to the Picker in context, which can then use this to calculate hash and decide which subconnection to use. See https://github.com/liyue201/grpc-lb/blob/master/balancer/consistent_hash.go for an implementation example.

sveatlo
  • 543
  • 9
  • 27