Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis - monitoring memory usage

Tags:

memory

redis

I am currently testing insertion of keys in a database Redis (on local). I have more than 5 millions keys and I have just 4GB RAM so at one moment I reach capacity of RAM and swap fill in (and my PC goes down)...

My problematic : How can I make monitoring memory usage on the machine which has the Redis database, and in this way alert no more insert some keys in the Redis database ?

Thanks.

like image 369
kozher Avatar asked Jun 23 '11 07:06

kozher


People also ask

How do I MONITOR my Redis memory usage?

You can collect all memory utilization metrics data for a Redis instance by running “info memory”. Sometimes, when Redis is configured with no max memory limit, memory usage will eventually reach system memory, and the server will start throwing “Out of Memory” errors.

How can I check Redis max memory?

Connect to the cluster using the redis-cli tool or another tool of your choice. For information on using the redis-cli tool, see Step 3.2: Connect to a Redis Cluster or Replication Group (Linux). 2. Run the info memory command and check the maxmemory value.

How do I check my data usage on Redis?

You can check the actual number by running redis-cli config get databases. In interactive mode, the database number is displayed in the prompt within square braces. For example, 127.0. 0.1:6379[13] shows that the 13th database is in use.


2 Answers

Memory is a critical resource for Redis performance. Used memory defines total number of bytes allocated by Redis using its allocator (either standard libc, jemalloc, or an alternative allocator such as tcmalloc).

You can collect all memory utilization metrics data for a Redis instance by running “info memory”.

 
127.0.0.1:6379> info memory
Memory
used_memory:1007280
used_memory_human:983.67K
used_memory_rss:2002944
used_memory_rss_human:1.91M
used_memory_peak:1008128
used_memory_peak_human:984.50K

Sometimes, when Redis is configured with no max memory limit, memory usage will eventually reach system memory, and the server will start throwing “Out of Memory” errors. At other times, Redis is configured with a max memory limit but noeviction policy. This would cause the server not to evict any keys, thus preventing any writes until memory is freed. The solution to such problems would be configuring Redis with max memory and some eviction policy. In this case, the server starts evicting keys using eviction policy as memory usage reaches the max.

Memory RSS (Resident Set Size) is the number of bytes that the operating system has allocated to Redis. If the ratio of ‘memory_rss’ to ‘memory_used’ is greater than ~1.5, then it signifies memory fragmentation. The fragmented memory can be recovered by restarting the server.

like image 98
VISHAL KUMAWAT Avatar answered Nov 15 '22 12:11

VISHAL KUMAWAT


Concerning memory usage, I'd advise you to look at the redis.io FAQ and this article about using redis as a LRU cache.

You can either cap the memory usage via the maxmemory configuration setting, in which case once the memory limit is reached all write requests will fail with an error, or you could set the maxmemory-policy to allkeys-lru, for example, to start overwriting the least recently used data on the server with stuff you currently need, etc. For most use cases you have enough flexibility to handle such problems through proper config.

My advice is to keep things simple and manage this issue through configuration of the redis server rather than introducing additional complexity through os-level monitoring or the like.

like image 36
Hristo Avatar answered Nov 15 '22 13:11

Hristo