Search code examples
javamultithreadingservletsredislettuce

One Lettuce Redis connection per servlet?


In a servlet, should Lettuce Redis connections be created in init() and shutdown in destroy() or should a connection be created for every request (in doPost or doGet?)

I am using sync RedisCommands (connection.sync())

Lettuce states:

Lettuce is thread-safe by design which is sufficient for most cases. All Redis user operations are executed single-threaded. Using multiple connections does not impact the performance of an application in a positive way. The use of blocking operations usually goes hand in hand with worker threads that get their dedicated connection. The use of Redis Transactions is the typical use case for dynamic connection pooling as the number of threads requiring a dedicated connection tends to be dynamic. That said, the requirement for dynamic connection pooling is limited. Connection pooling always comes with a cost of complexity and maintenance.


Solution

  • it depends on a scenario you are chasing. If you intend to use redis transactions, then you have to make sure ongoing transaction connection is not shared. If not, then one connection will be sufficient for most cases, since redis is single threaded and all your commands will be waiting in a command queue anyway.