Search code examples
gremlintitanjanusgraphgremlin-server

When are gremlin sessions better?


I understand that sessionless operations are the preferred method of using gremlin. I'm wondering when is the sessioned approach better?

So I might be doing something like...

graph.addVertex("foo").property("name","bar")

graph.traversal().V().has("name","bar").as("f").addV("foo").property("name","baz").as("g").addE("test").from("f").to("g")

I'm doing this type of operation a lot. Often there's also a query (usually involving a coalesce) beforehand to check if a node (g in my example) exists, and create it if not.

So I'm wondering if a session might be better because I could hold a handle to the previous vertices and just attach new nodes to them without the expense of the lookup.

Feel free to tell me why I'm wrong in anything else that I'm doing.. Just trying to make things faster.


Solution

  • First of all, I would avoid use of addVertex() and stick to addV() - see more details here.

    As to your question, I think the only time to leverage sessions is if you have some sort of loading operation that requires explicit control over transactions and you're not using a JVM based language. Even then, I might consider other options for dealing with that and just avoid sessions completely. You end up with a less portable solution as there are a number of graph systems which don't even support them directly (e.g. Neptune).

    The cost to do a T.id based lookup should be really fast, so saving a vertex between requests in a session really shouldn't vastly improve performance. Even if you keep the vertex between requests you will still need to pass the vertex into your traversal so you still have the lookup anyway - I'm not sure I see the difference in cost there.

    // first request
    v = g.addV(...).property(...).next()
    
    // second request
    g.V(v).addE(....
    
    // third request
    g.V(v).addE(....
    

    The above should not be that much faster than:

    // first request - returns id=1
    g.addV(...).property(...).id().next()
    
    // second request - where "1" is just passed in on the next request as a parameter
    g.V(1).addE(....
    
    // third request
    g.V(1).addE(....