1

I've been trying to find information about Cassandra sessions relating to the Node.js cassandra-driver by Datastax. I read something which said that cassandra-driver automatically manages a session and that I don't need to call client.shutdown().

I'm looking for general information about how cassandra-driver manages sessions, how can I see all active Cassandra sessions, and do I need to call shutdown() or is that counter productive having to reopen a session every time the script is run?

Based on "pm2 info" I don't see a ton of active handles so I don't think anything wrong is going on but I may be mistaken. Ram usage does seem a bit high for a small script (85mb).

  • What is the source of the claim that you don't need to close the connection? I could only found issues reported when it was not explicitly set to close the connection, like https://stackoverflow.com/questions/28624130/after-inserting-data-into-cassandra-with-node-js-the-program-seems-like-it-is-s#28628569 – Carlos Monroy Nieblas Aug 04 '19 at 04:30
  • I'm struggling to find it now. I'll keep looking. – somedbguy12 Aug 04 '19 at 20:27

2 Answers2

2

In the DataStax drivers, Session is a stateful object handling a pool of connections and aware of the status of nodes in the Cluster at any time (avoiding sending request to unavailable node). TCP sockets are opened and it is a best practice to close when you don't need it anymore. See here to get more infos : https://docs.datastax.com/en/developer/nodejs-driver-dse/2.1/features/connection-pooling/

Now session.connect() may takes a bit of time: the more nodes you have in your cluster, the longer it will be to open connections to every single one. This is the reason why, it is better to init connections in a "cold start" when you work with FAAS (avoiding to open/close for each request)

So:

  1. Always close your connections (shutdown()) when you don't need it anymore (shutdown hook in your applications)

  2. Keep your connections "alive" as long as you need it, do not shutdown for each request, this is NOT stateless.

clunven
  • 1,360
  • 6
  • 13
0

yes, it is "better" to connect the client outside of the handler function. to keep it state-Full.

however, AWS Lambda with nodeJS, by default function execution continues until the event loop is empty or the function times out.

create the client outside of handler, set the context.callbackWaitsForEmptyEventLoop = false and don't call client.shutdown.

Paul Rysevets
  • 39
  • 1
  • 4