Redis - monitoring memory usage

前端 未结 3 1436
广开言路
广开言路 2021-02-07 23:13

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 sw

相关标签:
3条回答
  • 2021-02-07 23:35

    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.

    0 讨论(0)
  • 2021-02-07 23:37

    There is a good Unix utility named vmstat. It is like top but command line, so you can get the memory usage and be prepared before you system is halt. You can also use ps v PID to get this info about specific process. Redis's PID is can be retrieved this way: pidof redis-server

    0 讨论(0)
  • 2021-02-07 23:40

    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.

    0 讨论(0)
提交回复
热议问题