Quick revision
The short answer
Redis moves file closing, AOF fsync and lazy freeing of large values to background (bio) threads, and it forks a child process for BGSAVE, AOF rewrites and full replica syncs. The main thread still runs active expiry, incremental rehashing, eviction and client timeout checks, and the fork call itself. Settings such as the lazyfree options, appendfsync everysec and io-threads move work off the main thread, but command execution always stays on it.
What to remember
In this answer, we saw that Redis is single-threaded for commands but not for everything. bio threads close files, fsync the AOF and free large values lazily, and child processes write snapshots and rewrite the AOF, although the fork call itself pauses the main thread. Active expiry, incremental rehashing, eviction and client timeouts still run on the main thread in small slices. The lazyfree settings, appendfsync everysec and I/O threads move work away from it, while commands and scripts always stay on it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Redis moves file closing, AOF fsync and lazy freeing of large values to background (bio) threads, and it forks a child process for BGSAVE, AOF rewrites and full replica syncs. The main thread still runs active expiry, incremental rehashing, eviction and client timeout checks, and the fork call itself. Settings such as the lazyfree options, appendfsync everysec and io-threads move work off the main thread, but command execution always stays on it.
Why this question is asked
Saying Redis is single-threaded is a useful first approximation, but anyone who has looked at top on a Redis host has seen several threads and, now and then, a second redis-server process. Senior interviews go one step further because the latency problems that are hardest to explain come from the work in between commands: a fork that pauses clients, an expiry cycle that runs longer than expected, or a delete that frees a million elements. In this answer, we will map which work runs in background threads, which runs in a child process, which still runs on the main thread, and which settings let us move expensive work away from it.
1A Quick Map of the Redis Process
A running redis-server has one main thread that runs the event loop and every command. Next to it are a few background threads, which the source code calls bio (background I/O) threads. When I/O threads are enabled, there are also several threads that only handle sockets. And from time to time Redis forks a child process, a copy of itself that shares memory pages with the parent until either side changes them.
The idea behind all of them is the same: anything slow that does not need to see a consistent, changing view of the data can be pushed away from the main thread, so clients keep getting answers.
- Main threadevent loop, every command, Lua, expiry, rehashing, eviction,
fork()call - I/O threads (optional)socket reads, request parsing, reply writes
- bio threadsclose files, AOF fsync for everysec, lazy freeing
- Child processRDB snapshot, AOF rewrite, data for full replica sync
4 layers, from the top down. Step through them one at a time.
The main thread owns the data; the other layers take work that can run without it.
2Background Threads: Closing, Syncing and Freeing
The bio threads do three jobs. The first is closing files, because closing a large file that was just unlinked can make the kernel free a lot of disk blocks, and that can take a while. The second is fsync for the AOF when appendfsync is everysec: the main thread writes to the file, and once per second a background thread asks the disk to flush it, so a slow disk does not stop commands (unless the fsync falls far behind, in which case Redis delays writes to keep the one-second promise).
The third job is lazy freeing. Deleting a key with ten million elements means freeing ten million allocations. DEL does that on the main thread. UNLINK removes the key from the keyspace immediately, so it is gone for every client, and hands the actual freeing to a bio thread. FLUSHALL ASYNC and FLUSHDB ASYNC do the same for a whole database. Small values are still freed inline, because handing them off costs more than freeing them.
3The lazyfree Settings
Many deletes are not caused by DEL at all. Redis deletes keys when it evicts them above maxmemory, when it expires them, when a command such as SET or RENAME overwrites an existing value, and when a replica flushes its data before a full sync. The lazyfree settings decide whether those implicit deletes free memory in the background.
The defaults differ between versions and between Redis and Valkey: older Redis versions shipped all of them as no, and newer releases have switched several on. So we should check our own version with CONFIG GET rather than assume. Turning them on costs almost nothing and removes a whole class of multi-second stalls.
4Child Processes and the Cost of fork
BGSAVE, BGREWRITEAOF and the snapshot for a full replica sync all fork a child. The child sees a frozen copy of memory and writes it to disk or to the socket while the parent keeps serving clients. Pages are shared through copy-on-write, so memory is only duplicated for pages the parent changes during the write.
But there is a catch: the fork call itself runs on the main thread, and the kernel must copy the parent's page tables before fork returns. Page tables grow with memory, roughly 2 MB of tables per GB of data with 4 KB pages, so a 30 GB instance can pause for tens or hundreds of milliseconds, more on some virtual machines. INFO stats reports the last pause as latest_fork_usec. No setting removes this pause; we make it smaller with smaller instances, or move it by taking snapshots on a replica.
5Housekeeping That Stays on the Main Thread
Several jobs touch the data structures directly, so they must run on the main thread between client requests. They run from serverCron and the event loop's before-sleep step, hz times per second; hz defaults to 10.
Active expiry samples keys that have a TTL (20 per round by default), deletes the expired ones, and repeats while more than about a quarter of the sample was expired, within a time budget. Incremental rehashing moves a few buckets of a growing dictionary to its new table on each pass, so a resize never happens all at once. Eviction runs before commands whenever used_memory is above maxmemory, and client timeout checks close idle connections. Each of these is designed to do a little work at a time, but under heavy expiry or eviction they still show up as latency, which is why LATENCY LATEST has events named expire-cycle and eviction-cycle.
6What I/O Threads Do and Do Not Move
I/O threads are off by default (io-threads 1). When we set io-threads to 4, for example, those threads take over reading from sockets, parsing requests and writing replies, which is often the biggest share of CPU for small commands. Command execution, Lua scripts and functions stay on the main thread, so atomicity is unchanged. In Redis 6 and 7, reads also needed io-threads-do-reads yes; Redis 8 and Valkey 8 reworked the threading model, so check your version's settings.
7Follow-up questions
What do you trade off when you raise hz?
A higher hz, say 50 or 100 instead of 10, runs serverCron more often, so expired keys are reclaimed sooner, client timeouts are noticed faster and each expiry cycle has a smaller slice of work. The cost is more CPU spent on housekeeping even when the server is idle, and more frequent short interruptions of command processing. Redis also has dynamic-hz, on by default, which raises the effective rate when there are many clients. Most deployments keep 10 and only raise it, after measuring, when memory is held too long by expired keys.
Does UNLINK make the key disappear later?
No. UNLINK removes the key from the keyspace immediately, so a GET right after it returns nil. Only the freeing of the memory happens later in a background thread.
Why not run expiry in a background thread too?
Expiry has to delete keys from the same dictionaries that commands read and write. Doing that from another thread would need locks around the keyspace, which is exactly what the single-threaded design avoids. Redis can, however, free the expired value in the background with lazyfree-lazy-expire.
8Conclusion
In this answer, we saw that Redis is single-threaded for commands but not for everything. bio threads close files, fsync the AOF and free large values lazily, and child processes write snapshots and rewrite the AOF, although the fork call itself pauses the main thread. Active expiry, incremental rehashing, eviction and client timeouts still run on the main thread in small slices. The lazyfree settings, appendfsync everysec and I/O threads move work away from it, while commands and scripts always stay on it.
Open with
Redis runs commands on one main thread, but the process has more. Background bio threads close files, do the AOF fsync when appendfsync is everysec, and free memory lazily for UNLINK and FLUSHALL or FLUSHDB ASYNC.
Then, if they want more
The lazyfree settings extend that to implicit deletes: lazyfree-lazy-eviction, lazy-expire, lazy-server-del, lazy-user-del, which turns DEL into UNLINK, and replica-lazy-flush. Defaults vary by version and between Redis and Valkey, so I check them with CONFIG GET.
BGSAVE, AOF rewrite and full replica syncs fork a child that writes the data, but the fork itself runs on the main thread and copies page tables, so a large instance pauses for a time that grows with memory; I watch latest_fork_usec and take snapshots on replicas if it hurts.
Some housekeeping stays on the main thread: active expiry in serverCron, which runs hz times a second and repeats while many sampled keys are expired, incremental rehashing, eviction above maxmemory and client timeout checks. Finally, I/O threads, off by default, only handle socket reads, parsing and writes; commands and Lua always run on the main thread.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Redis 6, Redis 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which work does Redis do off the main thread or in a child process, which background...": https://scrivio-interview-library.netlify.app/databases/redis-internals/66d72a0934/