-
Hello 👋 Followed: https://redis.io/docs/latest/develop/clients/lettuce/produsage/ and https://github.com/redis/lettuce/wiki/Spring-Support/450db075877f2e3baf522380881272454c29ba61 But I dont see any keep alives (ping/pongs) beeing sent over the connection with: I can manually call the following snippet to see the ping/pong: I set up the redis client with keep alives as follow: @Configuration
public class RedisConfig {
private static final Logger logger = Logger.getLogger(RedisConfig.class.getName());
@Bean(destroyMethod = "shutdown")
ClientResources clientResources() {
return DefaultClientResources.create();
}
@Bean(destroyMethod = "shutdown")
public RedisClient redisClient(ClientResources clientResource) {
RedisURI redisURI = RedisURI.Builder
.redis("localhost")
.withTimeout(Duration.ofSeconds(30))
.build();
RedisClient client = RedisClient.create(clientResource, redisURI);
SocketOptions.TcpUserTimeoutOptions tcpUserTimeout = SocketOptions.TcpUserTimeoutOptions.builder()
.tcpUserTimeout(Duration.ofSeconds(20))
.enable().build();
SocketOptions.KeepAliveOptions keepAliveOptions = SocketOptions.KeepAliveOptions.builder()
.interval(Duration.ofSeconds(5))
.idle(Duration.ofSeconds(5))
.count(3).enable().build();
SocketOptions socketOptions = SocketOptions.builder()
.tcpUserTimeout(tcpUserTimeout)
.keepAlive(keepAliveOptions)
.build();
client.setOptions(ClientOptions.builder()
.socketOptions(socketOptions)
.build());
return client;
}
@Bean(destroyMethod = "close")
StatefulRedisConnection<String, String> connection(RedisClient redisClient) {
return redisClient.connect();
}
} To verify settings and to see the ping/pong I do: @GetMapping("/test-redis2")
public void testRedisConnection2() {
logger.info(connection.getOptions().getSocketOptions().getKeepAlive().getInterval().toString());
logger.info(connection.getOptions().getSocketOptions().getKeepAlive().getIdle().toString());
logger.info(String.valueOf(connection.getOptions().getSocketOptions().getKeepAlive().getCount()));
connection.sync().ping();
} Prints: 2025-02-11T14:10:57.077+01:00 INFO 5551 --- [demo] [nio-8080-exec-3] com.example.demo.RedisTestController : PT5S
2025-02-11T14:10:57.077+01:00 INFO 5551 --- [demo] [nio-8080-exec-3] com.example.demo.RedisTestController : PT5S
2025-02-11T14:10:57.077+01:00 INFO 5551 --- [demo] [nio-8080-exec-3] com.example.demo.RedisTestController : 3
2025-02-11T14:10:57.077+01:00 DEBUG 5551 --- [demo] [nio-8080-exec-3] i.lettuce.core.protocol.DefaultEndpoint : [channel=0x8920dabb, /127.0.0.1:52488 -> localhost/127.0.0.1:6379, epid=0x1] write() writeAndFlush command AsyncCommand [type=PING, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
2025-02-11T14:10:57.077+01:00 DEBUG 5551 --- [demo] [nio-8080-exec-3] i.lettuce.core.protocol.DefaultEndpoint : [channel=0x8920dabb, /127.0.0.1:52488 -> localhost/127.0.0.1:6379, epid=0x1] write() done
2025-02-11T14:10:57.077+01:00 DEBUG 5551 --- [demo] [ioEventLoop-4-1] i.lettuce.core.protocol.CommandHandler : [channel=0x8920dabb, /127.0.0.1:52488 -> localhost/127.0.0.1:6379, epid=0x1, chid=0x1] write(ctx, AsyncCommand [type=PING, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command], promise)
2025-02-11T14:10:57.078+01:00 DEBUG 5551 --- [demo] [ioEventLoop-4-1] i.lettuce.core.protocol.CommandEncoder : [channel=0x8920dabb, /127.0.0.1:52488 -> localhost/127.0.0.1:6379] writing command AsyncCommand [type=PING, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
2025-02-11T14:10:57.078+01:00 DEBUG 5551 --- [demo] [ioEventLoop-4-1] i.lettuce.core.protocol.CommandHandler : [channel=0x8920dabb, /127.0.0.1:52488 -> localhost/127.0.0.1:6379, epid=0x1, chid=0x1] Received: 7 bytes, 1 commands in the stack
2025-02-11T14:10:57.078+01:00 DEBUG 5551 --- [demo] [ioEventLoop-4-1] i.lettuce.core.protocol.CommandHandler : [channel=0x8920dabb, /127.0.0.1:52488 -> localhost/127.0.0.1:6379, epid=0x1, chid=0x1] Stack contains: 1 commands
2025-02-11T14:10:57.078+01:00 DEBUG 5551 --- [demo] [ioEventLoop-4-1] i.l.core.protocol.RedisStateMachine : Decode done, empty stack: true
2025-02-11T14:10:57.078+01:00 DEBUG 5551 --- [demo] [ioEventLoop-4-1] i.lettuce.core.protocol.CommandHandler : [channel=0x8920dabb, /127.0.0.1:52488 -> localhost/127.0.0.1:6379, epid=0x1, chid=0x1] Completing command AsyncCommand [type=PING, output=StatusOutput [output=PONG, error='null'], commandType=io.lettuce.core.protocol.Command] |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hey @sklakegg , The Wouldn't this be enough for your use case? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply @tishun 🙏 I see, I also came across #2256 which probably alludes to a similar scenario. We have nginx between our client and the azure redis server. But reading the Azure Redis documentation: https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-best-practices-connection#idle-timeout
My understanding is that we would need to periodically send a PING to keep the connection to Azure Cache for Redis open? I guess in addition to any TCP keepalives. |
Beta Was this translation helpful? Give feedback.
My idea was a bit simpler and should work with any type of connection made with a client that has the appropriate
ClientResources
configured.You need 3 parts.
First you configure the client to use a Netty customizer of your own: