springboot-How to solve SocketTimeoutException Read timed out with springboot and redis

1. Introduction

Sometimes, when we use redis in SpringBoot app, we got this Exception:

INFO   | jvm 1    | 2019/07/13 20:09:46 | Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:202) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.jedis.Protocol.process(Protocol.java:151) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.jedis.Protocol.read(Protocol.java:215) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.jedis.BinaryJedis.quit(BinaryJedis.java:253) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at org.springframework.data.redis.connection.jedis.JedisConnection.close(JedisConnection.java:325) ~[spring-data-redis-1.8.9.RELEASE.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       ... 7 common frames omitted
INFO   | jvm 1    | 2019/07/13 20:09:46 | Caused by: java.net.SocketTimeoutException: Read timed out
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_212]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_212]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_212]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_212]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at java.net.SocketInputStream.read(SocketInputStream.java:127) ~[na:1.8.0_212]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196) ~[jedis-2.9.0.jar:na]
INFO   | jvm 1    | 2019/07/13 20:09:46 |       ... 14 common frames omitted

2. Environments

  • SpringBoot 1.x and 2.x

3. The solution

3.1 Increase the springboot redis connection timeout

You can increase the timeout of redis with this property:

spring.redis.timeout=30000 

This means the spring redis timeout is 30 seconds.

4. Summary

You can view the complete springboot properties from this document.