Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out redis memory usage in python when a maxmemory limit has been set

Tags:

python

redis

I am writing a python script which will periodically check redis memory usage. Now I know that used_memory or used_memory_human from redis info will tell us how much memory redis is using currently. But I want to know how much percentage of memory has been utilised.

In redis info we don't get something like max_memory because of the fact that redis uses up as much memory available to the system. But what if maxmemory option in the redis.conf was set?

What if my system has 1 GB of RAM but maxmemory was set to 100MB? How can we know the memory utilisation then?

like image 788
Shades88 Avatar asked Oct 24 '25 22:10

Shades88


1 Answers

You can use CONFIG command to get needed info about maximum memory and memory policy. The syntax of getting configuration info is:

config get <option-name>

Here's an example of how you can get maxmemory setting:

redis 127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"

Here's and example of how you can get maxmemory-policy setting:

redis 127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "volatile-lru"

There'are different policies of maxmemory management (you can get their description from sample Redis config file):

MAXMEMORY POLICY: how Redis will select what to remove when maxmemory is reached? You can select among five behavior:

  1. volatile-lru -> remove the key with an expire set using an LRU algorithm
  2. allkeys-lru -> remove any key accordingly to the LRU algorithm
  3. volatile-random -> remove a random key with an expire set
  4. allkeys->random -> remove a random key, any key
  5. volatile-ttl -> remove the key with the nearest expire time (minor TTL)
  6. noeviction -> don't expire at all, just return an error on write operations

And here's and example of how similar result can be achieved with pyredis (assuming that we have localhost database on standard port):

>>> import redis
>>> c = redis.Redis()
>>> print c.config_get('maxmemory')
{'maxmemory': '0'}
like image 109
Rostyslav Dzinko Avatar answered Oct 28 '25 05:10

Rostyslav Dzinko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!