模块系统

Redis 4.0 发生的最大变化就是加入了模块系统, 这个系统可以让用户通过自己编写的代码来扩展和实现 Redis 本身并不具备的功能, 具体使用方法可以参考 antirez 的博文《Redis Loadable Module System》: http://antirez.com/news/106

因为模块系统是通过高层次 API 实现的, 它与 Redis 内核本身完全分离、互不干扰, 所以用户可以在有需要的情况下才启用这个功能, 以下是 redis.conf 中记载的模块载入方法:

  1. ################################## MODULES #####################################
  2.  
  3. # Load modules at startup. If the server is not able to load modules
  4. # it will abort. It is possible to use multiple loadmodule directives.
  5. #
  6. # loadmodule /path/to/my_module.so
  7. # loadmodule /path/to/other_module.so

目前已经有人使用这个功能开发了各种各样的模块, 比如 Redis Labs 开发的一些模块就可以在 http://redismodules.com 看到, 此外 antirez 自己也使用这个功能开发了一个神经网络模块: https://github.com/antirez/neural-redis

模块功能使得用户可以将 Redis 用作基础设施, 并在上面构建更多功能, 这给 Redis 带来了无数新的可能性。

PSYNC 2.0

新版本的 PSYNC 命令解决了旧版本的 Redis 在复制时的一些不够优化的地方:

  • 在旧版本 Redis 中, 如果一个从服务器在 FAILOVER 之后成为了新的主节点, 那么其他从节点在复制这个新主的时候就必须进行全量复制。 在 Redis 4.0 中, 新主和从服务器在处理这种情况时, 将在条件允许的情况下使用部分复制。
  • 在旧版本 Redis 中, 一个从服务器如果重启了, 那么它就必须与主服务器重新进行全量复制, 在 Redis 4.0 中, 只要条件允许, 主从在处理这种情况时将使用部分复制。

缓存驱逐策略优化

新添加了 Last Frequently Used 缓存驱逐策略, 具体信息见 antirez 的博文《Random notes on improving the Redis LRU algorithm》: http://antirez.com/news/109

另外 Redis 4.0 还对已有的缓存驱逐策略进行了优化, 使得它们能够更健壮、高效、快速和精确。

非阻塞 DEL 、 FLUSHDB 和 FLUSHALL

在 Redis 4.0 之前, 用户在使用 DEL 命令删除体积较大的键, 又或者在使用 FLUSHDB 和 FLUSHALL 删除包含大量键的数据库时, 都可能会造成服务器阻塞。

为了解决以上问题, Redis 4.0 新添加了 UNLINK 命令, 这个命令是 DEL 命令的异步版本, 它可以将删除指定键的操作放在后台线程里面执行, 从而尽可能地避免服务器阻塞:

  1. redis> UNLINK fruits
  2. (integer) 1

因为一些历史原因, 执行同步删除操作的 DEL 命令将会继续保留。

此外, Redis 4.0 中的 FLUSHDB 和 FLUSHALL 这两个命令都新添加了 ASYNC 选项, 带有这个选项的数据库删除操作将在后台线程进行:

  1. redis> FLUSHDB ASYNC
  2. OK
  3.  
  4. redis> FLUSHALL ASYNC
  5. OK

交换数据库

Redis 4.0 对数据库命令的另外一个修改是新增了 SWAPDB 命令, 这个命令可以对指定的两个数据库进行互换: 比如说, 通过执行命令 SWAPDB 0 1 , 我们可以将原来的数据库 0 变成数据库 1 , 而原来的数据库 1 则变成数据库 0 。

以下是一个使用 SWAPDB 的例子:

  1. redis> SET your_name "huangz" -- 在数据库 0 中设置一个键
  2. OK
  3.  
  4. redis> GET your_name
  5. "huangz"
  6.  
  7. redis> SWAPDB 0 1 -- 互换数据库 0 和数据库 1
  8. OK
  9.  
  10. redis> GET your_name -- 现在的数据库 0 已经没有之前设置的键了
  11. (nil)
  12.  
  13. redis> SELECT 1 -- 切换到数据库 1
  14. OK
  15.  
  16. redis[1]> GET your_name -- 之前在数据库 0 设置的键现在可以在数据库 1 找到
  17. "huangz" -- 证明两个数据库已经互换

混合 RDB-AOF 持久化格式

Redis 4.0 新增了 RDB-AOF 混合持久化格式, 这是一个可选的功能, 在开启了这个功能之后, AOF 重写产生的文件将同时包含 RDB 格式的内容和 AOF 格式的内容, 其中 RDB 格式的内容用于记录已有的数据, 而 AOF 格式的内存则用于记录最近发生了变化的数据, 这样 Redis 就可以同时兼有 RDB 持久化和 AOF 持久化的优点 —— 既能够快速地生成重写文件, 也能够在出现问题时, 快速地载入数据。

