We are working on changing the database of our application from Oracle to Cassandra. We are using Datastax c/c++ driver to connect to cassandra database. In the application, multiple queries will be executed at same time on the tables of same Keyspace. We are planning to define a singleton class to get the session for a cluster and use it across the application. But will this have a impact if multiple queries are executed by the same session at the same time. Can any one please suggest a good way to achieve this. Thanks in advance.
1 Answers
It will not. In effect, it is recommended that you only use 1 session per keyspace. As referenced in 4 simple rules when using the DataStax drivers for Cassandra, the first two rules apply here:
- Use one Cluster instance per (physical) cluster (per application lifetime)
- Use at most one Session per keyspace, or use a single Session and explicitely specify the keyspace in your queries.
A Session comprises of a set of connections to your cassandra hosts. If you have multiple queries in different threads sharing this session, it is ok as it is thread safe and the connections will be shared. As referenced in the Architecture doc for the cpp driver:
CassSession is designed to be used concurrently from multiple threads. It is best practice to create a single session per keysapce. CassFuture is also thread safe. Other than these exceptions, in general, functions that modify an object's state are not thread safe. Objects that are immutable (marked const) can be read safely by multiple threads. None of the free functions can be called conncurently on the same instance of an object.
Creating additional Sessions won't really provide you more value. If you are running into issues where you are overwhelming your connections, you can configure more through your configuration.