Suggest an editImprove this articleRefine the answer for “What does the event loop do in Redis?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The event loop in Redis is the central mechanism that handles all incoming and outgoing events: network connections, client commands, timers, and internal tasks; it provides Redis's asynchronous, non-blocking operation in single-threaded mode via I/O multiplexing (`epoll`, `kqueue`, `select`). **Key point:** Redis doesn't spin up a new thread per connection - every client is served in one thread, so while one client is waiting for a response, Redis can process other clients' commands, with no context switching between threads.Shown above the full answer for quick recall.Answer (EN)ImageThe **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: 1. waits for events (new connections, commands, readiness to send a response), 2. processes them, 3. 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), - `select` or `poll` (fallback options). ### 3. How the event loop runs, step by step Simplified, Redis's event loop works like this: 1. **poll()**, waits for activity on the sockets; 2. **accept()**, accepts new connections; 3. **read()**, reads data from clients; 4. **execute()**, runs the corresponding Redis command; 5. **write()**, sends the response to the client; 6. **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: 1. listens on every connection, 2. dispatches events, 3. executes commands sequentially, 4. manages internal tasks, 5. 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.