Jedis Connection Pool: Principles, Configuration, and Troubleshooting
Jedis uses Apache Commons’ GenericObjectPool to manage Redis connections, with Spring‑Data‑Redis creating a JedisConnectionFactory that holds a configurable pool where borrowObject and returnObject handle idle queues, eviction and validation settings (testOnBorrow, testWhileIdle, etc.) can be tuned, and JMX metrics aid troubleshooting.
The article explains the purpose of connection pools and why Jedis uses GenericObjectPool as its underlying pool implementation.
Principle Overview : It describes how Spring‑Data‑Redis creates a RedisConnectionFactory (specifically JedisConnectionFactory ) which internally holds a GenericObjectPool . Several diagrams illustrate the class hierarchy.
Class Structure : The key classes involved are GenericObjectPool , GenericObjectPoolConfig / BaseObjectPoolConfig , and the concrete JedisPoolConfig that overrides some defaults.
Core Methods :
BorrowObject – obtains a free connection from the idle queue, waiting up to maxWaitMillis before creating a new one.
ReturnObject – returns a connection to the idle queue.
Code example for returning/borrowing logic:
final int maxIdleSave = getMaxIdle();
if (isClosed() || maxIdleSave > -1 && maxIdleSave <= idleObjects.size()) {
// destroy object
} else {
// return to idleObjects
}Internal Mechanisms :
Evict (periodic eviction/keep‑alive): driven by timeBetweenEvictionRunsMillis and numTestsPerEvictionRun . Objects idle longer than configured thresholds are removed.
Test (validation): includes testOnBorrow , testOnReturn , testOnCreate , and testWhileIdle . Validation typically sends a PING to Redis and expects PONG .
Abandoned (abandonment): not supported by Jedis but mentioned for completeness.
Eviction policy example (default DefaultEvictionPolicy ):
if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() &&
config.getMinIdle() < idleCount) ||
config.getIdleEvictTime() < underTest.getIdleTimeMillis()) {
return true;
}
return false;Testing Scenarios :
testOnBorrow and testOnCreate should not be enabled in production because they add latency.
testOnReturn also should be disabled in production.
testWhileIdle works together with eviction to keep idle objects alive.
Troubleshooting : The pool registers JMX beans; you can inspect them with tools like JVisualVM or Arthas, e.g.,
mbean org.apache.commons.pool2:type=GenericObjectPool,name=pool-redisConnectionFactoryImportant metrics include CreatedCount , DestroyedCount , and DestroyedByEvictorCount .
In summary, understanding the GenericObjectPool internals helps diagnose connection‑pool‑related issues not only for Jedis but for other pools as well.
DeWu Technology
A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.