ScyllaDB Rust best practice

According to ScyllaDB rust documentation session use more resources. So as a developer do I need to keep single separate session for all queries? If yes, how can I do it in rust?

Otherwise need to create session for every rust file if it has a database connection.

As you said, creating the Session object is expensive, because it requires opening a lot of connections to the cluster.
This section in documentation explains all that: Connecting to the cluster | ScyllaDB Docs

It also provides a solution to your problem: you should use Arc<Session> to share it between threads / tasks / objects etc.

Ideally you will create a single Arc<Session> object during your application startup and share it with all the code that needs it.

1 Like

Yeah I saw it, Problem is how to share it and when use different ROLE that will be different sessions for each ROLE so it will be much expensive. So that is why I need to check any better solution.

You are right, that is the valid use case for multiple Session’s - you will need to create a separate Session for each role. I doubt it will cause performance problems - how many roles do you have?

I just keep it as 2 for SELECT and MODIFY.

It will be totally fine to have 2 (or some other amount) sessions instead of one.

The point of our recommendation in the docs is to not do things like session per user request (in case of e.g. web service) or even worse, session per DB statement. Creating N sessions on startup is fine.

1 Like