Skip to content
Advertisement

Collection Object lost due to redis TTL using redission client

We are using Redission client for java to get the data from redis but object gets deleted from collection due to TTL.

Example

We are trying the below approach to get the data from Redis with TTL.

final RList rList = client.getList(getEnvCacheKey(cacheKey));

rList.expire(7L, TimeUnit.SECONDS);

rlist.add("Value1");

rlist.add("Value2");

assertThat(rList).containsOnly("Value1", "Value2"); // This condition is true until 7 seconds

Now after 7 seconds assert rlist.size() == 2 condition becomes false since object references are deleted due to TTL.

Due this we landed up in a production issue. Is there any way we can retain the objects even after TTL? Any sort of help will be appreciated.

Advertisement

Answer

The TTL(Time-To-Live) itself sets the expiration of a particular key after which the key can no longer be retrieved. If you want to keep the key in the memory you could simply skip setting rList.expire(7L, TimeUnit.SECONDS); altogether (infinite expiration).

In case you want to extend expiration, you can do so by repeating the expire command. It is also possible to remove the TTL altogether this way, although I could not tell you how to do it specifically with the Redisson.

As for the expired keys, Redis clears them 10 times a second (according to this documentation), so I don’t think that you can (consistently) recover the values within the expired keys.

My general advice would be to take a step back and look at your system design. In case you are missing the expired keys, maybe this part of the product should get an extended TTL/no TTL/periodical TTL refresh

Advertisement