Redis Excessive Swap Usage

Redis swapping to disk, which significantly slows down performance since Redis is designed to operate in-memory.
  1. Check Redis Memory Usage: Determine how much memory Redis is currently using with the command:
  2. redis-cli info memory
  3. Look at the used_memory and used_memory_rss metrics to understand the current memory usage.
  4. Identify Top Memory Consumers: Use the Redis MEMORY USAGE command for keys (if you have an idea of your key patterns) or a script to iterate through keys to report their sizes. This will help identify which keys are consuming the most memory:
  5. redis-cli --scan --pattern '*' | xargs -L 1 -I{} sh -c 'echo -n "{}: " ; redis-cli memory usage "{}"'
  6. Check Current Configuration: Review your Redis instance configuration to understand memory limits and eviction policies:
  7. redis-cli config get maxmemory
    redis-cli config get maxmemory-policy
  8. Analyze Slow Queries: Examine slow queries which might be causing bottlenecks and leading to increased memory usage:
  9. redis-cli slowlog get 10
  10. Investigate the output to find any patterns or commands that are consistently slow.
  11. Monitor Swap Usage: Use system tools to monitor swap usage and identify if Redis or another process is causing excessive swap usage:
  12. vmstat 1
  13. or
  14. free -m
  15. to observe swap and memory usage over time.
  16. Reduce Memory Usage: If a specific key or set of keys are identified as consuming too much memory, consider deleting them if they are not needed, or reducing their size. For instance, to delete a key:
  17. redis-cli del <key_name>
  18. Adjust Max Memory: If the memory usage is reaching the limit, and it's safe to allocate more, adjust Redis’s max memory setting (make sure you have enough system memory):
  19. redis-cli config set maxmemory <new_limit>
  20. Change Eviction Policy: If necessary, change the eviction policy to better manage memory usage:
  21. redis-cli config set maxmemory-policy <policy_name>
  22. Choose an appropriate eviction policy that matches your use case, like volatile-lru, allkeys-lru, etc.
  23. Restart Redis: If changes are made to the configuration that cannot be applied dynamically, or as a last resort to clear the current memory and swap usage (caution, as this will lead to data loss if data is not persisted properly):
  24. redis-cli shutdown
  25. and then restart Redis.
  26. Monitor Metrics After Changes: After taking action, continue to monitor Redis memory and swap usage to ensure the problem is resolved:
  27. redis-cli info memory
  28. and use system tools to monitor swap usage.

Take these actions step by step to identify and mitigate the root cause of excessive swap usage in Redis.

Never debug

Redis

manually again

Let Dr. Droid create custom investigation plans for your infrastructure.

Book Demo
Automate Debugging for
Redis
See how Dr. Droid creates investigation plans for your infrastructure.

MORE ISSUES

Made with ❤️ in Bangalore & San Francisco 🏢

Doctor Droid