StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.87k stars 1.51k forks source link

Transaction over multiple Databases #2416

Open kevinvenclovas opened 1 year ago

kevinvenclovas commented 1 year ago

Hi,

i want to split my datas in multiple Databases. To keep it safe i want to add or delete datas only in an transaction.

I found multiple code snippet like this:

using var con= await ConnectionMultiplexer.ConnectAsync("127.0.0.1:6379");
var tran= con.CreateTransaction();

var db1= con.GetDatabase(1);
var db2= con.GetDatabase(2);

... Add or Delete on db1 and db2

await tran.ExecuteAsync();

But there is no function CreateTransaction on the ConnectionMultiplexer object. This function is only available on the IDatabase but here i need to set the ID of the database. I think the transaction is only available on the single Database and not on all other.

Here is my Code. How can i add an transaction to keep it safe?

using var con = await ConnectionMultiplexer.ConnectAsync("127.0.0.1:6379");

var db1= con.GetDatabase(1);
var db2= con.GetDatabase(2);

... Add or Delete on db1 and db2
mgravell commented 1 year ago

There isn't an API for that today. I don't think this is a technical redis-server limitation - just an API reality that it doesn't fall out of the existing model well. The library will actively fight attempts to subvert it via ScriptEvaluate (Lua) / Execute (raw commands), and will switch you right back to where it expected you to be.

So: a client limitation. I'll try to think of there are good ways of getting around this.