Databases 16 min read

Redis Client/Server Interaction Process: A Step‑by‑Step Overview

This article provides a comprehensive, source‑code‑driven walkthrough of the Redis client‑server interaction, detailing the six main steps from socket establishment to command execution and response, while also covering auxiliary mechanisms such as beforeSleep processing, key expiration strategies, and the underlying epoll I/O multiplexing model.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Redis Client/Server Interaction Process: A Step‑by‑Step Overview

The author examined the Redis source code to present a systematic view of the client/server interaction, focusing on the write path and offering DBA‑oriented notes. The process is divided into six steps: client initiates a socket connection, server accepts the connection, the client writes the command, the server receives and processes it, the server returns the result, and the client receives the reply.

Step 1 – Client initiates socket connection : Using ./src/redis-cli -p 6379 -h 127.0.0.1 the client gathers parameters, selects a mode (e.g., stat mode), and calls cliConnect , which invokes redisConnect or redisConnectUnix for TCP or Unix sockets. For Unix sockets the command is ./src/redis-cli -s /tmp/redis.sock .

Step 2 – Server accepts socket connection : The server creates a listening socket (via socket() , bind() , listen() ) and registers an AE_READABLE event with aeCreateFileEvent . When a client connects, acceptTcpHandler or acceptUnixHandler is invoked.

Step 3 – Client starts writing : The client’s repl method (using the linenoise library) reads user input, passes it to cliSendCommand , which formats the command with redisFormatCommandArgv and appends it to the output buffer via __redisAppendCommand . The buffer is then written to the socket with write(c->fd, c->obuf, sdslen(c->obuf)) .

Step 4 – Server receives the write : The server’s event loop triggers readQueryFromClient , which reads data into c->querybuf ( nread = read(fd, c->querybuf+qblen, readlen) ), parses it with processInputBuffer , and eventually calls call to execute the command (e.g., setCommand for SET ).

Step 5 – Server returns the result : After command execution, the reply is placed in the client’s output buffer and sent back using write(fd, c->buf+c->sentlen, c->bufpos-c->sentlen) . The write event is then removed with aeDeleteFileEvent .

Step 6 – Client receives the reply : The client reads the reply via redisBufferRead ( nread = read(c->fd, buf, sizeof(buf)) ) and formats it for display with cliFormatReplyRaw , finally printing it with fwrite(out, sdslen(out), 1, stdout) .

Additional details include the beforeSleep routine (which performs fast key expiration, ACK handling for blocked clients, AOF flushing, and cluster maintenance), and the two key‑expiration mechanisms (active expiration in serverCron and passive checks during reads). The article also explains the epoll I/O multiplexing model used by Redis, showing the creation and registration of events with epoll_create , epoll_ctl , and epoll_wait , and how Redis selects the appropriate I/O backend (epoll, kqueue, select, etc.) based on the operating system.

databaseRedisepollsocketevent-loopClient-ServerKey Expiration
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.