0%

Redis之多线程

几个重要版本:2.8版本引入psync以支持增量同步,3.2版本以后List的底层实现为QUICKLIST;4.0版本引入混合持久化;6.0版本引入多线程。

一、基础

  1. 先看更新内容,翻到最下方
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
================================================================================
Redis 6.0 RC1 Released Thu Dec 19 09:58:24 CEST 2019
================================================================================

Upgrade urgency LOW: This is the first RC of Redis 6.

Introduction to the Redis 6 release
===================================

Redis 6 improves Redis in a number of key areas and is one of the largest
Redis releases in the history of the project, so here we'll list only
the biggest features in this release:

* The modules system now has a number of new APIs that allow module authors
to make things otherwise not possible in the past. It is possible to
store arbitrary module private data in RDB files, to hook on different
server events, capture and rewrite commands executions, block clients on
keys, and so forth.
* The Redis active expire cycle was rewritten for much faster eviction of keys
that are already expired. Now the effort is tunable.
* Redis now supports SSL on all channels.
* ACL support, you can define users that can run only certain commands and/or
can only access only certain keys patterns.
* Redis now supports a new protocol called RESP3, which returns more
semantical replies: new clients using this protocol can understand just
from the reply what type to return to the calling program.
* There is server-side support for client-side caching of key values. This
feature is still experimental and will get more changes during the next
release candidates, but you can already test it and read about it here:
https://redis.io/topics/client-side-caching
* Redis can now optionally use threads to handle I/O, allowing to serve
2 times as much operations per second in a single instance when
pipelining cannot be used.
* Diskless replication is now supported even on replicas: a replica is now
able, under certain conditions the user can configure, to load the RDB
in the first synchronization directly from the socket to the memory.
* Redis-benchmark now supports a Redis Cluster mode.
* SRANDMEMBER and similar commands have a better distribution.
* Redis-cli improvements.
* Systemd support rewritten.
* A Redis Cluster proxy was released here:
https://github.com/artix75/redis-cluster-proxy
* A Disque module for Redis was released here:
https://github.com/antirez/disque-module

Thanks to all the users and developers who made this release possible.
We'll follow up with more RC releases, until the code looks production ready
and we don't get reports of serious issues for a while.

文档写的很清楚了,喜欢看中文的朋友可以参考如下(仅列举了笔者认为比较重要的):

  • 多线程IO,多线程部分只是用来处理网络数据的读写和协议解析,执行命令仍然是单线程

    • 默认关闭状态,开启选项io-threads-do-reads yes
    • 线程数设置选项io-threads 4,官方建议:线程数一定要小于机器核数且不可超过8个
  • 官方集群代理,对客户端几乎无感知,就像一个实例一样

  • 全面支持SSL协议,并新增TSL协议,更加安全

  • 引入RESP3协议,RESP(Redis Serialization Protocol)是Redis服务端与客户端之间通信的协议,Redis5使用的是RESP2,Redis6在兼容RESP2的基础上引入RESP3

  • 提升了RDB日志加载速度,预期可以提升20%~30%

  • 重新设计实现了客户端缓存功能,传送门

  • psync2优化,解决了Redis5中链式复制不一致问题

  • ACL权限管控,支持对客户端的权限控制,实现key级别的权限控制

    • 新增acl log命令,通过此命令可以查看所有违反ACL的命令

    • 查看acl相关的命令

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
       1) ACL <subcommand> arg arg ... arg. Subcommands are:
      2) LOAD -- Reload users from the ACL file.
      3) SAVE -- Save the current config to the ACL file.
      4) LIST -- Show user details in config file format.
      5) USERS -- List all the registered usernames.
      6) SETUSER <username> [attribs ...] -- Create or modify a user.
      7) GETUSER <username> -- Get the user details.
      8) DELUSER <username> [...] -- Delete a list of users.
      9) CAT -- List available categories.
      10) CAT <category> -- List commands inside category.
      11) GENPASS [<bits>] -- Generate a secure user password.
      12) WHOAMI -- Return the current connection username.
      13) LOG [<count> | RESET] -- Show the ACL log entries.

二、原理

  1. 处理过程
    • 主线程获取socket放入等待列表
    • 将socket分配给各个IO线程(并不会等列表满)
    • 主线程阻塞等待IO线程(多线程)读取socket完毕
    • 主线程执行命令-单线程(如果命令没有接收完毕会等IO下次继续)
    • 主线程阻塞等待IO线程(多线程)将数据回写socket完毕(一次没写完,会等下次再写)
    • 解除绑定,清空等待队列

      TODO

三、参考

  1. 参考一
  2. 参考二
  3. 参考三