Dragonfly Cloud is now available in the AWS Marketplace - learn more

Question: How can you monitor key evictions in Redis?

Answer

In Redis, an eviction is the process of removing keys to make space for new data when memory is full. Redis offers a wide range of strategies to manage these evictions. If you want to monitor key evictions, you can use a combination of Redis commands and configuration settings.

Configuration Settings

Firstly, ensure that your eviction policy is set. Redis provides various policies like noeviction, allkeys-lru, volatile-lru, etc. You can set this using the maxmemory-policy configuration directive in your redis.conf file.

maxmemory-policy allkeys-lru

Also make sure to set the maxmemory configuration directive to trigger the eviction when memory reaches the defined limit.

maxmemory 100mb

Monitoring Evictions

You can use the INFO command to get statistics about the Redis server, including evictions. The info command returns multiple fields; the one we're interested in is evicted_keys, which shows the number of keys that have been evicted due to max memory limit.

127.0.0.1:6379> INFO # Output omitted for brevity evicted_keys:12345 # Output continues...

Active Monitoring

For active monitoring, you can also use the MONITOR command, which streams every command processed by the Redis server. However, bear in mind that the MONITOR command can significantly reduce the performance of your Redis server as it has to send back all processed commands.

With the MONITOR command, you can detect operations like DEL which might be part of the eviction process (depending on the maxmemory-policy).

127.0.0.1:6379> MONITOR OK # You will then see every command processed, look for DEL commands

Additionally, if you want more granular control or automated alerts based on evictions, consider using an application performance management tool like New Relic or Datadog that supports Redis.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost