Best practice for DgraphClientStub, DgraphClient lifetime

Hi @clintwood,
The standard practice to use DgraphClient is inspired by the GRPC spec itself. In short, there is no need for creating new connections every time you try to connect to a GRPC server. There are a few reasons behind it:

  1. This approach is error-prone. If connections are not closed properly, it could lead to memory issues as more and more connections are getting created in the application. The eventual state would be OOM’s.
  2. In a highly available and concurrent system, this could lead to too many connections over the GRPC server which could lead to increased network latency and maybe downtime if maximum connection limits reached.

Just to set up a context, GRPC’s leverages HTTP2 under the hood. Besides introducing binary framing and compression in HTTP2, one of the key enhancement was multiplexing (none before Http1.1, limited in Http1.1). This allows concurrent systems to connect GRPC server over one single connection. Of course, you can manage your connections through call options like idleTimeout or keepAliveWithoutCalls. Available call options are listed here.

I request you to please go through this and this to understand GRPC better. You can find official documentation with core concepts here.