3

Here is a snapshot of my redis settings

Total Keys  Keys with Expiry    Keys without Expiry
60Lacs      20Lacs              40Lacs  

Is there any method provided by Redis:

  1. Automatically set a TTL of 1 day on any key created by an application that doesn't specify a TTL

  2. Remove All existing keys with no TTL

Tague Griffith
  • 3,963
  • 2
  • 20
  • 24

1 Answers1

1

As far as question one - No, Redis does not provide a global TTL setting. The TTL has to be set on a per key basis. There are some ways you could script a solution but there is nothing built in. If you are concerned about the memory usage, look in to your configurations and modify the max-memory settings. See this answer for more information.

As far as question two - This script in Python is a basic example of how to iterate over the keys and delete anything without a TTL, you should build on it for your needs. Important information about making this performant can be found in this answer.

import redis
r = redis.StrictRedis(host='localhost', port=6379)

for key in r.scan_iter("*"):
    ttl = r.ttl(key)
    if ttl == -1:
        r.delete(key)
Tague Griffith
  • 3,963
  • 2
  • 20
  • 24
  • 1
    runnung a scan on production ? it will lock the db –  Sep 21 '17 at 19:20
  • That is why I added the disclaimer - "you should build on it for your needs. Important information about making this performant can be found in this answer." – Tague Griffith Sep 21 '17 at 19:41
  • 1
    Actually scans are ok for production: https://redis.io/commands/scan . "Since these commands allow for incremental iteration, returning only a small number of elements per call, they can be used in production" – kbosak May 08 '18 at 18:43