CodePark/ questions / What Redis is & how it works
START PRACTICINGPRACTICE
Backend question · Redis & caching › What Redis is & how it works

Why is Redis so fast, and is it really single-threaded?

Redis is fast because it keeps all its data in memory. Reads and writes never wait for the disk. Its command execution is single-threaded, but the Redis process uses more than one thread.

Why it is fast:

  • In memory: all data lives in RAM, so no command waits on the disk.
  • Simple data structures: they are tuned for memory, and most commands are O(1) or O(log N).
  • Event loop: Redis uses an event loop with I/O multiplexing. This means one thread watches many network connections at once and handles whichever one is ready. On Linux it uses epoll for this. So one thread can serve thousands of connections with no context switching and no locks.

Is it single-threaded? Command execution is. Redis runs commands one at a time, so a single command is always atomic: no other command can run in the middle of it. But the Redis process is not strictly one thread:

  • Background threads do some jobs, like freeing memory lazily after UNLINK and closing files.
  • Persistence work (saving data to disk) happens in a forked child process.
  • Since Redis 6, optional I/O threads can read and write socket data in parallel. (In Redis 6 and 7, reads use the threads only if io-threads-do-reads is turned on.)

Even with I/O threads, commands still run on the main thread.

  • Data lives in memory, so the hot path never touches the disk.
  • An event loop with I/O multiplexing serves many connections on one thread with no locking cost.
  • Commands run one at a time on the main thread, so each command is atomic.
  • The optional I/O threads in Redis 6+ only make network reads and writes parallel.
  • Background threads and forked child processes handle memory freeing and persistence.