Understanding Redis Core Network Module and Its High‑Performance Event Loop
This article explains how Redis achieves high‑performance single‑threaded networking by leveraging Linux epoll for I/O multiplexing, detailing the server initialization, event‑loop architecture, connection handling, command processing, and write‑back mechanisms with code examples, providing a deep dive for backend developers.
1. Introduction
The author shares a personal story about moving from client‑side development to server‑side programming and being asked how to handle thousands of concurrent connections, leading to the motivation for exploring Redis's single‑threaded network design.
2. Understanding I/O Multiplexing (epoll)
Before diving into Redis, the article introduces the concept of I/O multiplexing using epoll. It explains that traditional blocking models waste CPU on context switches, while epoll acts like a shepherd dog that monitors many sockets with a single thread, allowing one process to handle thousands of connections efficiently.
3. Redis Server Startup Initialization
Redis’s entry point is in src/server.c. The main function calls initServer() and then enters the event loop via aeMain(server.el). The initialization performs three key steps:
Create an epoll object.
Bind and listen on the configured port(s).
Register the accept handler for the listening socket.
# git clone https://github.com/redis/redis
# cd redis
# git checkout -b 5.0.0 5.0.0 int main(int argc, char **argv) {
initServer();
aeMain(server.el);
}The initServer function creates the event loop with aeCreateEventLoop, calls listenToPort to bind sockets, and registers the accept callback acceptTcpHandler for each listening fd.
3.1 Creating the epoll Object
aeCreateEventLoopallocates an aeEventLoop structure and calls aeApiCreate, which ultimately invokes epoll_create(1024) to obtain the kernel epoll descriptor.
3.2 Binding the Listening Port
The function listenToPort loops over configured bind addresses and calls anetTcpServer, which eventually executes the bind and listen system calls.
3.3 Registering the Accept Event
For each listening socket, aeCreateFileEvent registers acceptTcpHandler as the readable callback. This stores the callback in the aeFileEvent array inside the event loop.
4. Redis Event‑Loop Processing (aeMain)
The core loop repeatedly:
Calls beforeSleep to flush any pending write buffers.
Invokes aeProcessEvents, which wraps epoll_wait to discover readable/writable events.
Handles new connections via acceptTcpHandler, creating a redisClient object and registering its read handler readQueryFromClient.
Processes client commands: parsing, command lookup, and execution (e.g., GET via getCommand).
Adds command results to the client’s reply buffer and enqueues the client in server.clients_pending_write.
4.1 Handling Readable Events
readQueryFromClientreads the request, calls processCommand, which looks up the command in redisCommandTable and invokes the associated handler (e.g., getCommand). The result is placed into the reply buffer via addReplyBulk and addReply.
4.2 Write‑Back Mechanism
Before each epoll_wait, beforeSleep runs handleClientsWithPendingWrites. It iterates over server.clients_pending_write, attempts to write the reply using writeToClient, and if the socket’s send buffer is full, re‑registers a writable event ( sendReplyToClient) so the next epoll_wait will continue sending.
5. Summary
Redis achieves remarkable single‑threaded performance by:
Using epoll for scalable I/O multiplexing.
Keeping the entire request‑response pipeline inside a compact event loop.
Deferring write operations to a pending‑write queue and handling partial writes gracefully.
Understanding initServer and aeMain provides a solid foundation for backend developers interested in high‑performance network programming.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Refining Core Development Skills
Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
