Connection management

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?

  1. 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)
    
  2. When an incoming request arrives, use the existing client to perform the necessary Dgraph queries/mutations (i.e. keep this one client in memory and pass it around to whatever needs it).

  3. At application shutdown, close the stub:

    stub.close()
    
1 Like

Yup, this approach will work. You can pass the client connection around. The connection itself is OK to share across your app.

What you don’t want to do is pass newTxn() objects around concurrently, as each txn has its own txn context.

1 Like