memoryredisblockingttllru

Redis performance comparison: using TTL vs allkeys-lru policy


In Redis, using allkeys-lru deletes the key no matter if it's an expire-set key or not.

Using TTL, setting an expiration for the key, uses memory.

Quoting from Redis.io:

It is also worth noting that setting an expire to a key costs memory, so using a policy like allkeys-lru is more memory efficient since there is no need to set an expire for the key to be evicted under memory pressure.

  1. Is it really more efficient overall to NOT put a TTL on the key and let allkeys-lru policy handle it?
  2. Isn't there any tradeoffs in this situation? For example, does the allkeys-lru block the write action until it completes the expiration? That would make me use the TTL if this expiration is going to take long durations.

I would love to discuss about this. Thanks for everybody's input!


Solution

  • allkeys-lru is triggered by Redis allocated memory limit. It's a safety feature to avoid crashing Redis entirely. If you rely on only allkeys-lru to cleanup your data then your Redis will run slower because any operation would have to be applied to a bigger DB. And your Redis DB will always be at max size.

    Also it makes it harder to monitor your resources during your business growth.

    Using TTL on your values is more a technical decision based on your use case. It gives you more control over which events you don't need anymore. TTL uses more memory because it has to store the TTL value for each record, makes sense.

    For Redis-Streams, you can use the MAXLEN property to not grow your streams too much, specially when you don't need older data. This property is per stream so it will not increase Redis memory that much. Redis-streams are expired by stream(by key), not by record. So it's not possible to expire old records from Streams based on a TTL/record if you continuously receive new data.

    Main conclusion: Use TTL and MAXLEN where possible to cleanup unnecessary data sooner so Redis will not need to do it all at once and you will have more control over your data and resources.