这个功能可以通过 aof-use-rdb-preamble 选项进行开启, redis.conf 文件中记录了这个选项的使用方法:

  1. # When rewriting the AOF file, Redis is able to use an RDB preamble in the
  2. # AOF file for faster rewrites and recoveries. When this option is turned
  3. # on the rewritten AOF file is composed of two different stanzas:
  4. #
  5. # [RDB file][AOF tail]
  6. #
  7. # When loading Redis recognizes that the AOF file starts with the "REDIS"
  8. # string and loads the prefixed RDB file, and continues loading the AOF
  9. # tail.
  10. #
  11. # This is currently turned off by default in order to avoid the surprise
  12. # of a format change, but will at some point be used as the default.
  13. aof-use-rdb-preamble no

内存命令

新添加了一个 MEMORY 命令, 这个命令可以用于视察内存使用情况, 并进行相应的内存管理操作:

  1. redis> MEMORY HELP
  2. 1) "MEMORY USAGE <key> [SAMPLES <count>] - Estimate memory usage of key"
  3. 2) "MEMORY STATS - Show memory usage details"
  4. 3) "MEMORY PURGE - Ask the allocator to release memory"
  5. 4) "MEMORY MALLOC-STATS - Show allocator internal stats"

其中, 使用 MEMORY USAGE 子命令可以估算储存给定键所需的内存:

  1. redis> SET msg "hello world"
  2. OK
  3.  
  4. redis> SADD fruits apple banana cherry
  5. (integer) 3
  6.  
  7. redis> MEMORY USAGE msg
  8. (integer) 62
  9.  
  10. redis> MEMORY USAGE fruits
  11. (integer) 375

使用 MEMORY STATS 子命令可以查看 Redis 当前的内存使用情况:

  1. redis> MEMORY STATS
  2. 1) "peak.allocated"
  3. 2) (integer) 1014480
  4. 3) "total.allocated"
  5. 4) (integer) 1014512
  6. 5) "startup.allocated"
  7. 6) (integer) 963040
  8. 7) "replication.backlog"
  9. 8) (integer) 0
  10. 9) "clients.slaves"
  11. 10) (integer) 0
  12. 11) "clients.normal"
  13. 12) (integer) 49614
  14. 13) "aof.buffer"
  15. 14) (integer) 0
  16. 15) "db.0"
  17. 16) 1) "overhead.hashtable.main"
  18. 2) (integer) 264
  19. 3) "overhead.hashtable.expires"
  20. 4) (integer) 32
  21. 17) "overhead.total"
  22. 18) (integer) 1012950
  23. 19) "keys.count"
  24. 20) (integer) 5
  25. 21) "keys.bytes-per-key"
  26. 22) (integer) 10294
  27. 23) "dataset.bytes"
  28. 24) (integer) 1562
  29. 25) "dataset.percentage"
  30. 26) "3.0346596240997314"
  31. 27) "peak.percentage"
  32. 28) "100.00315093994141"
  33. 29) "fragmentation"
  34. 30) "2.1193723678588867"

使用 MEMORY PURGE 子命令可以要求分配器释放更多内存:

  1. redis> MEMORY PURGE
  2. OK

使用 MEMORY MALLOC-STATS 子命令可以展示分配器内部状态:

  1. redis> MEMORY MALLOC-STATS
  2. Stats not supported for the current allocator

兼容 NAT 和 Docker

Redis 4.0 将兼容 NAT 和 Docker , 具体的使用方法在 redis.conf 中有记载:

  1. ########################## CLUSTER DOCKER/NAT support ########################
  2.  
  3. # In certain deployments, Redis Cluster nodes address discovery fails, because
  4. # addresses are NAT-ted or because ports are forwarded (the typical case is
  5. # Docker and other containers).
  6. #
  7. # In order to make Redis Cluster working in such environments, a static
  8. # configuration where each node known its public address is needed. The
  9. # following two options are used for this scope, and are:
  10. #
  11. # * cluster-announce-ip
  12. # * cluster-announce-port
  13. # * cluster-announce-bus-port
  14. #
  15. # Each instruct the node about its address, client port, and cluster message
  16. # bus port. The information is then published in the header of the bus packets
  17. # so that other nodes will be able to correctly map the address of the node
  18. # publishing the information.
  19. #
  20. # If the above options are not used, the normal Redis Cluster auto-detection
  21. # will be used instead.
  22. #
  23. # Note that when remapped, the bus port may not be at the fixed offset of
  24. # clients port + 10000, so you can specify any port and bus-port depending
  25. # on how they get remapped. If the bus-port is not set, a fixed offset of
  26. # 10000 will be used as usually.
  27. #
  28. # Example:
  29. #
  30. # cluster-announce-ip 10.1.1.5
  31. # cluster-announce-port 6379
  32. # cluster-announce-bus-port 6380

