I have a service that uses jedis to read from redis. Obviously I don’t intend to establish a new connection with every GET request but curious on the guard rails i need in case read fails because of connection failures ? To clean up idle connections i have configured timeout
on server side. Reference link
Can some jedis/redis experts here recommend me the ideal way keeping latency in mind. I can think of the following, if there is any better way, please suggest
- If read fails due to connection failure, try to establish connection
again and then read again. - Before every read, verify
jedis.isBroken()
orjedis.isConnected()
. Should i verify both or either would suffice ? - Do
jedis.ping()
and on success only read , else try re-establish the connection.
2
Answers
nodejs with use ioredis package
You can know like this
Use
JedisPool
a orJedisPooled
b and forget the hassles of connection management. This will cover your (1) and (2).For (3), you can use
GenericObjectPoolConfig#setTestOnBorrow(true)
. Both JedisPoolc and JedisPooledd accepts a GenericObjectPoolConfig in some of their constructors.[a] JedisPool is available since very long time ago.
[b] JedisPooled is available since version 4.0.0.
[c] It is
GenericObjectPoolConfig<Jedis>
for JedisPool.[d] It is
GenericObjectPoolConfig<Connection>
for JedisPooled.An example with JedisPooled: