jedis icon indicating copy to clipboard operation
jedis copied to clipboard

Add support for auto reconneting in Jedis

Open costin opened this issue 15 years ago • 13 comments

Best explained in the comments at https://jira.springsource.org/browse/DATAKV-29

costin avatar Jan 23 '11 20:01 costin

This is interesting. I wonder why testOnBorrow wouldn't be enough.

xetorthio avatar Jan 24 '11 02:01 xetorthio

I think it should be (not sure how expensive the validation query is or whether multi/pipeline support affect this). It would be nice to know what impact it has on performance as it might be worth enabling it by default.

costin avatar Jan 24 '11 16:01 costin

yeah, even if PING is not an expensive operation, executing one every time you get a connection from the pool seems too pessimistic. probably we should trust on the pool most of the time, so maybe it is better to assume the connection is OK and if it fails, just return it broken and ask for a new connection and repeat the operation. probably Jedis should do this automatically and leave the amount of times to do this as a configuration somewhere.

xetorthio avatar Jan 24 '11 17:01 xetorthio

It's important to make sure the behaviour is configurable - for example one might not want the operation to be retried (which can be configured on and off through a setting). As for defaults, I think it's important to use simple, intuitive defaults that give great performance - restarting the server is somewhat unusual. It's nice to be able to cope with it but if enabling this has side effects (like retries), then the application might want to know about it - for example, how can I test if the database is restarted if my PING requests are being retried instead of just failing...

costin avatar Jan 24 '11 17:01 costin

We tried the testOnBorrow property and the impact on performance was horrible to say the least. I'd suggest straying away from it as a long term solution.

ensonik avatar Jan 26 '11 20:01 ensonik

Ok. I will work on a reconnect mechanism ASAP and if you can let me know how it works!

xetorthio avatar Jan 26 '11 20:01 xetorthio

reconnectting is an important feature.

for high throughout environment, testonborrow is not a good way.

we can use do and catching exception pattern:

Get a connection from pool; try { do something; } catch (Exception e) { redis.clients.util.returnBrokenResource(resource); }

juyide avatar Jan 28 '11 15:01 juyide

Issue 43 should improve on this by differentiating between connection exceptions and protocol/application exceptions. That way one can know whether the connection is actually broken or not.

costin avatar Jan 28 '11 17:01 costin

please check issue #123 as I think it may impact on this. Many connection errors were not modifying connection state in jedis so jedis would never know it was disconnected even when throwing broken pipe exceptions. I think my fix would reconnect on sending commands for most cases

TioBorracho avatar Apr 13 '11 15:04 TioBorracho

no movement since april... should we close this?

TioBorracho avatar Jun 03 '11 15:06 TioBorracho

I think this issue is not relevant now as of Jedis 2.0.0 when connection is broken, Jedis will close it and reopen it on the next command. It is something in the middle between. Reconnection is not that good for some cases. For example multi/exec blocks, pipelines, etc. So this gives you a good way to handle this, without the need to use testOnBorrow. Closing now. If the current solution is not enough we can reopen and re-discuss :)

xetorthio avatar Jun 03 '11 15:06 xetorthio

Does Jedis provides this auto reconnecting / auto retry feature? if yes then how can i configure it?

PriyankaRanjan avatar Mar 14 '16 15:03 PriyankaRanjan

@PriyankaRanjan no, because otherwise Jedis would have to queue all pending operations and retry them in a different thread until the server comes back online (or similar). Jedis returns a JedisConnectionException whenever the server is down or something happened in the communication. It's up to the user to decide how / when to retry the operation. You can re-use the same Jedis instance as it'll try to re-connect if a connection error has been received in the past.

The reason why we decided not to do any retry logic is because usually users have different requirements for retry policies depending on their situation. That's why we encourage the community to come up with their own retry-library and open source it so others can use it.

marcosnils avatar Mar 14 '16 18:03 marcosnils