Redis 是一个高性能服务端的典范。它通过多路复用 epoll 来管理海量的用户连接,只使用一个线程来通过事件循环来处理所有用户请求,就可以达到每秒数万 QPS 的处理能力。

aix运行redis redis io threads_任务队列

 

 一、多线程 Redis 服务启动

默认情况下多线程是默认关闭的。如果想要启动多线程,需要在配置文件中做适当的修改。相关的配置项是 io-threads 和 io-threads-do-reads 两个。

#vi /usr/local/soft/redis6/conf/redis.conf 
io-threads 4 #启用的 io 线程数量
io-threads-do-reads yes #读请求也使用io线程

其中 io-threads 表示要启动的 io 线程的数量。io-threads-do-reads 表示是否在读阶段也使用 io 线程,默认是只在写阶段使用 io 线程的。

1.1 主线程初始化

aix运行redis redis io threads_aix运行redis_02

 

 

  • 初始化读任务队列、写任务队列
  • 创建一个 epoll 对象
  • 对配置的监听端口进行 listen
  • 把 listen socket 让 epoll 给管理起来
//file: src/server.c
void initServer() {

    // 1 初始化 server 对象
    server.clients_pending_write = listCreate();
    server.clients_pending_read = listCreate();
    ......

    // 2 初始化回调 events,创建 epoll
    server.el = aeCreateEventLoop(server.maxclients+CONFIG_FDSET_INCR);

    // 3 绑定监听服务端口
    listenToPort(server.port,server.ipfd,&server.ipfd_count);

    // 4 注册 accept 事件处理器
    for (j = 0; j < server.ipfd_count; j++) {
        aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE,
            acceptTcpHandler,NULL);
    }
    ...
}

初始化 server 对象

在 initServer 的一开头,先是对 server 的各种成员变量进行初始化。值得注意的是 clients_pending_write 和 clients_pending_read 这两个成员,它们分别是写任务队列和读任务队列。将来主线程产生的任务都会放在放在这两个任务队列里。

主线程会根据这两个任务队列来进行任务哈希散列