[4.0-beta1] Enable timeout when using Async API
I'm using the Async API to query Redis. I want a behavior where, when Redis is down, the client tries to reconnect periodically and while the connection is down it fails the commands immediately.
I tried setting a timeout on the client and the connection, hoping that the connection would send a timeout that would fail the future with a TimeoutException, however, what I see is all the commands being added to an internal queue to be later executed as soon as Redis is back up.
How can I configure my client/connection to make sure my Future complete with a failure after a specific amount of time when the connection is down? I want to avoid growing a queue of backedup commands.
Hi @vptheron, lettuce (3.x and 4.x) allows currently all-or-nothing: Either you get transparent reconnects with internal queueing of commands or no reconnects at all but then with canceling commands when the connection is disconnected.
Please be aware that canceling commands do not reduce the internal queue size. As soon as Redis available again and the connection is restored, canceled commands will not be triggered to the Redis instance.
You can use for now the not-so-obvious extensibility support of lettuce to implement the desired behavior on top of lettuce. By using this approach, you need to implement two classes on top of lettuce:
- Extending the
RedisClient(orRedisClusterClient, depending on which client you use) - Extending the
StatefulRedisConnectionImpl(orStatefulRedisPubSubConnectionImpl,StatefulRedisSentinelConnectionImpl, depending on which connection type you use)
You can use the protected methods to extend lettuce and as soon as lettuce 4.0.Final is released the API will remain stable, breaking changes will be avoided under all circumstances.
I published a demo and a JUnit test how the code could look like. You can find the code in the following Gist: https://gist.github.com/mp911de/407632a6b98e6ee36180
HTH, Mark
Thanks a lot for the fast response and the gist, that's what I had in mind since I was fearing your answer about "all-or-nothing" :)
Please be aware that canceling commands do not reduce the internal queue size. As soon as Redis available again and the connection is restored, canceled commands will not be triggered to the Redis instance.
That's interesting. So even if the commands are canceled and won't be played once the connection is re-established, they are enqueued. Is there a way to avoid that? I'm worried about the unbounded queue growing indefinitely while the client tries to reconnect.
Edit: I happen to use an async pool for some of my queries, and it looks like when sending commands via a pool while the server is down, the future fails right away as opposed to the behavior I previously described. I couldn't find anything specific to the pooled connections that would explain why they are able to fail the RedisFuture as opposed to enqueuing the commands.
No, there is no way to aviod that behavior. However, I could think of adding a command/queue limit as a Client option since limiting the queue can help to keep your application alive.
@vptheron ping. Just wanted to check whether the limitation-by-abstraction way is suitable for you? PR's are also welcome.
@mp911de I ended up implementing a custom timeout at the Future level. Not ideal but I really had to use the async API while guaranteeing a specific timeout for the caller.
I think it would be great to make the size of the internal queue configurable, or even disable the "retry-on-reconnect" behavior altogether.
I may end up trying to add what I was missing when I find the time :)
Appreciate your help very much, thanks.
Appreciate your feedback. I'll keep this one open for implementing the queue limit.
Implemented requestQueueSize for lettuce 3.4 and 4.0.
About the retry-on-reconnect behavior: The retry-on-reconnect mechanism has some implications on the behavior, meaning that switching it off is a fallback to at-most-once message delivery. Another question is: What happens with commands issued while the connection is in a disconnected state? I do not have enough information to decide on these topics yet.
Implemented requestQueueSize within the context of this ticket, will track the disconnected-behavior in #121