惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 司徒正美
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
Help Net Security
Help Net Security
N
News and Events Feed by Topic
O
OpenAI News
L
LangChain Blog
F
Full Disclosure
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
Cloudbric
Cloudbric
W
WeLiveSecurity
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
Attack and Defense Labs
Attack and Defense Labs
PCI Perspectives
PCI Perspectives
TaoSecurity Blog
TaoSecurity Blog
AI
AI
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
T
Threatpost
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Last Week in AI
Last Week in AI
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 聂微东
P
Proofpoint News Feed
Latest news
Latest news
S
SegmentFault 最新的问题
J
Java Code Geeks
T
Threat Research - Cisco Blogs
H
Help Net Security
P
Privacy International News Feed

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例) 读《为什么学生不喜欢上学》 OpenAI Prompt Engineering 摘录和总结 读《打造真正的新产品》 VueJS 总结 Linux 自动挂载 alist 提供的webdav FreeBSD 使用 vm-bhyve 安装Debian虚拟机 FreeBSD 和 Linux 网卡聚合实现提速 GPT 帮我搞定了时区转换问题 长任务系统如何处理? macOS/Linux 编译 InputLeap 使用开源软KVM - synergy-core 解决 macOS 终端hostname一直变化问题 KVM 共享 Intel 集成显卡 PromQL 备忘 读《格鲁夫给经理人的第一课》 读《打开心智》 为什么要把复杂的联表操作拆成多个单表查询? 红包系统的设计 MySQL Index Condition Pushdown Optimization Go mod 简明教程 OpenWRT 使用 Android/iOS USB 网络 搭建旁路由 Golang gRPC 错误处理 编写可维护的单元测试代码 OAuth 2 详解(六):Authorization Code Flow with PKCE OAuth 2 详解(五):Device Authorization Flow OAuth 2 详解(三):Resource Owner Password Credentials Grant OAuth 2 详解(四):Client Credentials Flow OAuth 2 详解(二):Implict Grant Flow OAuth 2 详解(一):简介及 Authorization Code 模式 ElasticSearch 学习笔记 三种git流程以及发版模型 错误处理实践 权限模型(RBAC/ABAC) OIDC(OpenID Connect) 简介 任务队列简介 PostgreSQL 操作笔记 使用Drone CI构建CI/CD系统 Golang migrate 做数据库变更管理 使用PostgreSQL做搜索引擎 Nginx 源码阅读(三): 连接池、内存池 Nginx 源码阅读(二): 请求处理 Nginx 源码阅读(一): 启动流程 Go 泛型简明教程 KVM 显卡穿透给 Windows 使用 HTTP Router 处理 Telegram Bot 按钮回调 使用反射(reflect)对结构体赋值 GIN 是如何绑定参数的 你好 2022(2021 年终总结) 用Go导入大型CSV到PostgreSQL 使用 OpenWRT 搭建软路由 使用软KVM切换器 barrier 共享键鼠 SQL 防注入及原理 使用 gomock 测试 Go 代码 gevent不是黑魔法(二): gevent 实现 gevent不是黑魔法(一): greenlet 实现 用 entgo 替代 gorm 应用内使用crontab不是那么方便 单测时要不要 mock 数据库? Sentry 自建指南 用selenium完成自动化任务 用闲置的安卓手机做垃圾电话短信过滤 推荐三个时间管理工具 一次事故反思 当JS遇到uint64:JS整数溢出问题 SQLite3 存储以及ACID原理 Redis源码阅读:pub/sub实现 Redis源码阅读:zset实现 Redis源码阅读:bitmap 位图的运算 Redis源码阅读:set是怎么做交并集运算的?
Redis是如何工作的?
Jiajun Huang · 2020-05-21 · via Jiajun的技术笔记

黄健宏老师的《Redis源码剖析》采用自底向上的讲述方法,先从Redis的数据结构讲起。但是,Redis是怎么从监听端口到执行这些 命令的呢?本文记录了我探索这一问题的过程(Redis 6.0)。

如果是一个阻塞版本的echo服务器,大概长成这样:

# https://pymotw.com/3/socket/tcp.html
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(16)
            print('received {!r}'.format(data))
            if data:
                print('sending data back to the client')
                connection.sendall(data)
            else:
                print('no data from', client_address)
                break

    finally:
        # Clean up the connection
        connection.close()

可以看到,server是经历了 bind - listen - accept - recv 这几个步骤,然后才能拿到数据,再通过sendall写入数据,但是这是 一个阻塞版本的,也就是说,由于这里没有使用线程池,一次最多只能处理一个请求,大家排成队,一个一个来。

