Jedis fails now and then with Read timed out
Read timed outShort 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.
Fix it fast
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 102Match what you see, then jump to its fix
What does the error look like?
Read timed outJedisConnectionException 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 102Fix
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:*' | head3Verify
New slow log entries stop appearing and the timeouts go away.
redis-cli SLOWLOG GET 5Confirmed by redis.io
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 timeout2Fix
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_clientsConfirmed by redis.io
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
Jedis runs out of pooled connections
cannot get resource from pool
A Jedis pool hands out a fixed number of connections, 8 by default.
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