Jedis runs out of pooled connections
Cannot get jedis connection: cannot get resource from poolShort answer
A Jedis pool hands out a fixed number of connections, 8 by default. The error means every one is in use and none came back in time. Either code borrows a connection and never closes it, or the pool is too small for the number of threads. Close what you borrow and size the pool.
Fix it fast
1Find out which cause you have
Look at the Redis side: how many clients are connected right now. A count stuck at the pool size while traffic is low points at leaks.
redis-cli INFO clients | grep connected_clients2Match what you see, then jump to its fix
What does the error look like?
Cannot get jedis connection: cannot get resource from poolcannot get resource from pool
Cause 1 of 2
Connections are borrowed and never given back
1Check
When code closes a pooled connection, it is not really closed: it goes back into the pool. A path that skips close (an exception before it, a missing finally) keeps that connection busy for good.
grep -rn "getResource()" src/main/java2Fix
Borrow inside try-with-resources so the connection always returns, also on an exception.
try (Jedis jedis = pool.getResource()) {
jedis.set("key", "value");
}3Verify
connected_clients stays flat while the app handles traffic.
watch -n 2 "redis-cli INFO clients | grep connected_clients"Confirmed by redis.io
Cause 2 of 2
More threads need a connection than the pool allows
1Check
The commons-pool defaults are maxTotal 8, maxIdle 8 and minIdle 0. A busy server with more concurrent requests than that makes the extra ones wait, and they fail when the wait runs out.
grep -rn "PoolConfig\|setMaxTotal" src/main2Fix
Set maxTotal to the number of threads that talk to Redis at the same time, let callers wait a bounded time, and keep idle connections healthy.
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(64);
poolConfig.setMaxIdle(64);
poolConfig.setMinIdle(8);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setMaxWait(Duration.ofSeconds(2));
poolConfig.setTestWhileIdle(true);
RedisClient jedis = RedisClient.builder()
.hostAndPort("localhost", 6379)
.poolConfig(poolConfig)
.build();3Verify
A load test at your real concurrency finishes without the exception.
redis-cli INFO stats | grep -E 'total_connections_received|rejected_connections'Confirmed by redis.io
How do I stop it coming back?
- Use try-with-resources for every pooled connection.
- Create one pool per application, not one per request.
- Set maxTotal from real concurrency and test it under load.
- Keep Redis maxclients above the sum of all application pools.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Jedis fails now and then with Read timed out
JedisConnectionException Read timed out
Read timed out means Jedis sent a command and the socket got no reply within its timeout.
2 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
The AWS SDK for Java cannot find credentials, and AwsCredentials.properties does nothing
software.amazon.awssdk.core.exception.SdkClientException: Unable to load credentials from any of the providers in the chain
AwsCredentials.properties on the classpath is an old convention: the default provider chain of the AWS SDK for Java does not read it.
4 causes, each with a fixBack end
Celery in a Django project does not connect to Redis or ignores the broker URL
kombu.exceptions.InconsistencyError: ... _kombu.binding.celery
Celery talks to Redis through a client library and reads its broker URL from your Django settings.
4 causes, each with a fixBack end
Gradle runs no Cucumber scenarios from my feature files
Feature files exist under src/test but nothing runs
Cucumber runs on the JUnit Platform, so Gradle must call useJUnitPlatform() and have the cucumber-junit-platform-engine on the test classpath.
4 causes, each with a fixBack end
req.session is undefined in an Express route
TypeError: Cannot set properties of undefined (setting 'user')
express-session only attaches req.session when its middleware runs first and can reach its store.
3 causes, each with a fix