Redis可不能这样设计,为了处理高并发请求,Redis采用了I/O多路复用,在Linux下,就是epoll,但是Redis设计的时候就是想要在 多个系统下可以使用,因此它对I/O多路复用进行了一个封装,这个抽象对程序来说增加了跨平台能力,但是对于读代码的人来说却 增加了心智负担。总的来说,由于以下两点,增加了阅读难度:

  • 使用I/O多路复用,把本来连贯的代码拆成了各种回调函数,本来一整块的代码,变成了碎片
  • 抽象提高了软件健壮性,但是(相比面条式代码从上往下读过去即可)增加了心智负担

不过我们还是要克服这些困难,去理解Redis到底咋工作的。

读起来

首先我们要大概的了解一下Redis源码的结构,可以看一下README。然后我们要找到 main 函数所在处,因为在C里,main函数是 用户程序的入口,可以通过搜索,也可以通过gdb,搜索的话,因为Redis里有很多测试代码里都有main函数,所以可能不好找:

$ ack -Q 'int main' | wc
     31     174    1911

所以我们用gdb:

$ make -j8
$ gdb --args ./src/redis-server --port 6380
GNU gdb (Debian 8.2.1-2+b3) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./src/redis-server...done.
(gdb) 
(gdb) b main
Breakpoint 1 at 0x3a0c0: file server.c, line 4874.
 

就知道了,在 server.c 的 4874 行(行数可能不同,下同,因为我自己加了点私货代码在里面)。

可以喵一喵 main 函数的代码,不过没关系,肯定喵不出什么特别多的东西,但是大概知道就可以了:

  • 最后几行是 aeMain(server.el); 这里是事件循环,那么在此之前肯定有地方是初始化server
  • 往前翻可以翻到 initServer();

这两个都很长,说实话,我是看不出什么细节来的。所以,我得找一个命令,然后下个断点,接着来看函数调用栈,翻了一下 server.c 发现上面有一个这样的代码:

struct redisCommand redisCommandTable[] = {
    {"module",moduleCommand,-2,
     "admin no-script",
     0,NULL,0,0,0,0,0,0},

    {"get",getCommand,2,
     "read-only fast @string",
     0,NULL,1,1,1,0,0,0},

