RedisCommandTimeoutException intermittently
Hello,I am using the latest version of the lettuce-core on an ElastiCache setup using a single redis Instance and notice that during heavy loads,I am getting the error log as attached. I am using a single synchronous client for performing all my operations.I have configured the redis timeout to be of 2 secs but since the operation is a simple get(),I cannot understand why the time limit should be breached.Any steps on how to proceed with this or help you understand the issue better maybe? Please find logs here https://gist.github.com/bvaidya121393/4965226134151fe0fa4d0482c373a431
- Using Lettuce Version 5.0.3
Thanks for reporting this issue. Have you measured how long the actual command takes and whether the command timeout is legitimate? Did you observe disconnects?
I have queried the redis slowquery log.There are only about 10 entries which have a high querying time.None of which are during the 56 seconds of timespan during which we recieved these errors.No disconnects either during the said interval according to the number of established connections remaining constant for that time period.We have a thread pool defined as 100 though in the application. Can it be due to many threads invoking commands on the single redis client?Or am I barking up the wrong tree?
Lettuce is thread-safe and is expected to handle low and high loads of commands. Are you able reproduce the issue or does that happen sporadically?
We'd need to capture the state of the client/connection to see what's going on and what causes commands to not complete.
It happens sporadically. A correction to my earlier comment.There was a drop in the number of connections during the period I was getting the timeout exceptions and restored to earlier value after the execptions stopped appearing. Is it because the client was not connected that the requests were getting timed out?
We'd need to capture the state of the client/connection to see what's going on and what causes commands to not complete
Any suggestions as to how I can achieve this?Could you suggest a few pointers?
Is it because the client was not connected that the requests were getting timed out?
Yes, this is possible. Lettuce works transparently by default regarding disconnects. It auto-reconnects and commands issued at that time are buffered and replayed on reconnect.
You can tweak options through ClientOptions, specifically disconnectedBehavior (see https://lettuce.io/core/release/reference/#client-options).
We'd need to capture the state of the client/connection to see what's going on and what causes commands to not complete
Applies to the actual investigation. Having a reproducer makes things easier as we can run the reproducer and inspect the state on failure. Connecting a debugger to a high load prod system isn't really feasible.
What we would do during the analysis is checking queued/buffered commands while the timeout happens.
It auto-reconnects and commands issued at that time are buffered and replayed on reconnect.
Just wanted to confirm the behaviour again.If they are buffered and replayed,why should these face a timeout vis-a-vis a request that had,say, been requested during a period when the client was connected and wasn't disconnected during a period of request? If all buffered requests are replayed on a new Redis connection,they should'nt face a RedisCommandTimeoutException,should they?
Ive had a similar issue cause my production system to become unresponsive multiple times over the past few months. I am running the most recent version of spring boot, with embedded tomcat server 8, java 1.8, and spring-data-redis. If I simply restart my embedded tomcat server, it resolves the issue. But this is not acceptable in a production environment; the most recent time this happened under virtually no load. I can usually get about a week of runtime before I get a crash with a generic error:
org.apache.jasper.JasperException: org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out
FYI: I am using AWS cloud services, including their redis cluster. And it's virtually impossible for me to reliably reproduce, other than waiting for it to randomly fail time and again.
@tpthaler Please report such issues way earlier. Can you pull a heap dump in that state and share it with me? Feel free to mail me a dropbox link to not share it publicly.
Hello, Lettuce - 5.0.3.RELEASE We are also facing this problem since couple of days in production and also in test environment. We are using Redis cache in AWS with Replication (1 primary node and 1 replica)
Is there any solution or workaround for this? @mp911de
@akhilkalra without knowing the cause it's not possible to know what you can change/do instead. Happy to receive any heap dumps/logs/whatever helps to diagnose the issue.
Hello,
we ran into similar problem with 5.0.4.RELEASE. The behavior we are seeing is as follows:
- We create connection as shown in the listing below
- One or more threads do
brpopon same key and process the result - After a while elements are popped from the list but not being processed
This is super easy to reproduce when withTimeout() is set very low like 5s.
I believe better way of doing this would be to use timeout in connection.sync().brpop(1, tag); but I don't understand why this is failing in such an unexpected way.
// connection set-up
RedisURI uri = RedisURI
.builder()
.withHost(host)
.withPort(port)
.withClientName(clientName)
.withTimeout(Duration.ofSeconds(5))
.build();
client = RedisClient.create(uri);
connection = client.connect();
// worker thread
while (!stopped) {
try {
KeyValue<String, String> result = connection.sync().brpop(0, tag);
// process result, this is never reached after a while; omitted for brevity
log.info("{}: received {}", Thread.currentThread().getName(), result.getValue());
} catch (Exception e) {
log.error(Thread.currentThread().getName(), e);
}
}
This is what we see in the logs
DEBUG [2018-05-18 17:53:30,705] io.lettuce.core.protocol.CommandHandler: [channel=0x268f5b69, /127.0.0.1:64375 -> localhost/127.0.0.1:6379, chid=0x1] Received: 1179 bytes, 154 commands in the stack
DEBUG [2018-05-18 17:53:30,705] io.lettuce.core.protocol.CommandHandler: [channel=0x268f5b69, /127.0.0.1:64375 -> localhost/127.0.0.1:6379, chid=0x1] Stack contains: 154 commands
DEBUG [2018-05-18 17:53:30,705] io.lettuce.core.protocol.RedisStateMachine: Decode AsyncCommand [type=BRPOP, output=KeyValueOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
DEBUG [2018-05-18 17:53:30,705] io.lettuce.core.protocol.RedisStateMachine: Decoded AsyncCommand [type=BRPOP, output=KeyValueOutput [output=KeyValue[retracted, {"retracted"}], error='null'], commandType=io.lettuce.core.protocol.Command], empty stack: true
DEBUG [2018-05-18 17:53:30,706] io.lettuce.core.protocol.CommandHandler: [channel=0x268f5b69, /127.0.0.1:64375 -> localhost/127.0.0.1:6379, chid=0x1] Stack contains: 153 commands
DEBUG [2018-05-18 17:53:30,706] io.lettuce.core.protocol.RedisStateMachine: Decode AsyncCommand [type=BRPOP, output=KeyValueOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
DEBUG [2018-05-18 17:53:30,706] io.lettuce.core.protocol.RedisStateMachine: Decoded AsyncCommand [type=BRPOP, output=KeyValueOutput [output=KeyValue[retracted, {"retracted"}], error='null'], commandType=io.lettuce.core.protocol.Command], empty stack: true
ERROR [2018-05-18 17:53:32,134] com.zmp.core.Forwarder: zmptest01-worker-thread-0
! io.lettuce.core.RedisCommandTimeoutException: Command timed out
! at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114)
! at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:62)
! at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)
! at com.sun.proxy.$Proxy32.brpop(Unknown Source)
! at com.zmp.core.Forwarder$Worker.run(Forwarder.java:85)
I'll try to capture my server logs next time it crashes, but I do remember seeing hundreds of these ("org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out"), and so far it's been crashing in my production server about once a week.
@ihuk Timeouts on the synchronous API and the timeout passed to BRPOP are not related at all. If tag is more than 5 SECONDS, then it's quite normal that you see RedisCommandTimeoutException as Redis responds at a later time if the list is empty.
@mp911de I understand that.
However it seems like the code above can get into a state where Redis responds[1] but RedisCommandTimeoutException is raised and result is never delivered to the caller[2]. That's what I don't understand.
[1] DEBUG [2018-05-18 17:53:30,706] io.lettuce.core.protocol.RedisStateMachine: Decoded AsyncCommand [type=BRPOP, output=KeyValueOutput [output=KeyValue[retracted, {"retracted"}], error='null'], commandType=io.lettuce.core.protocol.Command], empty stack: true
[2] ERROR [2018-05-18 17:53:32,134] com.zmp.core.Forwarder: zmptest01-worker-thread-0
! io.lettuce.core.RedisCommandTimeoutException: Command timed out
! at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114)
! at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:62)
! at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)
! at com.sun.proxy.$Proxy32.brpop(Unknown Source)
! at com.zmp.core.Forwarder$Worker.run(Forwarder.java:85)
What happens above is that you have over 150 commands in the queue. The head command hasn't received a response, while the bottom commands time out from a synchronization perspective.
Or said differently: Commands remain in the command queue after timing out. Commands do not get removed after being canceled or when they time out and commands issued earlier must complete in order to process commands that were sent later.
This is because commands are already sent to the Redis server and await response processing. This can only happen when Redis starts to respond to the command which is at the head position. What you're seeing is a response to a command that ran into a timeout earlier (probably the command at the queue head) while the tail command was the one you were seeing to time out.
Let me see if I got that right: If I do connection.sync().brpop(0, tag); and it times out after a while that will actually leave a BRPOP command somewhere in lettuce command queue. When Redis eventually responds it will be consumed by the leftover BRPOP command in the command queue but since original caller is now long gone due to timeout it will just drop whatever it received?
Exactly.
Understood. Thanks.
@ihuk can you explain how @mp911de 's explanation helped you? Did you end up changing your code somehow? I'm getting intermittent timeout errors. It appears to be random and at random calls.
It appears to be more frequent in the listener of a pubsub. In the listener it makes sync commands to write to redis, and the pubsub and the commands are sharing a connection. So I guess I could see how that might cause a deadlock situation the sync command gets put on the same queue as the listener, and is the listener is waiting for the sycn command to return? However, I'm getting the timeout exception in other places not in the listener. I cannot reproduce this locally using a listener and sync() calls in the listener.
I'm using 5.0.4.RELEASE.