什么是在Java Spring项目中使用Gremlin客户端集群的最佳实践?

huangapple go评论83阅读模式
英文:

What is the best practice for gremlin client cluster in java spring project

问题

我正在使用 Neptune(AWS)图数据库,我的客户端 API 是基于 Java Spring 的。我的应用程序读写数据库。实际上,我们有 2 个用于读写的集群作为 bean。我们正在生成多个遍历,每次提交后决定使用 try with resources 来关闭它。

在每次使用完遍历后,通过 traversal().withRemote(..) 来关闭遍历并重新创建它,这是否是最佳做法?在一个有多个连接的大型项目中,线程中的最佳实践是什么?

英文:

I am using Neptune (AWS) graph data base, and my client api is in java spring. My application read and write into my database. Actually we have 2 clusters for reading and writing as a bean. W e are generating several traversal and after submitting each we decided to close it by using try with ressource.
Is it a best practice to close traversal and recreate it traversal().withRemote(..) ?
In huge project with several connection in one thread what is the best practice?

答案1

得分: 3

如果您的代码运行时间较长,从Java的典型最佳实践(假设您正在使用Gremlin Java客户端)来看,最好的方法是创建一个连接池,然后在您的线程之间共享图遍历源对象(g)。线程将共享连接池。如果您的应用程序是多线程的,为了实现高吞吐量,通常需要的线程数量大约是连接到的Neptune实例的虚拟CPU数量的两倍。如果您的应用程序能够维护状态并且运行时间较长,那么没有必要不断打开和关闭连接。如果您的应用程序更短暂(例如定期启动和停止容器或使用AWS Lambda函数),那么在每次调用时创建和关闭连接通常是最佳实践。如果您的代码运行时间较长(例如在服务器中),那么保持一个连接池处于打开状态并在应用程序的线程之间共享连接通常是最佳方法。这样可以避免每次发出查询时都创建新的连接池的开销。

如果连接在一段时间内处于空闲状态,Neptune可能会关闭它。如果您使用Gremlin Java,客户端会包括一个保持活动的ping。

如果您使用IAM身份验证,连接将在凭据过期后的10天内关闭。

关于Amazon Neptune的WebSocket行为,可以在这里找到一些文档:https://docs.aws.amazon.com/neptune/latest/userguide/limits.html#limits-websockets

关于Java客户端的一些其他相关信息可以在这里找到:https://docs.aws.amazon.com/neptune/latest/userguide/best-practices-gremlin-java-client.html

英文:

If your code is long running the typical best practice from Java (assuming you are using the Gremlin Java client) is to create a connection pool using the client and sharing that graph traversal source object (g) among your threads. The threads will share the connection pool. If your application is multi-threaded, a good number of threads to have to achieve high throughput is approximately twice as many as the number of virtual CPUs on the Neptune instance you are connecting to. There is no need to keep opening and closing the connection if your application is able to maintain state and is long running. If your application is more ephemeral (such as when starting and stopping containers regularly or using AWS Lambda functions) then creating and closing the connection on each invocation is often a best practice. If your code is longer running (such as in a server) then keeping a connection pool open and sharing the connections among threads in your application is usually the best approach. This avoids the overhead of creating a new connection pool each time you issue some queries.

If a connection is idle for a period of time Neptune may close it. If you are using Gremlin Java then the client includes a keep alive ping.

If you are using IAM authentication the connection will be closed after 10 days when the credentials expire.

There is some documentation here on Web Socket behavior with Amazon Neptune: https://docs.aws.amazon.com/neptune/latest/userguide/limits.html#limits-websockets

There is some additional information pertinent to the Java client here: https://docs.aws.amazon.com/neptune/latest/userguide/best-practices-gremlin-java-client.html

huangapple
  • 本文由 发表于 2020年9月8日 15:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63789210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定