Databases 9 min read

Understanding Redis’s Single‑Threaded Model and File Event Handler (FEH)

The article explains how Redis implements a high‑performance single‑threaded architecture by using a Reactor‑style file‑event handler (FEH) built on I/O multiplexing, detailing socket events, the I/O multiplexing program, dispatcher, handler mapping, and the overall client‑server communication flow.

Top Architect
Top Architect
Top Architect
Understanding Redis’s Single‑Threaded Model and File Event Handler (FEH)

Hello, I am a top‑level architect.

1 Single‑Thread Model Design

Why the single‑thread model is efficient:

Pure in‑memory operations.

Based on non‑blocking I/O multiplexing.

Avoids frequent context switches of multi‑threading.

2 File Event Handler

Redis, using the Reactor pattern, has developed its own network event processor – the File Event Handler (FEH). FEH runs in a single thread, which is why Redis adopts a single‑threaded model.

2.1 Socket

A file event abstracts a socket operation. When a socket becomes ready for accept , read , write , close , etc., a file event is generated.

2.2 I/O Multiplexing Program

The I/O multiplexing program listens to multiple sockets, queues the events, and dispatches them one by one in a synchronous order.

I/O Multiplexing Implementation

Redis implements the I/O multiplexing program by wrapping common libraries such as select , epoll , evport , and kqueue . Each library has a dedicated source file, and the core file ae.c contains macros that select the highest‑performance library at compile time.

2.3 File Event Dispatcher

The dispatcher receives sockets from the I/O multiplexing program and, based on the event type, invokes the corresponding handler.

2.4 File Event Handler

Different sockets are associated with different handlers (functions) that define what the server should do when a specific event occurs.

Handler Mapping

Connection accept handler – for establishing client connections.

Command request handler – for processing commands sent by clients.

Command reply handler – for sending execution results back to clients.

Replication handler – for master‑slave replication operations.

2.5 Types of File Events

The I/O multiplexing program can monitor two event types defined in ae.h :

AE_READABLE – generated when a socket is readable (e.g., after write , close , or a new connection connect ).

AE_WRITABLE – generated when a socket is writable (e.g., after a client issues a read ).

If both events occur simultaneously, the dispatcher prioritizes AE_READABLE , meaning Redis reads before writing.

3 Summary

The overall client‑Redis communication process:

During Redis startup, the connection‑accept handler is bound to AE_READABLE .

When a client connects, an AE_READABLE event triggers the accept handler, which creates a socket and binds it to the command‑request handler.

Client requests generate AE_READABLE events, invoking the command‑request handler to read and execute commands.

When Redis prepares a response, it binds AE_WRITABLE to the command‑reply handler, which writes the response to the socket.

After the reply is fully written, the AE_WRITABLE mapping is removed.

RedisI/O multiplexingsingle-threadeddatabasesevent-driven
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.