【特性】Redis4.0新特性的更多相关文章

  1. Redis4.0新特性

    redis 4.0 新特性 Redis 4.0在2017年7月发布为GA.包含几个重大改进:更好的复制(PSYNC2),线程DEL / FLUSH,混合RDB + AOF格式,活动内存碎片整理,内存使 ...

  2. Redis4.0新特性之-大KEY删除

    接上一篇,我们得知了redis中存在大KEY,那么这个大KEY如何删除呢?本文将从源码角度分析Redis4.0带来的新特性. 在Redis中,对于大KEY的删除一直是个比较头疼的问题,为了不影响服务, ...

  3. Redis4.0新特性(一)-Memory Command

    Redis4.0版本增加了很多诱人的新特性,在redis精细化运营管理中都非常有用(猜想和antirez加入redislabs有很大关系):此系列几篇水文主要介绍以下几个新特性的使用和效果. Redi ...

  4. Redis4.0新特性 -Lazy Free

    Redis4.0新增了非常实用的lazy free特性,从根本上解决Big Key(主要指定元素较多集合类型Key)删除的风险.笔者在redis运维中也遇过几次Big Key删除带来可用性和性能故障. ...

  5. Redis 6.0 新特性-多线程连环13问!

    Redis 6.0 来了 在全国一片祥和IT民工欢度五一节假日的时候,Redis 6.0不声不响地于5 月 2 日正式发布了,吓得我赶紧从床上爬起来,学无止境!学无止境! 对于6.0版本,Redis之 ...

  6. 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?

    来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...

  7. Java基础和JDK5.0新特性

    Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...

  8. Visual Studio 2015速递(1)——C#6.0新特性怎么用

    系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...

  9. Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth

    Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1 Python 3_x 新特性1 python3.4新特性1 python3.5新特性1 值得关注的新特性1 ...

随机推荐

  1. linux ~/ 和 /

    /是目录层的分隔.表示符.只有一个/表明是root,/etc/表明是根目录下面的etc目录(当然目录最后不需要/,但有/直接表明他是目录,没有末尾的/,那么/etc需要检测一下确定是目录还是文件,虽然 ...

  2. java设计模式之动态代理的概述和实现

    概述 1.代理:本来应该自己做的事情,请了别人来做,被请的人就是代理对象. 举例:春节回家买票让人代买 2.在Java中java.lang.reflect包下提供了一个Proxy类和一个Invocat ...

  3. 以太坊客户端Ethereum Wallet与Geth区别简介

    以太坊客户端Ethereum Wallet与Geth区别简介 最近有不少朋友在搭建交易平台,在咨询和技术交流的过程中发现很多朋友不太清楚Ethereum Wallet和Geth区别.甚至有朋友使用Ge ...

  4. Javascript深入理解构造函数和原型对象

    1.在典型的oop的语言中,如java,都存在类的概念,类就是对象的模板,对象就是类的实例.但在js中不存在类的概念,js不是基于类,而是通过构造函数(constructor)和原型链(propoty ...

  5. POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...

  6. 不懂RPC实现原理怎能实现架构梦

    RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...

  7. Python小项目四:实现简单的web服务器

    https://blog.csdn.net/u010103202/article/details/74002538 本博客是整理在学习实验楼的课程过程中记录下的笔记形成的,参考:https://www ...

  8. NOSQL中的redis缓存数据库

    NOSQL概述 什么是NOSQL? NoSql(NoSQL=Not Only SQL),意思为"不仅仅是SQL",是一个全新的数据库理念,泛指非关系型的数据库. 为什么需要NOSQ ...

  9. 通过RMAN 识别失败数据库损坏的对象

    背景 业务起不来,读取数据库时报坏块,无法读取数据 数据库版本:11.2.0.3 数据库无备份,无归档 1. 识别坏块 执行以下命令后,rman 会把坏块信息统计到 v$database_block_ ...

  10. php 阳历转农历优化版

    网上转换方法很多例子错误. 测试例子1:输入公历 2010年2月1号测试,对比百度万年历 农历应该为己丑年(2009)腊月(12月)十八. 测试例子2:输入农历1990.11.初十,丑时,公历应该为1 ...