Maaz Zindani

Loading the index…

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

Jedis fails now and then with Read timed out

ErrorRead timed out

Short answer

Read timed out means Jedis sent a command and the socket got no reply within its timeout. It happens when one command runs long and blocks the server, or when a pooled connection sat idle and was closed on the way. Find slow commands with SLOWLOG, check the server's idle timeout, and retry on a fresh connection.

  • Applies to Jedis 5, 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 the server which commands were slow around the time of the errors. Entries show the duration in microseconds and the command.

    redis-cli SLOWLOG GET 10
  2. 2Match what you see, then jump to its fix

    1. 1SLOWLOG GET lists entries with a long duration at the time of the timeoutsA slow command blocks the server for longer than the client waitsFix
    2. 2The first command after a quiet period fails, the next one worksAn idle pooled connection was closed by the server or the networkFix

What does the error look like?

  • Read timed out
  • JedisConnectionException Read timed out

Cause 1 of 2

A slow command blocks the server for longer than the client waits

1Check

Redis runs commands one at a time. A command that takes long (a big key read, a large range or a bulk delete) delays every other client, and their reads time out. The slow log records commands that run longer than slowlog-log-slower-than.

redis-cli CONFIG GET slowlog-log-slower-than
redis-cli SLOWLOG GET 10

2Fix

Replace the slow command: split big reads into smaller ranges or scans, and delete big keys in pieces. Only after that, raise the client timeout if the command must stay.

# find the keys that are big
redis-cli --bigkeys -i 0.1
# list keys without blocking the server
redis-cli --scan --pattern 'cache:*' | head

3Verify

New slow log entries stop appearing and the timeouts go away.

redis-cli SLOWLOG GET 5

Confirmed by redis.io

Did this fix it?

Cause 2 of 2

An idle pooled connection was closed by the server or the network

1Check

If the Redis timeout setting is above 0, the server closes connections that stay idle that long. A pooled connection then looks open to Jedis but is dead. The default is 0, so look at your own value.

redis-cli CONFIG GET timeout

2Fix

Make the pool test idle connections, so dead ones are dropped, and retry once on a new connection when a command fails with a connection error.

poolConfig.setTestWhileIdle(true);
poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(30));

// retry: ask the pool for a connection again
try {
    jedis.set(key, value);
} catch (JedisConnectionException e) {
    jedis.set(key, value);
}

3Verify

Leave the app idle past the old failing time, then send a command.

redis-cli INFO clients | grep connected_clients

Confirmed by redis.io

Did this fix it?

How do I stop it coming back?

  • Alert on SLOWLOG entries so slow commands are fixed before clients time out.
  • Keep values small and read big collections in pieces.
  • Keep tcp-keepalive on and set the pool to test idle connections.
  • Retry connection errors a limited number of times with a short backoff.

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.