在C#后端应用程序中是否可以有两个Cassandra会话?

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

Can I have two Cassandra Sessions in C# backend application?

问题

我需要在我的后端与两个Cassandra集群交流,保持稳定的环境。然而,在测试阶段,我只有一个集群,它的配置是重复的,因此在启动期间我们创建了两个会话。

一个集群是否可以有两个会话?

此外,我们有多个keyspaces,但只有一个连接。我应该为每个keyspace创建新的会话吗?

我了解到会话应该是单例,但我认为这不是一个要求,而是一个建议。

英文:

I need to talk to two Cassandra clusters in my backend at stable environment. However, in beta I have only one cluster and it's config is duplicated, so during startup we create two sessions.
Is it ok to have two sessions for one cluster?

Also we have multiple keyspaces, but only one connection for them. Should I make new session for each keyspace?

I see that session should be singleton, but I think it's not a demand, but a recommendation.

答案1

得分: 0

以下是翻译的部分:

"每个应用程序仅创建和重复使用单个会话的建议是因为会话的创建非常昂贵。"

"每次创建会话时,驱动程序必须经历其标准的初始化过程并打开到集群中每个节点的连接池。除了显著增加内存使用量之外,这将使您的应用程序变得缓慢而毫无好处。"

"为每个键空间创建会话毫无意义,因为会话可以同时处理数千个请求。您只需要在查询中引用表时指定键空间,例如:"

"从 keyspace_name.table_name WHERE ...''

"正如您所指出的,从技术上讲,没有阻止应用程序创建多个会话的障碍。但这样做也没有任何好处,只会带来很多不利之处,因此我们不建议这样做。祝好!"

英文:

The recommendation to only create and reuse a single session per application is because session creation is very expensive.

Each time a session is created, the driver has to go through its standard initialisation process and open connection pools to every node in the cluster. Apart from the significant increase in memory usage, this will slow down your application for no benefit.

It makes no sense to create a session for each keyspace since the session can handle thousands of requests concurrently. All you need to do is to specify the keyspace when referring to a table in the query, for example:

SELECT ... FROM keyspace_name.table_name WHERE ...

As you pointed out, there is no technical barrier that prevents your application from creating multiple sessions. But there is also no benefit to doing so, just a lot of disadvantages so we don't recommend it. Cheers!

huangapple
  • 本文由 发表于 2023年2月9日 00:01:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388475.html
匿名

发表评论

匿名网友

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

确定