Report this

What is the reason for this report?

Redis client: custom connections manager

Posted on June 30, 2017

I am using spring’s JedisConnectionFactory to manage and connect to Redis from my Java application. JedisConnectionFactory internally uses JedisPool and GenericObjectPool.

Our ask is to create new Redis connections as the pool size exhausts or crosses a certain threshold.

The latest versions of GenericObjectPool does not have option of growing pool size dynamically. I also could not find a way to pass a custom object pool in Jedis.

Is there a way to do this at client side using Jedis ?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

In case someone else stumbles upon the question, unfortunately, there is no built-in way to dynamically grow the connection pool size in Jedis or JedisConnectionFactory. As you mentioned, the latest version of GenericObjectPool does not have this capability, and JedisConnectionFactory does not provide an option to pass a custom object pool.

However, you can implement your own dynamic connection pool growth logic using a combination of Jedis and some custom code. Here’s an approach you can consider:

  1. Create a custom ConnectionPool class that extends JedisPool and adds dynamic pool growth logic. For example, you can override the getResource() method to check if the pool size has reached a certain threshold, and if so, create a new connection and return it instead of one from the pool. You can also implement a check-in method that releases a connection back to the pool and checks if the pool size can be reduced.

  2. In your application code, create an instance of your custom ConnectionPool and pass it to JedisConnectionFactory using the setPoolConfig() method. This will configure JedisConnectionFactory to use your custom connection pool.

Here’s an example implementation of a custom ConnectionPool class:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class DynamicConnectionPool extends JedisPool {

    private static final int DEFAULT_POOL_SIZE = 10;
    private static final int MAX_POOL_SIZE = 100;

    public DynamicConnectionPool(String host, int port) {
        this(host, port, DEFAULT_POOL_SIZE);
    }

    public DynamicConnectionPool(String host, int port, int poolSize) {
        super(new JedisPoolConfig(), host, port);
        this.internalPool.setMaxTotal(poolSize);
    }

    @Override
    public Jedis getResource() {
        Jedis jedis = null;
        if (this.getNumIdle() == 0 && this.getNumActive() < MAX_POOL_SIZE) {
            jedis = new Jedis(this.getHost(), this.getPort());
        } else {
            jedis = super.getResource();
        }
        return jedis;
    }

    public void checkIn(Jedis jedis) {
        if (this.getNumActive() > this.internalPool.getMaxIdle()) {
            jedis.close();
        } else {
            super.returnResource(jedis);
        }
    }
}

In this example, the DynamicConnectionPool class extends JedisPool and overrides the getResource() method to create a new Jedis instance if the pool size has reached a certain threshold (MAX_POOL_SIZE). The checkIn() method releases a connection back to the pool and checks if the pool size can be reduced.

You can then use the DynamicConnectionPool class in your application code like this:

DynamicConnectionPool pool = new DynamicConnectionPool("localhost", 6379, 20);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setPoolConfig(pool);

This will configure JedisConnectionFactory to use your custom connection pool with a maximum size of 20. You can adjust the pool size and threshold values as needed for your specific use case.

I hope this will help

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.