1.Modules
Modules
让用户通过自己编写的代码来扩展和实现redis
本身并不具备的功能。
它通过高层 API
实现,与Redis
内核本身完全分离、互不干扰。所以你可以在不用修改redis源码的基础上,去开发新的命令,支持新的数据结构以及开发其他一些新功能。
使redis
能提供丰富的个性化功能去的支持业务以及运维需要。
实例:
Redis Labs
开发的一些模块 http://redismodules.com
作者自己实现的 https://github.com/antirez/neural-redis
2.psync2
psync2
对主从复制功能进行优化。
避免因故障转移、重启原因造成的不必要的全量主从同步。
详见:https://moji.wemomo.com/article/62516
psync3也在设计中:
1)aof
也将能像rdb
一样保存replication met-data
2) 优化Redis Sentinel
和 Redis Cluster failovers
机制,使它在无持久化或者使用rdb方式时,更加安全。
详见:https://github.com/antirez/redis/issues/4357
3.pipe优化aofrewrite
redis
是通过fork
子进程来做aofrewrite
。
同时为了保证aof
的连续性,父进程把aofrewrite
期间的写命令缓存起来,等子进程结束之后再追加到新的aof
文件。如果期间写入量较大的话,就要有大量的写硬盘操作。这个写硬盘的操作是阻塞的。
通过管道方式,将追加写盘的操作也就转交给了子进程,避免阻塞主进程。
详见:https://yq.aliyun.com/articles/177819?spm=a2c4e.11154837.971143.14.67c326ebVC4Wbn
4.支持发现热点key
./redis-cli --hotkeys
# Scanning the entire keyspace to find hot keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
[00.00%] Hot key 'counter:000000000002' found so far with counter 87
[00.00%] Hot key 'key:000000000001' found so far with counter 254
由于热点key
的发现是基于LFU
算法实现的,注意需要内存逐出策略设置为allkeys-lfu
或者volatile-lfu
才能使用该功能。
5.lazyfree异步方式优化阻塞
- 大
key
删除 -
FLUSHDB
和FLUSHALL
清空包含大量键的数据库 - 大量
key
集中过期 - 主从全量复制时,从库清空旧数据。
都会造成redis
阻塞。
4.0增加unlink
、flushdb async
、flushall async
命令。
还增加了4个后台删除配置项,分别为:
-
slave-lazy-flush
:slave
接收完rdb
文件后清空数据选项 -
lazyfree-lazy-eviction
:内存满逐出选项 -
lazyfree-lazy-expire
:过期key删除选项 -
lazyfree-lazy-server-del
:内部删除选项,比如rename srckey destkey
时,如果destkey
存在需要先删除destkey
6.MEMORY命令更好的了解redis内存
MEMORY STATS
详细拆分redis内存
- 持久化:
cow
使用的内存、aof.buffer
- 主从复制:
replication.backlog
、clients.slaves
- 客户端:
clients.normal
-
kv
数据实际占用的内存
帮助分析线上问题,方便合理规划内存。
MEMORY DOCTOR
根据内存使用,诊断可能故障原因
- 内存使用峰值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.
- 每个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
7.redis 碎片整理
redis
碎片率过高时,物理内存利用率低。以前只能通过重启的方式来解决,现在提供动态清理的方式。
- 支持在运行期进行自动内存碎片清理 (
config set activedefrag yes
) - 支持通过命令
memory purge
进行清理(与自动清理区域不同)
需要使用jemalloc
作为内存分配器(默认的).
redis
为了保证碎片清理时的性能,只会对属于jemalloc small bin (not large or huge)的kv
进行清理。
8.混合存储
RDB
文件本质上是一份内存快照保存了创建RDB文件那个时间点的redis
全量数据具有数据文件小创建、恢复快的优点但是由于快照的特性无法保存创建RDB之后的增量数据。
AOF
文件基本上是human-readable
的文本所以其体积相对较大在从AOF
文件恢复数据时就是做日志回放执行AOF
文件中记录的所有命令所以相对RDB
而言恢复耗时较长。
混合存储先以RDB
格式写入全量数据再追加增量日志呢这样既可以提高aofrewrite
和恢复速度也可以减少文件大小还可以保证数据的完毕性整合RDB
和AOF
的优点。
redis5.0
比较重要的功能:
redis-cli
中的集群管理器从 Ruby (redis-trib.rb)
移植到了 C
语言代码,方便运维。
支持新的流数据类型(Stream data type
)。
详见:https://github.com/antirez/redis/blob/5.0/00-RELEASENOTES
更详细的redis4.0功能
2.8-4.0 redis性能优化与功能修改:https://moji.wemomo.com/doc#/detail/63096
网友评论