Can I set global TTL in redis?

No, Redis doesn't have a notion of a global/default TTL and yes, you do have to set it for each key independently. However, depending on your requirements and on what you're trying to do, there may be other ways to achieve your goal. Put differently, why do you need it?

For example, if you want to use Redis as a cache and not worry about having to remove "old" items, you can get simply by setting the maxmemory_policy to allkey-lru. This will evict the least recently used keys whenever Redis' memory is exhausted.

EDIT: for more information, see the helpful links in the comments below from @arganzheng and @Kristján, as well as the inline documentation in the redis.conf configuration file.


While not being a "pure" Redis solution for this, check out this RedisGears example that achieves this purpose: https://oss.redislabs.com/redisgears/master/examples.html#automatic-expiry


If you are setting a key , you can set the TTL in the same time : look at the set command

a side , you can done this by scripting (on linux like - for 60 seconds):

for k in `redis-cli --raw keys '*'` ; do redis expire $k 60;done

Tags:

Redis