I’m developing a typical API server (Node.js with Apollo), and I’m using the JavaScript gRPC client to interface with Dgraph. I’d like to understand how to properly manage the lifecycle of Dgraph connections.
I was specifically thinking about connection pooling, but according to this discussion it seems connection pooling is not necessary:
You don’t need to create a new connection for every request. You can just create one connection, and use it for many concurrent requests.
So are the following steps the “right way” to create and use one connection?
-
At application startup, create a stub and client:
import { DgraphClient, DgraphClientStub } from 'dgraph-js' const stub = new DgraphClientStub('localhost:9080') const client = new DgraphClient(stub)
-
When an incoming request arrives, use the existing
client
to perform the necessary Dgraph queries/mutations (i.e. keep this oneclient
in memory and pass it around to whatever needs it). -
At application shutdown, close the
stub
:stub.close()