0

I am storing detail of a user for one day. Which one of below is better approach assuming this user is viewed 2k times in day .

  1. TTL set one time

    Set TTL of 24hours. On every access to the user we will fetch data from Redis and won't update TTL. after 24 hours, when the key was expired update the TTL.

  2. TTL set on every request.

    Set TTL of 4hours. On every access to the user we will fetch data from Redis and update TTL to +4 hours from now. unused keys will automatically expire.

Question: I feel the first approach is better as on 2nd approach we don't need to update TTL 10k times(views) and need to fire query only one time. Please suggest which is better approach?

chicharito
  • 1,047
  • 3
  • 12
  • 41
  • If you're only caching, why bother with setting a TTL in the first place? Just let Redis take care of that using an LFU/LRU/random eviction policy on all keys. Missing (evicted) keys will be fetched by the app and put in the cache regardless, no? – Itamar Haber Feb 14 '18 at 16:07
  • Oh, I see you've also hit the mailing list and got a similar reply from @antirez :) Also, it appears this isn't first time something similar has been discussed: https://stackoverflow.com/questions/25618045/can-i-set-global-ttl-in-redis/25618782#25618782 – Itamar Haber Feb 14 '18 at 20:20
  • @ItamarHaber when Redis automatically take care using LRU i.e when memory is full, it's not considered good as compared to TTL expiry – chicharito Feb 15 '18 at 04:33
  • I'm not sure as to what "good" means here – Itamar Haber Feb 15 '18 at 14:06
  • when memory is full, its like full gc. – chicharito Feb 15 '18 at 19:25
  • Understood. Full memory eviction is indeed better to be avoided, but even that is throttled and unless your data varies hugely (i.e. very small and very large values), you can expect a constant amortized overhead on that. TTL can be used to fine tune, but some cases you can get away without it. – Itamar Haber Feb 15 '18 at 22:02
  • @ItamarHaber you are not replying as per my question asked? – chicharito Feb 16 '18 at 19:20
  • It is very hard to say which approach is better, because "better" is an undefined term. The 1st approach is less "expensive" in terms of total ops, but unused data will stay longer and may become stale. The 2nd approach will be more intensive both in operations as well in regards to what is in RAM. Because both add some complexity, I suggested the eviction as a cheap way to push it entirely down to the server and let it care of it. – Itamar Haber Feb 16 '18 at 22:42

0 Answers0