AT

Reusing connections

DCDragos Ciupureanu11/8/2023
Hi, I'm wondering what's the recommended way of using connections to a graph DB. The documentation uses web sockets like so (in javascript)
const gremlin = require('gremlin');
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;

const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
const gremlin = require('gremlin');
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;

const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
but I was wondering if there's a way of opening and managing such connections in an app that uses the repository pattern. I have a frontend that runs queries every now and then and opening a new websocket connection every time I want to run a query doesn't seem too efficient, hence thinking of something like a connection pool managed by the driver (similar to what JDBC drivers do). Is the current advice to have one connection (DriverRemoteConnection) and multiple traversals using that connection or multiple connections, one for each traversal?
Solution:
Connection pooling in Javascript is a bit of an oddity, since the language has mostly been intended for single-threaded purposes (running in a browser). With that, the websocket library native to Javascript hasn't traditionally supported connection pooling natively. In testing we've done within AWS, the Javascript websocket library is by far the most efficient in creating a new connection (single digit milliseconds). Whereas something like Python's implementation can be really expensive (10s of milliseconds, or worse). So maintaining long-lived connections in Javascript is likely not as big of a deal as it is in other runtimes. (My opinion, so take this with a grain of salt). You should really avoid using websockets until you're faced with a use case that would benefit from them. There's a lot of dev overhead in creating and maintaining websocket connections. Websocket connections are also problematic in distributed compute implementations (or for high availability) as they act like "sticky sessions". They can also cause problems with load balancing logic. I would suggest using http requests for as long as you can before relying on the use of websockets. There is obviously the tradeoff of serialization when it comes to using Gremlin with http vs websockets, but that is something you should be able to handle once and be done with it....
Jump to solution
DCDragos Ciupureanu11/8/2023
To add more context to this: I'm writing a web app that needs to update the graph on events happening inside the web app. For this I'm currently abstracting away the graph by creating a service that takes a gremlin query as string in the form of
g.V(<my_id_here>).out().unfold()
g.V(<my_id_here>).out().unfold()
but those string queries become hard to maintain and debug since there's little to no IDE that supports gremlin syntax (or even knows of). Looking at the example given abot I see how that works in an "gremlin 101" way but I'm wondering if that's the correct approach if I still want to expose the functionality of gremlin through a service that abstracts away all the plumbing required to connect to the database and that only acceps a "query" as input.
Solution
Ttriggan11/9/2023
Connection pooling in Javascript is a bit of an oddity, since the language has mostly been intended for single-threaded purposes (running in a browser). With that, the websocket library native to Javascript hasn't traditionally supported connection pooling natively. In testing we've done within AWS, the Javascript websocket library is by far the most efficient in creating a new connection (single digit milliseconds). Whereas something like Python's implementation can be really expensive (10s of milliseconds, or worse). So maintaining long-lived connections in Javascript is likely not as big of a deal as it is in other runtimes. (My opinion, so take this with a grain of salt). You should really avoid using websockets until you're faced with a use case that would benefit from them. There's a lot of dev overhead in creating and maintaining websocket connections. Websocket connections are also problematic in distributed compute implementations (or for high availability) as they act like "sticky sessions". They can also cause problems with load balancing logic. I would suggest using http requests for as long as you can before relying on the use of websockets. There is obviously the tradeoff of serialization when it comes to using Gremlin with http vs websockets, but that is something you should be able to handle once and be done with it.
DCDragos Ciupureanu11/9/2023
Great, thanks for the nice explanation. Since the volume of queries in my app is not that hight using http for now should be fine.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Can I surpress gremlin console's warnings?How can I surpress these WARNING messages? I've tried gremlin -l but can't seem to get the syntax rSequential IDs in Neptune?@neptune I'm attempting to implement sequential IDs for the vertices in our AWS Neptune graph. So Gremlin console vs REST APII'm trying to get a path and the properties of the vertices and the edges for that path by running aCryptic Neptune Gremlin Error Rate Creeping - What Would You Recommend?This relates more to do with Neptune usage, nevertheless, it is also related to the Gremlin Query erkubehoundIf anyone is familiar with KubeHound DSL. Can someone explain why Query 1 is different from Query 2.Help with visualizing in the graph-notebookI am trying to visualize a graph in the graph-notebook but no matter what I do I cannot get it to beGremlin browser code editorHi, I'm looking for a code editor like monaco https://microsoft.github.io/monaco-editor/ to embed inConnecting to local gremlin server with websocket addressHello everyone. I'm looking for help with a client app written in Java that uses Tinkerpop Gremlin tClarification on Kerberos configuration for Gremlin DriverI'm a little bit unclear on the role of the JAAS configuration file for the Gremlin client in the coGremlin Driver and frequently changing serversIn a containerised environment, hosts are frequently replaced and their IP address can change severaGlobal SearchIs there a way where i can scan all the vertex or edge properties that match a given keyword in gremGraphSON mapperHi, I'm trying to ingest some data into AWS Neptune and due to its size I'm forced to use a bulk d