redis4.0之MEMORY命令详解

简介: 在过去,查看redis的内存使用状态只有info memory命令,而且也只有一些基础信息,想要获取全局信息就有些困难。4.0开始redis提供了`MEMORY`命令,一切都变得简单起来。

前言

在过去,查看redis的内存使用状态只有info memory命令,而且也只有一些基础信息,想要获取全局信息就有些困难。4.0开始redis提供了MEMORY命令,一切都变得简单起来。

MEMORY命令

MEMORY命令一共有5个子命令,可以通过MEMORY HELP来查看:

127.0.0.1:6379> memory help
1) "MEMORY DOCTOR                        - Outputs memory problems report"
2) "MEMORY USAGE <key> [SAMPLES <count>] - Estimate memory usage of key"
3) "MEMORY STATS                         - Show memory usage details"
4) "MEMORY PURGE                         - Ask the allocator to release memory"
5) "MEMORY MALLOC-STATS                  - Show allocator internal stats"

接下来我们从MEMORY STATS开始,一一介绍各个子命令的功能。

1. MEMORY STATS

首先,我们需要明确一个概念,redis的内存使用不仅包含所有的key-value数据,还有描述这些key-value的元信息,以及许多管理功能的消耗,比如持久化、主从复制,通过MEMORY STATS可以更好的了解到redis的内存使用状况。

这里我们启动了一个打开持久化功能并且带slave的redis,向其中随机写入了一些数据(某些数据还带有过期时间),以便读者可以更好的了解redis的内存使用,接下来执行MEMORY STATS命令:

127.0.0.1:6379> memory stats
 1) "peak.allocated"
 2) (integer) 423995952
 3) "total.allocated"
 4) (integer) 11130320
 5) "startup.allocated"
 6) (integer) 9942928
 7) "replication.backlog"
 8) (integer) 1048576
 9) "clients.slaves"
10) (integer) 16858
11) "clients.normal"
12) (integer) 49630
13) "aof.buffer"
14) (integer) 3253
15) "db.0"
16) 1) "overhead.hashtable.main"
    2) (integer) 5808
    3) "overhead.hashtable.expires"
    4) (integer) 104
17) "overhead.total"
18) (integer) 11063904
19) "keys.count"
20) (integer) 94
21) "keys.bytes-per-key"
22) (integer) 12631
23) "dataset.bytes"
24) (integer) 66416
25) "dataset.percentage"
26) "5.5934348106384277"
27) "peak.percentage"
28) "2.6251003742218018"
29) "fragmentation"
30) "1.1039986610412598"

一共有15项内容,内存使用量均以字节为单位,我们一个一个来看:

1. peak.allocated

  • redis启动到现在,最多使用过多少内存。

2. total.allocated

  • 当前使用的内存总量。

3. startup.allocated

  • redis启动初始化时使用的内存,有很多读者会比较奇怪,为什么我的redis启动以后什么都没做就已经占用了几十MB的内存?
  • 这是因为redis本身不仅存储key-value,还有其他的内存消耗,比如共享变量、主从复制、持久化和db元信息,下面各项会有详细介绍。

4. replication.backlog

  • 主从复制backlog使用的内存,默认10MB,backlog只在主从断线重连时发挥作用,主从复制本身并不依赖此项。

5. clients.slaves

主从复制中所有slave的读写缓冲区,包括output-buffer(也即输出缓冲区)使用的内存和querybuf(也即输入缓冲区),这里简单介绍一下主从复制:

  • redis把一次事件循环中,所有对数据库发生更改的内容先追加到slave的output-buffer中,在事件循环结束后统一发送给slave。
  • 那么主从之间就难免会有数据的延迟,如果主从之间连接断开,重连时为了保证数据的一致性就要做一次全量同步,这显然是不够高效的。backlog就是为此而设计,master在backlog中缓存一部分主从复制的增量数据,断线重连时如果slave的偏移量在backlog中,那就可以只把偏移量之后的增量数据同步给slave即可,避免了全量同步的开销。

6. clients.normal

  • 除slave外所有其他客户端的读写缓冲区。
  • 有时候一些客户端读取不及时,就会造成output-buffer积压占用内存过多的情况,可以通过配置项client-output-buffer-limit来限制,当超过阈值之后redis就会主动断开连接以释放内存,slave亦是如此。

7. aof.buffer

此项为aof持久化使用的缓存和aofrewrite时产生的缓存之和,当然如果关闭了appendonly那这项就一直为0:

  • redis并不是在有写入时就立即做持久化的,而是在一次事件循环内把所有的写入数据缓存起来,待到事件循环结束后再持久化到磁盘。
  • aofrewrite时缓存增量数据使用的内存,只在aofrewrite时才会使用,aofrewrite机制可以参考之前的文章《redis4.0之利用管道优化aofrewrite》

可以看出这一项的大小与写入流量成正比。

