lettuce icon indicating copy to clipboard operation
lettuce copied to clipboard

single StatefulRedisConnection to support different database by mutiple threads

Open ywtsang opened this issue 8 years ago • 3 comments

I am using 5.0.1.RELEASE with reactive api.

I need to connect to the same redis server with multiple databases.

The databases are in predefined range (e.g. 1...15) but the databases to connect are not known in init, instead, it is determined by each redis method call (get/set/etc)

For now, I create (and cache) dynamically a RedisClient per database from method parameter to create a new connection.

But I find this approach to have different problems (concurrent access, unnecessary multiple connections created per database).

It looks like RedisReactiveCommands.select can be used to change database , so I have questions:

  • will it have problem if I only create one StatefulRedisConnection to be shared by multiple threads , each thread call "StatefulRedisConnection#reactive#select" first, then use the same "reactive" to proceed to execute command?

  • will "StatefulRedisConnection#reactive#select" issue a separate network roundtrip?

ywtsang avatar Feb 05 '18 11:02 ywtsang

Lettuce is thread-safe per se, but if you start changing the connection state (Transaction, SELECT) without synchronizing the calling code, you might mess up the state.

If you share a connection and call SELECT before each command, then the connection will change the database but a subsequent command is not guaranteed to hit the same database as SELECT and the following commands do not represent an atomic unit of work.

I'd propose to keep a single RedisClient instance and create then a connection for each database that you store in e.g. Map<Integer, StatefulRedisConnection>. You don't create additional roundtrips by calling SELECT before each command and you minimize the number of connections/client instances to the really needed ones.

You would lookup the connection by database number and then operate in the connection. You are guaranteed to hit the appropriate database and benefit from thread-safety as you're not changing the connection state.

mp911de avatar Feb 05 '18 12:02 mp911de

Thanks @mp911de , this clarifies my questions.

ywtsang avatar Feb 06 '18 02:02 ywtsang

Hello @mp911de. So you suggest to use single RedisClient and obtain numerous StatefulRedisConnection<K, V from it, then call select(int) on each connection and store it within a map for later use?

What happens in case a sentinel is used and physical connection of StatefulRedisConnection<K, V> obtained via RedisClient.connect(RedisCodec<K, V>) fails - replica is promoted to master. I expect that an executed command on StatefulRedisConnection<K, V> will not fail (new physical connection to the new master is created and attached to the StatefulRedisConnection<K, V>) and will be processed on a new physical connection. In such a case, how does Lettuce handle selected database from a previous connection? ~~If there is no such a handling, your suggestion is not correct I think.~~

I would expect, that I have to specify multiple RedisClients and their own RedisURI objects with database number set to specific database. So every new connection will use its own default database.

Thank you for your reply.

EDIT: I have mad a few tests and it looks like the Lettuce remembers the very last selected database on a StatefulRedisConnection<K, V> and set it after successful reconnection to a new master.

little-fish avatar Aug 31 '20 10:08 little-fish