了解Redis后台内存的使用情况

Recently, I was talking to a long-time friend, previous university colleague and former boss, who mentioned the fact that Redis was failing to persist data to disk in low memory conditions. For that reason, he advised to never let a Redis in-memory dataset to be bigger than 50% of the system memory. Thinking about how wasteful that practice would be, it's interesting to understand why this can happen and look for alternatives to assure that Redis will be able to use as much memory as there's available to it, without sacrificing its durability.

最近,我和一个多年的朋友、以前的大学同事和前老板聊天,他提到Redis在低内存条件下无法将数据持久化到磁盘。出于这个原因,他建议永远不要让Redis内存中的数据集大于系统内存的50%。考虑到这种做法是多么的浪费,了解为什么会发生这种情况并寻找替代方案以确保Redis能够使用尽可能多的内存,而不牺牲其持久性是很有趣的。

#!/usr/bin/env python import random
import string
import uuid
import redis MEM_GB = 2 * 1024**3
KEY_SIZE = 1024**2
TOTAL_KEYS = int((MEM_GB * 0.5) / KEY_SIZE) def gen_data(): return ''.join([random.choice(string.ascii_letters + string.digits) for x in range(1024)]) * 1024 r = redis.StrictRedis() for i in range(TOTAL_KEYS):    r.set(uuid.uuid4(), gen_data())

It will generate random key/value pairs of 1MB each, using up to half of the total memory available. As it was executed on a 2GB RAM virtual machine, it will create a dataset about 1GB in size. Considering the memory used by the OS and all other processes, we can be sure that Redis is now using a bit more than 50% of the total system memory. From this point in time, calling BGSAVE will result in an error:

它将生成每个1MB的随机键/值对,使用多达一半的可用总内存。由于它是在一个2GB内存的虚拟机上执行的,它将创建一个大约1GB大小的数据集。考虑到操作系统和所有其他进程使用的内存,我们可以肯定Redis现在使用的内存略高于系统总内存的50%。从这个时间点开始,调用BGSAVE将导致错误。

127.0.0.1:6379> BGSAVE (error) ERR

127.0.0.1:6379> BGSAVE (error) ERR

And the following message will appear in /var/log/redis/redis-server.log (on a Ubuntu 18.04 LTS system):

而以下信息将出现在/var/log/redis/redis-server.log中(在Ubuntu 18.04 LTS系统上)。

10202:M 13 Sep 11:34:16.535 # Can't save in background: fork: Cannot allocate memory

10202:M 13 Sep 11:34:16.535 # Can't save in background: fork: Cannot allocate memory

Looking at the s...

开通本站会员,查看完整译文。

trang chủ - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-15 19:53
浙ICP备14020137号-1 $bản đồ khách truy cập$