Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Jedis cannot borrow a connection from its pool

Errorredis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool

Short 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.

  • Applies to Jedis 7.2, Redis 7.4, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 maxclients
  2. 2Match what you see, then jump to its fix

    1. 1Threads 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. 2connected_clients on the server is at or near maxclients, usually with many app instancesRedis has reached maxclientsFix
    3. 3The first command after a quiet period fails; later ones workIdle connections are closed by the server or the networkFix

What does the error look like?

  • redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
  • Caused 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 -l

2Fix

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_clients

Confirmed by redis.io

Did this fix it?

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 20000

3Verify

connected_clients falls below the limit and new connections succeed.

redis-cli INFO clients

Confirmed by redis.io

Did this fix it?

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-keepalive

2Fix

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 idle

Confirmed by redis.io

Did this fix it?

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

Databases

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.