javareddison

How can I create Redisson Client to connect to a redis


I am not sure how to create a RedissonClient object in the application. 1- Should this object be created for every transaction or 2- The object should singleton per JVM? 3- Only one object for all the services?

Currently, my setup is an embedded Jetty with Jersey APIs for 3 Services running on a different set of Kubernetes pods and Redis setup is 3 + 3 master and slave config.

Current config looks like this.

public class RedisTemplate {

public static final RedissonClient REDISCLIENT;
private static final Logger logger = LogManager.getLogger(RedisTemplate.class);

public static final String redisMaster = "redis-cluster";

static {
    Config config = new Config();

    logger.info("redis config for server");

    config.useClusterServers().addNodeAddress("redis://" + redisMaster + ":6379");

    REDISCLIENT = Redisson.create(config);

}

}


Solution

  • As stated in their FAQ, redisson should be singleton:

    Q: When do I need to shut down a Redisson instance, at the end of each request or the end of the life of a thread?
    A : Redisson instance requires manual shutdown only if you want to stop using all of its features. It is a common pattern that Redisson starts and stops along with the application. Since it is completely thread safe, you may treat a Redisson instance as a singleton. The shutdown sequence will disconnect all the active connections held in each connection pool, and it will clean up certain types of Redisson objects require a manual destroy action upon disposal, it will then stop the event loops. Please be advised, the entire shutdown process is not instant.