Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to set expire for all keys in redis

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.

like image 563
NameNotFoundException Avatar asked Nov 26 '14 05:11

NameNotFoundException


People also ask

How do Redis keys expire?

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.

Why does Redis add a second layer of active expiration?

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.

How do I get a list of all keys in Redis?

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

How do I clear key timeout in Redis?

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.


1 Answers

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.

like image 138
Min Fu Avatar answered Sep 25 '22 23:09

Min Fu