What does the event loop do in Redis?
The event loop in Redis is the central mechanism that manages all incoming and outgoing events: network connections, client commands, timers, and internal tasks. It's what gives Redis its asynchronous, non-blocking operation in single-threaded mode.
1. The event loop's main role
The event loop runs an infinite cycle, in which Redis:
- waits for events (new connections, commands, readiness to send a response),
- processes them,
- goes back to waiting for the next event.
That's how Redis can serve thousands of clients at once without creating separate threads or processes.
2. What counts as an "event" in Redis
Every action is an event, placed into the loop's queue:
- data arriving from a client (a command);
- readiness to send a response to the client;
- a timer expiring (e.g. a key's TTL);
- internal tasks, replication, publications, lifetime checks.
Redis uses an event-multiplexing mechanism, built on OS system calls:
epoll(Linux),kqueue(BSD/macOS),selectorpoll(fallback options).
3. How the event loop runs, step by step
Simplified, Redis's event loop works like this:
- poll(), waits for activity on the sockets;
- accept(), accepts new connections;
- read(), reads data from clients;
- execute(), runs the corresponding Redis command;
- write(), sends the response to the client;
- cron(), runs internal periodic tasks (expiring TTLs, replication, statistics).
Then the cycle repeats indefinitely.
4. Why this is efficient
- Redis doesn't spawn a new thread per connection, unlike traditional servers.
- Every client is served in a single thread through I/O multiplexing.
- While one client is waiting for a response, Redis can process other clients' commands.
- There's no context switching between threads, which makes the system predictable and fast.
5. How it interacts with other components
- The main event loop handles the networking side and executes commands.
- Background threads handle heavy operations: saving to disk, freeing memory, replication.
- They run independently and don't block the event loop.
6. Summary
The event loop in Redis is the mechanism that:
- listens on every connection,
- dispatches events,
- executes commands sequentially,
- manages internal tasks,
- serves thousands of clients in a single thread.
That's what lets Redis stay a single-threaded, non-blocking, high-performance server with minimal overhead.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.