8. db.0

  • redis每个db的元信息使用的内存,这里只使用了db0,所以只打印了db0的内存使用状态,当使用其他db时也会有相应的信息。

    db的元信息有以下三项:
    

    a) redis的db就是一张hash表,首先就是这张hash表使用的内存(redis使用链式hash,hash表中存放所有链表的头指针);

    b) 每一个key-value对都有一个dictEntry来记录他们的关系,元信息便包含该db中所有dictEntry使用的内存;

    c) redis使用redisObject来描述value所对应的不同数据类型(string、list、hash、set、zset),那么redisObject占用的空间也计算在元信息中。

  • overhead.hashtable.main:

    
    db的元信息也即是以上三项之和,计算公式为:
    

    hashtable + dictEntry + redisObject

  • overhead.hashtable.expires:

    对于key的过期时间,redis并没有把它和value放在一起,而是单独用一个hashtable来存储,但是expires这张hash表记录的是key-expire信息,所以不需要`redisObject`来描述value,其元信息也就少了一项,计算公式为:
    

    hashtable + dictEntry

9. overhead.total

  • 3-8项之和:startup.allocated+replication.backlog+clients.slaves+clients.normal+aof.buffer+dbx

10. dataset.bytes

  • 所有数据所使用的内存——也即total.allocated - overhead.total——当前内存使用量减去管理类内存使用量。

11. dataset.percentage

  • 所有数据占比,这里并没有直接使用total.allocated做分母,而是除去了redis启动初始化的内存,计算公式为:

    100 * dataset.bytes / (total.allocated - startup.allocated)

12. keys.count

  • redis当前存储的key总量

13. keys.bytes-per-key

  • 平均每个key的内存大小,直觉上应该是用dataset.bytes除以keys.count即可,但是redis并没有这么做,而是把管理类内存也平摊到了每个key的内存使用中,计算公式为:

        (total.allocated - startup.allocated) / keys.count
    

14. peak.percentage

  • 当前使用内存与历史最高值比例

15. fragmentation

  • 内存碎片率

2. MEMORY USAGE

相信所有redis用户都希望对每一个key-value的内存使用了如指掌,然而4.0之前redis并没有提供一个明确的方法来进行内存评估,不过从4.0开始,MEMORY命令实现了这一功能。

首先看下使用方法:MEMORY usage [samples]

命令参数不多,通过字面意思也可以看出来是评估指定key的内存使用情况。samples是可选参数默认为5,以hash为例看下其如果工作:

  1. 首先类似于上一节中的overhead.hashtable.main,要计算hash的元信息内存,包括hash表的大小以及所有dictEntry的内存占用信息。
  2. 与overhead.hashtable.main不同的是,每个dictEntry中key-value都是字符串,所以没redisObject的额外消耗。在评估真正的数据内存大小时redis并没有去遍历所有key,而是采用的抽样估算:随机抽取samples个key-value对计算其平均内存占用,再乘以key-value对的个数即得到结果。试想一下如果要精确计算内存占用,那么就需要遍历所有的元素,当元素很多时就是使redis阻塞,所以请合理设置samples的大小。

其他数据结构的计算方式类似于hash,此处就不再赘述。

3. MEMORY DOCTOR

此项子命令是作者给出的关于redis内存使用方面的建议,在不同的允许状态下会有不同的分析结果:

首先是没问题的情况

  • 运行状态良好:

    Hi Sam, I can't find any memory issue in your instance. I can only account for what occurs on this base.
    
  • redis的数据量很小,暂无建议:

    Hi Sam, this instance is empty or is using very little memory, my issues detector can't be used in these conditions. Please, leave for your mission on Earth and fill it with some data. The new Sam and I will be back to our programming as soon as I finished rebooting.
    

接下来出现的结果就需要注意了

  • 内存使用峰值1.5倍于目前内存使用量,此时内存碎片率可能会比较高,需要注意:

     Peak memory: In the past this instance used more than 150% the memory that is currently using. The allocator is normally not able to release memory after a peak, so you can expect to see a big fragmentation ratio, however this is actually harmless and is only due to the memory peak, and if the Redis instance Resident Set Size (RSS) is currently bigger than expected, the memory will be used as soon as you fill the Redis instance with more data. If the memory peak was only occasional and you want to try to reclaim memory, please try the MEMORY PURGE command, otherwise the only other option is to shutdown and restart the instance.
    
  • 内存碎片率过高超过1.4,需要注意:

    High fragmentation: This instance has a memory fragmentation greater than 1.4 (this means that the Resident Set Size of the Redis process is much larger than the sum of the logical allocations Redis performed). This problem is usually due either to a large peak memory (check if there is a peak memory entry above in the report) or may result from a workload that causes the allocator to fragment memory a lot. If the problem is a large peak memory, then there is no issue. Otherwise, make sure you are using the Jemalloc allocator and not the default libc malloc.
    
  • 每个slave缓冲区的平均内存超过10MB,原因可能是master写入流量过高,也有可能是主从同步的网络带宽不足或者slave处理较慢:

    Big slave buffers: The slave output buffers in this instance are greater than 10MB for each slave (on average). This likely means that there is some slave instance that is struggling receiving data, either because it is too slow or because of networking issues. As a result, data piles on the master output buffers. Please try to identify what slave is not receiving data correctly and why. You can use the INFO output in order to check the slaves delays and the CLIENT LIST command to check the output buffers of each slave.
    
  • 普通客户端缓冲区的平均内存超过200KB,原因可能是pipeline使用不当或者Pub/Sub客户端处理消息不及时导致:

    Big client buffers: The clients output buffers in this instance are greater than 200K per client (on average). This may result from different causes, like Pub/Sub clients subscribed to channels bot not receiving data fast enough, so that data piles on the Redis instance output buffer, or clients sending commands with large replies or very large sequences of commands in the same pipeline. Please use the CLIENT LIST command in order to investigate the issue if it causes problems in your instance, or to understand better why certain clients are using a big amount of memory.
    

