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?
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:
- volatile-lru -> remove the key with an expire set using an LRU algorithm
- allkeys-lru -> remove any key accordingly to the LRU algorithm
- volatile-random -> remove a random key with an expire set
- allkeys->random -> remove a random key, any key
- volatile-ttl -> remove the key with the nearest expire time (minor TTL)
- 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'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With