Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Connection pool with Jedis

Tags:

I am using Jedis to connect with a Redis server in a REST service.

When I am calling the web service I want to do operations like jedis.hmget , jedis.exits and hgetALL.

For example:

jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0);

The configuration that I am using for Redis is:

Jedis jedis;

    JedisShardInfo shardInfo;

    @PostConstruct
    public void init() {

        try {

            shardInfo = new JedisShardInfo(Config.getRedisHost(), Config.getRedisPort());
            shardInfo.setPassword(Config.getRedisPassword());
            jedis = new Jedis(shardInfo);
            jedis.select(2);
        //jedis.se
        } catch (Exception e) {
            logger.error("Exception in init ------- > " + e);
        }

    }

I know that Jedis is NOT thread safe. When I am using 1000 threads at once to call the service at that time I am getting an exception as Unexpected end of stream. I want to know Jedis pool is thread safe? Unable to find a specific solution for it.

Thanks. Any Help would be appreciated.

like image 387
Abhijeet Behare Avatar asked Jun 15 '17 14:06

Abhijeet Behare


People also ask

What is Jedis connection pool?

Use JedisPool This allows you to talk to redis from multiple threads while still getting the benefits of reused connections. The JedisPool object is thread-safe and can be used from multiple threads at the same time. This pool should be configured once and reused.

How do I make a Jedi connection?

The Java CodeJedis jedis = new Jedis("localhost"); // prints out "Connection Successful" if Java successfully connects to Redis server. Here is a breakdown of the above code: Jedis jedis = new Jedis("localhost"); This connects our Java to Redis server running on our local host.

Does Redis need connection pooling?

If we create a separate Redis connection for a thread it will be overhead for the Redis server. We need to have a pool of connections in multi-threaded environments to address these challenges. This allows us to talk to Redis from multiple threads while still getting the benefits of reused connections.


1 Answers

JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", portno, 10000,
            "password");

See here: https://github.com/xetorthio/jedis/wiki/Getting-started

like image 53
Radhika Mantri Avatar answered Sep 30 '22 01:09

Radhika Mantri