grpcgrpc-java

Multiple channel vs single channel in grpc java


Trying to understand which is better in terms of lower resource footprints, having a multiple channel and one stream per each channel (different channel to create different stubs and calling RPCs on that stub) or single channel(shared) with multiple streams.

According to some of the best practises(found online), it is recommended to use single channel and share it across to call many RPCs(streams).

  1. Does it depend on one's usecase, like in one use case multiple channel and single stream per channel works efficiently and in other single channel with multiple streams..
  2. Is there any benchmark results (for general use case) that upholds which one is better.

Solution

  • A channel represents a virtual connection to an endpoint and while you can create multiple of them that point to the same endpoint, you will just add extra setup cost and memory consumption.

    The channel will handle the physical connection to the backend (or multiple physical connections if you are load balancing) and multiplexing your requests over that connection. Your client will be more efficient if you reuse a single channel for all your RPCs to an endpoint.

    I'm not aware of any benchmarks for testing the two scenarios, but I also can't think of a reason why creating multiple channels would improve your client performance.