I am using redis for caching in one of my application,I need to store around 500 000 keys per day & need to set a EXPIRE for all keys. After 5 days I will be have about 2.5 million keys & I have set EXPIRE for all the keys. Is it fine to set EXPIRE for the keys & let the redis scan every minute to find out expire keys (According to the documentation). How much would be the performance impact because of this?
Is there a better alternative for this?
I tried to search on Google,But nothing found about performance. My main concern is performance as well as memory too.
PS. Currently my redis is running out of space because of too much keys on my redis server.
Redis keys are expired in two ways: a passive way, and an active way. A key is passively expired simply when some client tries to access it, and the key is found to be timed out. Of course this is not enough as there are expired keys that will never be accessed again.
But the problem is that if a key is never touched, it just takes up memory for no reason. So Redis adds a second layer of random active expiration. It just reads random keys all the time, and when an expired key is touched it is deleted based on the lazy mechanism.
First, we use redis-cli --scan --pattern <pattern> to get a list of keys, one key per line. We then run an awk script for each key. This awk script is a little complex, so we will break it down
To manually clear a key’s timeout, use the persist command: The persist command will return (integer) 1 if it completed successfully, indicating that the key will no longer expire. This guide details a number of commands used to manipulate and check key persistence in Redis.
Using expires as the replacement algorithm is not always a good idea, which works like FIFO.
The answer depends on your workload.
If your workload is completely random (no key is accessed more frequent than others), no replacement algorithm works well (so, choose a simpler one).
If older keys are less accessed than recent keys, FIFO is ok.
If some keys are accessed more frequently (i.e., there are hot keys), lru is better. See Using Redis as an LRU cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With