Jedis cannot borrow a connection from its pool
redis.clients.jedis.exceptions.JedisException: Could not get a resource from the poolShort answer
The pool could not give the code a working connection in time. The line under it says why. Either every pooled connection is busy because they are not released or the pool is too small, Redis has reached maxclients and is refusing new ones, or something between the app and Redis closes idle connections.
Fix it fast
1Find out which cause you have
Ask Redis how many clients are connected and what the limit is. Then read the Caused by line under the Jedis message: a timeout, a refusal or a closed connection each point to a different cause below.
redis-cli INFO clients redis-cli CONFIG GET maxclients2Match what you see, then jump to its fix
- 1
Threads wait, then fail after the pool's maximum wait time; connected_clients on Redis stays flat at your maxTotalConnections are not released, or the pool is too small for the loadFix - 2
connected_clients on the server is at or near maxclients, usually with many app instancesRedis has reached maxclientsFix - 3
The first command after a quiet period fails; later ones workIdle connections are closed by the server or the networkFix
- 1
What does the error look like?
redis.clients.jedis.exceptions.JedisException: Could not get a resource from the poolCaused by: java.net.SocketTimeoutException
Cause 1 of 3
Connections are not released, or the pool is too small for the load
1Check
Closing a pooled connection returns it to the pool instead of closing it, so any code path that never closes one keeps it busy for good. A new connection is made only while the count is under maxTotal (default 8), and with blockWhenExhausted a caller waits up to maxWait for a free one.
grep -rn 'getResource\|setMaxTotal\|setMaxWait' src/main/java
redis-cli CLIENT LIST | wc -l2Fix
Use the client object from the pool directly, or close each borrowed connection in try-with-resources. Size maxTotal for your real concurrency and set a wait time you can live with.
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(32);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setMaxWait(Duration.ofSeconds(2));
RedisClient jedis = RedisClient.builder()
.hostAndPort("redis.example.com", 6379)
.poolConfig(poolConfig)
.build();3Verify
Run a load test; the number of connected clients stays under your maxTotal and errors stop.
redis-cli INFO clients | grep connected_clientsConfirmed by redis.io
Cause 2 of 3
Redis has reached maxclients
1Check
When Redis cannot accept a new client because the limit is reached, it tries to send an error and closes the connection at once. The default maxclients is 10,000, and the operating system's file-descriptor limit can lower it. Pool size multiplied by app instances must stay well below it.
redis-cli CONFIG GET maxclients
redis-cli INFO clients | grep -E 'connected_clients|maxclients'2Fix
Reduce maxTotal per instance, or raise maxclients and the file-descriptor limit together. Find leaking clients with CLIENT LIST and its idle column.
redis-cli CLIENT LIST
redis-cli CONFIG SET maxclients 200003Verify
connected_clients falls below the limit and new connections succeed.
redis-cli INFO clientsConfirmed by redis.io
Cause 3 of 3
Idle connections are closed by the server or the network
1Check
By default Redis keeps idle clients forever, but a timeout setting closes them after the given number of seconds. Network equipment between app and Redis can also drop quiet connections. Redis enables TCP keepalive by default (about 300 seconds) to help with that.
redis-cli CONFIG GET timeout
redis-cli CONFIG GET tcp-keepalive2Fix
Make the pool test idle connections with PING, so dead ones are removed before they are handed out, and keep the pool's eviction run shorter than the shortest idle limit. Retry the command once with a fresh connection after a JedisConnectionException.
poolConfig.setTestWhileIdle(true);
poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(30));3Verify
Leave the app idle past the old failure time, then send a command.
redis-cli CLIENT LIST | grep -c idleConfirmed by redis.io
How do I stop it coming back?
- Prefer the client class that manages the pool for you and never hold a borrowed connection across slow work.
- Budget connections: pool size times instance count must stay far below maxclients.
- Alert on connected_clients as a share of maxclients.
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
Node.js cannot reach Redis: ECONNREFUSED on port 6379
Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
ECONNREFUSED on 6379 means no Redis server accepted the connection at the address the client used.
4 causes, each with a fixDatabases
Spring Boot cannot connect to Redis and shows the wrong host
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
Spring Boot connects to localhost:6379 unless it finds Redis properties it knows.
2 causes, each with a fixDatabases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
Hibernate cannot find hibernate.cfg.xml at startup
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
Hibernate loads cfg.xml as a resource, not as a file on disk, so a path like /MyProject/src/hibernate.cfg.xml is not a valid location.
2 causes, each with a fixDatabases
EntityNotFoundException when loading an entity that has a missing related row
ConstraintViolationException on an orphaned foreign key
EntityNotFoundException means Hibernate followed an id and no row was there.
3 causes, each with a fixDatabases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fix