Amir El-Bashary
Amir El-Bashary

Reputation: 437

How Redis manage the unused cache keys?

My questions is very simple

Assuming I'm not specifying expires_in key for my generated cache key

Let's says i generate a cache key for posts with key "posts/#{maximum_record_updated at}" with no expires_in key

Now my content changed and new key has been set and is getting used with new "posts/#{maximum_record_updated_at}"

The cache is now calling the latest key only

Now the question is... what happens to the first key which is not going to be used anymore and has no expires_in specified ?

will it live forever or Redis will manage deleting it if it's not going to be used anymore ?

I know i would just specify the expires_in simply, but posts (in my case) could stay 1 week without any changes, maybe months, years, so I'm generating new cache key only when something changes

I'm just worried about the old keys and any unexpected memory issue

Upvotes: 2

Views: 477

Answers (1)

LeoMurillo
LeoMurillo

Reputation: 6764

The old unused key will stay there until Redis reaches maxmemory usage.

Then, Redis will stop accepting write commands or will start evicting keys, depending on the config value of maxmemory-policy. See https://redis.io/topics/lru-cache

Upvotes: 1

Related Questions