← All workEntry 6 of 6
  • C#
  • Sockets
  • RESP

Redis Clone

Redis, rebuilt to learn how a fast single-threaded server handles many clients at once.

§1

The question

Redis serves thousands of clients on one thread. How does that work, and what does it take to add blocking reads and replication on top?

§2

How it works

  1. Clientsmany connections
  2. Event loopone thread, Select
  3. RESP parsercommands in
  4. Storewrites → replicas
Fig. 1  One thread waits on every socket at once and handles whichever is ready, so no client blocks another.

Commands arrive in Redis's RESP protocol and run against an in-memory store. Blocking commands (BLPOP, XREAD BLOCK) park the client until data arrives or a timeout fires, without stopping the loop. Replicas connect with the real handshake and receive every write.

§3

Results

  • Strings, lists, streams and transactions (MULTI / EXEC)
  • Blocking reads: BLPOP and XREAD BLOCK
  • Master–replica replication
  • RDB persistence started, not finished
§4

What I learned

  • Event loops: how one thread serves many clients
  • Parsing a binary-safe protocol from a raw byte stream