    /* Note that we can't flag set as fast, since it may perform an
     * implicit DEL of a large key. */
    {"set",setCommand,-3,
     "write use-memory @string",
     0,NULL,1,1,1,0,0,0},

这是Redis的命令,set,就是我们要找的,看看 setCommand 是个啥:

/* SET key value [NX] [XX] [KEEPTTL] [EX <seconds>] [PX <milliseconds>] */
void setCommand(client *c) {
    int j;
    ...

是个函数,非常好,给它下断点!

$ gdb --args ./src/redis-server --port 6380
GNU gdb (Debian 8.2.1-2+b3) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./src/redis-server...done.
(gdb) b setCommand 
Breakpoint 1 at 0x6a5d0: file t_string.c, line 103.
(gdb) run
Starting program: /home/jiajun/Code/redis/src/redis-server --port 6380
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
32392:C 21 May 2020 14:08:59.432 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32392:C 21 May 2020 14:08:59.432 # Redis version=5.9.101, bits=64, commit=96851c4e, modified=1, pid=32392, just started
32392:C 21 May 2020 14:08:59.432 # Configuration loaded
32392:M 21 May 2020 14:08:59.433 * Increased maximum number of open files to 10032 (it was originally set to 1024).

起一个 redis-cli 连上这个服务器,然后执行 set a b,这个时候就会卡在断点上:

Thread 1 "redis-server" hit Breakpoint 1, setCommand (c=0x7ffff791c300) at t_string.c:103
103	    for (j = 3; j < c->argc; j++) {
(gdb) bt
#0  setCommand (c=0x7ffff791c300) at t_string.c:103
#1  0x000055555559786d in call (c=0x7ffff791c300, flags=15) at server.c:3195
#2  0x000055555559813c in processCommand (c=c@entry=0x7ffff791c300) at server.c:3549
#3  0x00005555555a57e0 in processCommandAndResetClient (c=c@entry=0x7ffff791c300) at networking.c:1655
#4  0x00005555555a974f in processInputBuffer (c=0x7ffff791c300) at networking.c:1750
#5  0x0000555555623083 in callHandler (handler=0x5555555aa000 <readQueryFromClient>, conn=0x7ffff7815100) at connhelpers.h:78
#6  connSocketEventHandler (el=<optimized out>, fd=<optimized out>, clientData=0x7ffff7815100, mask=<optimized out>) at connection.c:276
#7  0x0000555555591654 in aeProcessEvents (eventLoop=eventLoop@entry=0x7ffff780b480, flags=flags@entry=11) at ae.c:459
#8  0x000055555559189b in aeMain (eventLoop=0x7ffff780b480) at ae.c:519
#9  0x000055555558e59b in main (argc=<optimized out>, argv=0x7fffffffe0b8) at server.c:5048

非常好,这就是我们要的东西,我们从上往下,依次找一下是怎么被调用的,然后再反过来看一下,就知道Redis是怎么依次把它们 设置好的(步骤我以代码注释的方式写):

/* SET key value [NX] [XX] [KEEPTTL] [EX <seconds>] [PX <milliseconds>] */
void setCommand(client *c) {
    int j;
    robj *expire = NULL;
    int unit = UNIT_SECONDS;
    int flags = OBJ_SET_NO_FLAGS;

    for (j = 3; j < c->argc; j++) {

// TODO 所以我们要看哪里调用的 setCommand,看调用栈:at server.c:3549

int processCommand(client *c) {
    moduleCallCommandFilters(c);
    ...
    } else {
    call(c,CMD_CALL_FULL);
    ...

// TODO 所以我们继续往上翻 networking.c:1655

int processCommandAndResetClient(client *c) {
    int deadclient = 0;
    server.current_client = c;
    if (processCommand(c) == C_OK) {

// TODO 继续 networking.c:1750

void processInputBuffer(client *c) {
    /* Keep processing while there is something in the input buffer */
    while(c->qb_pos < sdslen(c->querybuf)) {
    ...
    /* We are finally ready to execute the command. */
    if (processCommandAndResetClient(c) == C_ERR) {

// TODO 继续 connhelpers.h:78

static inline int callHandler(connection *conn, ConnectionCallbackFunc handler) {
    printf("=== callHandler\n");
    conn->flags |= CONN_FLAG_IN_HANDLER;
    if (handler) handler(conn);
    conn->flags &= ~CONN_FLAG_IN_HANDLER;
    if (conn->flags & CONN_FLAG_CLOSE_SCHEDULED) {
        connClose(conn);
        return 0;
    }
    return 1;
}

// 继续 connection.c:276

static void connSocketEventHandler(struct aeEventLoop *el, int fd, void *clientData, int mask)
{
    printf("=== connSocketEventHandler: fd: %d\n", fd);
    UNUSED(el);
    UNUSED(fd);
    connection *conn = clientData;
    ...
    /* Handle normal I/O flows */
    if (!invert && call_read) {
        if (!callHandler(conn, conn->read_handler)) return;
    }

// 继续 ae.c:459
            if (!invert && fe->mask & mask & AE_READABLE) {
                printf("=== handle readable event of fd %d\n", fd);
                fe->rfileProc(eventLoop,fd,fe->clientData,mask);
                fired++;
            }


// 到这里,似乎断掉了联系。我们回去看看 connSocketEventHandler 在哪些地方被调用了,搜索:
ConnectionType CT_Socket = {
    .ae_handler = connSocketEventHandler,
    .close = connSocketClose,
    .write = connSocketWrite,
    .read = connSocketRead,
    .accept = connSocketAccept,
    .connect = connSocketConnect,
    .set_write_handler = connSocketSetWriteHandler,
    .set_read_handler = connSocketSetReadHandler,
    .get_last_error = connSocketGetLastError,
    .blocking_connect = connSocketBlockingConnect,
    .sync_write = connSocketSyncWrite,
    .sync_read = connSocketSyncRead,
    .sync_readline = connSocketSyncReadLine
};

// 搜索 CT_Socket
connection *connCreateSocket() {
    connection *conn = zcalloc(sizeof(connection));
    conn->type = &CT_Socket;
    conn->fd = -1;

    return conn;
}

// 搜索 connCreateSocket
connection *connCreateAcceptedSocket(int fd) {
    connection *conn = connCreateSocket();
    conn->fd = fd;
    conn->state = CONN_STATE_ACCEPTING;
    return conn;
}

// 搜索 connCreateAcceptedSocket
void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    int cport, cfd, max = MAX_ACCEPTS_PER_CALL;
    char cip[NET_IP_STR_LEN];
    UNUSED(el);
    UNUSED(mask);
    UNUSED(privdata);

    while(max--) {
        cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
        if (cfd == ANET_ERR) {
            if (errno != EWOULDBLOCK)
                serverLog(LL_WARNING,
                    "Accepting client connection: %s", server.neterr);
            return;
        }
        serverLog(LL_VERBOSE,"Accepted %s:%d", cip, cport);
        acceptCommonHandler(connCreateAcceptedSocket(cfd),0,cip);
    }
}

// 搜索 acceptTcpHandler

void initServer(void) {
    int j;
    ...
    for (j = 0; j < server.ipfd_count; j++) {
        if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE,
            acceptTcpHandler,NULL) == AE_ERR)

到这里,就比较明了了,Redis首先在 initServer 里设置好 acceptTcpHandler,然后这个里面提供了回调,在建立好了连接之后, 根据不同的命令,调用不同的 xxxCommand 函数去处理。

好了,分析到此为止,至于Redis的数据结构嘛,还是看《Redis源码分析》或者算法书吧 :)