Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Jedis runs out of pooled connections

ErrorCannot get jedis connection: cannot get resource from pool

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

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

Fix it fast

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

    1. 1The error shows up after the app has been running a while, even at low loadConnections are borrowed and never given backFix
    2. 2Errors appear only under load and go away when traffic dropsMore threads need a connection than the pool allowsFix

What does the error look like?

  • Cannot get jedis connection: cannot get resource from pool
  • cannot 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/java

2Fix

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

Did this fix it?

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/main

2Fix

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

Did this fix it?

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

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 fix

Back 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

Next step

Want this handled for good?

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