search
HomeDatabaseRedisWhat is the process of Redis request processing?

Overview

  • #The first is to register the processor;

  • Open the loop listening port, and create a Goroutine every time a connection is monitored ;

  • Then the Goroutine will wait in a loop to receive the request data, and then match the corresponding processor in the processor routing table according to the requested address, and then hand the request to the processor for processing ;

It’s expressed in code like this:

func (srv *Server) Serve(l net.Listener) error { 
    ...
    baseCtx := context.Background()  
    ctx := context.WithValue(baseCtx, ServerContextKey, srv)
    for {
        // 接收 listener 过来的网络连接
        rw, err := l.Accept()
        ... 
        tempDelay = 0
        c := srv.newConn(rw)
        c.setState(c.rwc, StateNew) 
        // 创建协程处理连接
        go c.serve(connCtx)
    }
}

It’s a little different for Redis, because it is single-threaded and cannot use multi-threading to handle connections. Therefore, Redis chooses to use an event driver based on the Reactor pattern to implement concurrent processing of events.

What is the process of Redis request processing?

The so-called Reactor mode in Redis is to monitor multiple fds through epoll. Whenever these fds respond, epoll will be notified in the form of events for callbacks. Each Each event has a corresponding event handler.

For example: accept corresponds to the acceptTCPHandler event handler, read & write corresponds to the readQueryFromClient event handler, etc., and then the event is assigned to the event processor for processing through the event loop dispatch.

So the above Reactor mode is implemented through epoll. For epoll, there are mainly three methods:

//创建一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大
int epoll_create(int size);

/*
 * 可以理解为,增删改 fd 需要监听的事件
 * epfd 是 epoll_create() 创建的句柄。
 * op 表示 增删改
 * epoll_event 表示需要监听的事件,Redis 只用到了可读,可写,错误,挂断 四个状态
 */
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

/*
 * 可以理解为查询符合条件的事件
 * epfd 是 epoll_create() 创建的句柄。
 * epoll_event 用来存放从内核得到事件的集合
 * maxevents 获取的最大事件数
 * timeout 等待超时时间
 */
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);

So we can implement a simple method based on these three methods Server:

// 创建监听
int listenfd = ::socket();

// 绑定ip和端口
int r = ::bind();  
// 创建 epoll 实例
int epollfd = epoll_create(xxx); 
// 添加epoll要监听的事件类型
int r = epoll_ctl(..., listenfd, ...);
 
struct epoll_event* alive_events =  static_cast<epoll_event*>(calloc(kMaxEvents, sizeof(epoll_event)));

while (true) {
    // 等待事件
    int num = epoll_wait(epollfd, alive_events, kMaxEvents, kEpollWaitTime);
	// 遍历事件,并进行事件处理
    for (int i = 0; i < num; ++i) {
        int fd = alive_events[i].data.fd;
        // 获取事件
        int events = alive_events[i].events;
		// 进行事件的分发
        if ( (events & EPOLLERR) || (events & EPOLLHUP) ) {
            ...
        } else  if (events & EPOLLRDHUP) {
            ...
        } 
        ...
    }   
}

Calling process

#So according to the above introduction, you can know that for Redis, an event loop is nothing more than a few steps:

  • Register event listening and callback functions;

  • Loop to wait for events to be acquired and processed;

  • Call the callback function to process data logic;

  • Write data back to Client;

What is the process of Redis request processing?

  • Register fd to epoll, And set the callback function acceptTcpHandler. If there is a new connection, the callback function will be called;

  • Start an infinite loop to call epoll_wait to wait and continue to process the event. Later we will return to the aeMain function to loop the call aeProcessEvents function;

  • When a network event comes, the callback function acceptTcpHandler will be called all the way to readQueryFromClient for data processing. readQueryFromClient will parse the client's data and find the corresponding cmd function. Execution;

  • After receiving the client request, the Redis instance will process the client command and write the returned data into the client output buffer instead of returning immediately;

  • Then the beforeSleep function will be called every time the aeMain function loops to write the data in the buffer back to the client;

The entire event above In fact, the code steps of the loop process have been written very clearly, and there are many articles on the Internet about it, so I won’t go into details.

Command Execution Process & Writeback Client

#Command Execution

# Let’s talk about something that is not mentioned in many articles on the Internet and see how Redis executes commands. , then store it in the cache, and write the data back to the client from the cache.

What is the process of Redis request processing?

We also mentioned in the previous section that if a network event comes, the readQueryFromClient function will be called, which is where the command is actually executed. We will just follow this method and look down:

  • readQueryFromClient will call the processInputBufferAndReplicate function to process the requested command;

  • In the processInputBufferAndReplicate function It will call processInputBuffer and determine whether the command needs to be copied to other nodes if it is cluster mode;

  • processInputBuffer function will loop through the requested command and call it according to the requested protocol processInlineBuffer function, after calling the redisObject object, processCommand is called to execute the command;

  • processCommand will use lookupCommand to find the corresponding command according to the command in the server.commands table when executing the command. Execute the function, and then after a series of verifications, call the corresponding function to execute the command, and call addReply to write the returned data into the client output buffer;

server. commands will register all Redis commands in the populateCommandTable function as a table that obtains command functions based on the command name.

For example, to execute the get command, the getCommand function will be called:

void getCommand(client *c) {
    getGenericCommand(c);
}

int getGenericCommand(client *c) {
    robj *o;
	// 查找数据
    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
        return C_OK;
    ...
}

robj *lookupKeyReadOrReply(client *c, robj *key, robj *reply) {
    //到db中查找数据
    robj *o = lookupKeyRead(c->db, key);
    // 写入到缓存中
    if (!o) addReply(c,reply);
    return o;
}

Find the data in the getCommand function, and then call addReply to write the returned data into the client output buffer .

Data write-back client

#After writing the command into the buffer, the data needs to be taken out from the buffer and returned to the client. For the process of writing data back to the client, it is actually completed in the event loop of the server.

What is the process of Redis request processing?

  • First of all, Redis will call the aeSetBeforeSleepProc function in the main function to register the function beforeSleep of the writeback package into the eventLoop;

  • Then when Redis calls the aeMain function for the event loop, it will determine whether beforesleep has been set. If so, it will call it;

  • beforesleep function will call Go to the handleClientsWithPendingWrites function, which calls writeToClient to write data back to the client from the buffer.

The above is the detailed content of What is the process of Redis request processing?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools