gRPC: should I use a single client for the entire application?
Asked Answered
I

1

5

I know that the gRPC team recommends that we use a single channel through the entire app, which is exactly what we going to do. But we are not sure about the client class, that receives this channel on the constructor. What is the best practice:

  • Create a new instance of the client class each call
  • Use a single client instance per thread
  • Use a single client through the entire app (the same way we did on the channel)

We are interested in using the first approach, but we don't know if create a new instance of the client class also is a costly operation.

Iniquitous answered 19/7, 2019 at 10:42 Comment(0)
O
6

The client is essentially just a wrapper for the CallInvoker, which will be DefaultCallInvoker if you're starting from a Channel. DefaultCallInvoker is also just a wrapper for the Channel, with no state or complexity. Neither the client nor the call-invoker add anything like synchronization, so talking to a new client will behave identically to talking to a pre-existing client that shares the channel.

Conclusion: it isn't exactly expensive to create a new client / call-invoker, but if you want to shave an extra 2 allocations, you should be able to share the client with no perceivable difference.

Orbital answered 19/7, 2019 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.