PubSubConnection after disconnected/connected
More of a question than an issue...
Am I supposed to re-instantiate my StatefulRedisPubSubConnection (or even my StatefulRedisConnection) when I get a detect a pair of disconnected/connected events on the client's ClusterListener ?
Asking this, because when I get some disconnections/reconnections (connecting to an externally hosted Redis), the PubSub connection is not responsive anymore.
Lettuce auto-reconnects by default (see https://github.com/lettuce-io/lettuce-core/wiki/Client-options). Pub/Sub connections re-subscribe to all known patterns and channels once they reconnect. If that's not the case or you run into issues, please either capture as much as you have of the state or provide a reproducible test case.
I would like to have a reproducible test case, but I run in this issue only when connecting to my Compose-hosted Redis instance, not on my dev/staging environments.
Like I said above, the connection isn't dead, but I get a major performance hit until I restart the process to force a fresh reconnection.
Here's a log extract of the latest of such events:
2017-08-06T01:04:28,011 [lettuce-eventExecutorLoop-2-3] INFO com.lambdaworks.redis.protocol.ConnectionWatchdog - Reconnecting, last destination was localhost/127.0.0.1:6379
2017-08-06T01:04:28,011 [lettuce-eventExecutorLoop-2-4] INFO com.lambdaworks.redis.protocol.ConnectionWatchdog - Reconnecting, last destination was localhost/127.0.0.1:6379
2017-08-06T01:04:28,023 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:28,023 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:28,114 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:28,118 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,314 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,314 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,411 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,411 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,511 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,514 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,612 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,613 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,711 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,712 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,810 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,811 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,910 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:29,910 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,010 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,011 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,110 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,110 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,210 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,211 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,312 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,312 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,410 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,410 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,509 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,509 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,609 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,609 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,709 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,709 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,809 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,810 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,909 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:30,909 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,009 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,009 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,109 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,110 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,210 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,210 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,309 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,309 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,409 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,409 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,509 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,510 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,610 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,610 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,709 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:31,710 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,209 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,211 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,309 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,309 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,410 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,410 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,510 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,510 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,609 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,609 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,809 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:32,810 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,107 [lettuce-eventExecutorLoop-2-5] INFO com.lambdaworks.redis.protocol.ConnectionWatchdog - Reconnecting, last destination was localhost/127.0.0.1:6379
2017-08-06T01:04:33,108 [lettuce-eventExecutorLoop-2-6] INFO com.lambdaworks.redis.protocol.ConnectionWatchdog - Reconnecting, last destination was localhost/127.0.0.1:6379
2017-08-06T01:04:33,110 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,111 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,209 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,210 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,309 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,310 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,410 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,411 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,510 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,510 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,609 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,610 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,709 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,710 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,809 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,809 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,909 [lettuce-nioEventLoop-5-5] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:33,910 [lettuce-nioEventLoop-5-6] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:34,009 [lettuce-nioEventLoop-5-8] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:34,010 [lettuce-nioEventLoop-5-7] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:34,110 [lettuce-nioEventLoop-5-1] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:34,110 [lettuce-nioEventLoop-5-2] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:34,209 [lettuce-nioEventLoop-5-3] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
2017-08-06T01:04:34,209 [lettuce-nioEventLoop-5-4] INFO com.lambdaworks.redis.protocol.ReconnectionHandler - Reconnected to localhost:6379
You have either a lot of connections or a lot of disconnects. In any case, you should reduce the number of reconnects. Less connections allow better diagnosis.
Thanks, I wish I could give you more info (btw, I'm running 4.4.0.Final).
I only instantiate two connections, once, when the process starts :
pubSubConnection = redisClient.connectPubSub();
redisConnection = redisClient.connect();
redisClient is created with the default options (no tweaking of ClientResources).
Thanks for the details. Using a static number of connections like you do is a good approach. The question is then, why you get such a massive number of reconnects. Could you monitor your production Redis server for connection count? Enabling trace (debug) logging can reveal what's happening on the protocol level (i.e. a Redis response of the disconnect).
Lettuce should reconnect once and stay connected.
Sure, I'll try to capture more details. What I described as a performance hit, was in fact just a symptom, the real issue is with the pub/sub connection (I have a scheduled background task that pull from redis periodically even if the pub/sub connection is dead).
@mp911de would you mind explaining how to attach a ReconnectListener? I need to re-sync some data by myself after pubsub is resubscribed to account for missed messages.
@mp911de so as I see ReconnectListener is an internal api :( but could I possibly count on 'subscribe' event being re-emitted to PubSubListener or also no?
@ninja- Check out the documentation on connection events in the first place. The pub/sub connection re-subscribes to the previously subscribed patterns and channels. After the subscription commands complete, the RedisPubSubListener.subscribed/RedisPubSubListener.psubscribed listeners are notified.
May I close this ticket or is there anything else I can assist you with?
Hey, you can close it. I'm still struggling with disconnections, but a node driver also report them, so the problem is certainly not specific to lettuce.