1

We are using Redis v3.2.100 windows version for caching in a Spring Framework project. Redis related dependencies:

<dependency>
     <groupId>org.springframework.data</groupId>
     <artifactId>spring-data-redis</artifactId>
     <version>1.6.6.RELEASE</version>
</dependency>
<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-pool2</artifactId>
     <version>2.4.2</version>
</dependency>
<dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>2.8.0</version>
</dependency>

And config:

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" p:max-total="400" p:maxIdle="350" p:maxWaitMillis="1000"
         p:test-on-borrow="true" p:test-on-return="true" p:testOnCreate="true" />

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
     p:host-name="127.0.0.1" p:port="6379" p:use-pool="true" p:password="11223344">
   <constructor-arg ref="jedisPoolConfig"/>
</bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
     p:connection-factory-ref="jedisConnectionFactory" p:enable-transaction-support="true"/>

And the usage sample in Kotlin:

@Resource(name = "redisTemplate")
private val redisLongKeyStrValueHashOps: HashOperations<String, Long, String>? = null

...
{
    ...
    redisLongKeyStrValueHashOps!!.get("RepoName", 111L).toString()
    ...
}

But there is a problem, sometimes Spring Application cannot work with Redis and connection of server with Redis lost. If we check the status of Redis at that moment, the connection count is around 1600 and, if we restart Spring Application, connection count return to zero! and everything becomes fine!

s7vr
  • 73,656
  • 11
  • 106
  • 127
Ali Ahm...
  • 195
  • 1
  • 2
  • 9

3 Answers3

2

It sounds to me that your connection pool is getting exhausted,

May be you can try auto wire redisTemplate directly and then use execute method with callbacks, like:

template.execute(new SessionCallback<List<Object>>() {
 //...
});

Or

template.execute(new RedisCallback<Void>() {
// ...
}

Please see this question for more info

madz
  • 1,803
  • 18
  • 45
1

Use lettuce with a single persistent connection. It is thread safe. The only problem is that you need to be able to recreate connection if it gets corrupted for some reason.

Imaskar
  • 2,773
  • 24
  • 35
0

It sounds like problem of your idle connections. I saw your maxIdle is 350 and usually it should be around 10-60 for single redis server and a single redis client. Besides, there is no others in your config for idle connections.

Try these config below:

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- max connections -->
        <property name="maxTotal" value="30" />
        <!-- max idle connections -->
        <property name="maxIdle" value="10" />
        <!-- max released connections each time -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- time interval of releasing connection scan (ms) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- min connection idle time -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- time interval to release for idle connection, 
        when the number of  idle connection is bigger than 'maxIdle' and reaches this time
        it would be realsed inmediately-->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- max waiting time of getting connection, less than zero means uncertain time, default -1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- test connection work when get connection, default false -->
        <property name="testOnBorrow" value="true" />
        <!-- test idle connection work, default false -->
        <property name="testWhileIdle" value="true" />
        <!-- if it is blocked when connection is exhausted, false throws exception, true blocked until timeout, default true-->
        <property name="blockWhenExhausted" value="false" />
    </bean>
wl.GIG
  • 306
  • 1
  • 2
  • 13
  • @wI.GIG - Request count per second to my server is high and the execution time of each one is about 400ms. During processing a request I have many interactive with Redis server. According to this info, do you want to make any change in the recommended config? – Ali Ahm... Jul 21 '19 at 06:12