4. MEMORY MALLOC-STATS

打印内存分配器状态,只在使用jemalloc时有用。

5. MEMORY PURGE

请求分配器释放内存,同样只对jemalloc生效。

结束

基于4.0的云redis已正式上线,登陆阿里云控制台即可开通:https://www.aliyun.com/product/kvstore

目录
相关文章
|
9月前
|
存储 缓存 监控
Redis设计与实现——Redis命令参考与高级特性
Redis 是一个高性能的键值存储系统,支持丰富的数据类型(字符串、列表、哈希、集合等)和多种高级功能。本文档涵盖 Redis 的核心命令分类,包括数据类型操作、事务与脚本、持久化、集群管理、系统监控等。特别介绍了事务的原子性特性、Lua 脚本的执行方式及优势、排序机制、发布订阅模型以及慢查询日志和监视器工具的使用方法。适用于开发者快速掌握 Redis 常用命令及其应用场景,优化系统性能与可靠性。
|
5月前
|
存储 缓存 NoSQL
Redis基础命令与数据结构概览
Redis是一个功能强大的键值存储系统,提供了丰富的数据结构以及相应的操作命令来满足现代应用程序对于高速读写和灵活数据处理的需求。通过掌握这些基础命令,开发者能够高效地对Redis进行操作,实现数据存储和管理的高性能方案。
160 12
|
5月前
|
存储 消息中间件 NoSQL
【Redis】常用数据结构之List篇:从常用命令到典型使用场景
本文将系统探讨 Redis List 的核心特性、完整命令体系、底层存储实现以及典型实践场景,为读者构建从理论到应用的完整认知框架,助力开发者在实际业务中高效运用这一数据结构解决问题。
|
6月前
|
存储 缓存 人工智能
Redis六大常见命令详解:从set/get到过期策略的全方位解析
本文将通过结构化学习路径,帮助读者实现从命令语法掌握到工程化实践落地的能力跃迁,系统性提升 Redis 技术栈的应用水平。
|
7月前
|
NoSQL Redis
Lua脚本协助Redis分布式锁实现命令的原子性
利用Lua脚本确保Redis操作的原子性是分布式锁安全性的关键所在,可以大幅减少由于网络分区、客户端故障等导致的锁无法正确释放的情况,从而在分布式系统中保证数据操作的安全性和一致性。在将这些概念应用于生产环境前,建议深入理解Redis事务与Lua脚本的工作原理以及分布式锁的可能问题和解决方案。
268 8
|
9月前
|
存储 缓存 NoSQL
Redis中的常用命令-get&set&keys&exists&expire&ttl&type的详细解析
总的来说,这些Redis命令提供了处理存储在内存中的键值对的便捷方式。通过理解和运用它们,你可以更有效地在Redis中操作数据,使其更好地服务于你的应用。
525 17
|
9月前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
958 16
|
9月前
|
JSON NoSQL Redis
在Rocky9系统上安装并使用redis-dump和redis-load命令的指南
以上步骤是在Rocky9上使用redis-dump和redis-load命令顺利出行的秘籍。如果在实行的过程中,发现了新的冒险和挑战,那么就像一个勇敢的航海家,本着探索未知的决心,解决问题并前进。
278 14
|
NoSQL 应用服务中间件 API
Redis是如何建立连接和处理命令的
本文主要讲述 Redis 是如何监听客户端发出的set、get等命令的。
1655 160
|
9月前
|
消息中间件 NoSQL Unix
Redis的基本特性以及其基础命令用法
这只是冰山一角,Redis的强大功能和简洁的操作方法值得我们深入了解和掌握,是复杂数据问题解决的有力工具。所以,来一场有趣的Redis冒险吧!
221 6

相关产品

  • 云数据库 Tair(兼容 Redis)