Skip to content

learnwithparam/build-your-own-redis-nodejs

Repository files navigation

Build your own Redis in Node.js

Sibling to the Python and Go workshops. Same architectural progression, idiomatic Node.js: net.createServer, Buffer, Map, setInterval, async/await, event-driven everywhere.

The build

# Step New idea LOC
01 TCP echo net.createServer + data event ~30
02 RESP parser Buffer + cursor + recursive parser ~110
03 GET / SET Map + dispatch object, single-thread = no locks ~110
04 Expiry Date.now() + setInterval for active sweep ~140
05 AOF persistence fs.createWriteStream + readFileSync replay ~150
06 RDB snapshots fs.writeFile + fs.rename, async BGSAVE ~130
07 Pub/Sub Map<channel, Set>, conn.write fan-out ~140
08 Replication SYNC + write streaming via net.connect ~150
09 Graceful shutdown process.on(SIGTERM) + drain loop ~110
10 Capstone benchmark Async worker pool, measure vs real Redis ~110

Each step folder ships with:

File Purpose
server.mjs (or bench.mjs in step 10) The runnable server
README.md Run instructions and what's new
GUIDE.md Deep-dive on the Node-specific idiom
EXERCISES.md 4 hands-on tasks

Getting started

Prerequisites

  • Node.js 20+ (uses node: import scheme and parseArgs)
  • Optional: redis-cli for talking to the server

Setup

git clone https://github.com/learnwithparam/build-your-own-redis-nodejs.git
cd build-your-own-redis-nodejs
make 01-tcp-echo

In another terminal:

nc localhost 6380
> hello
< hello

Run any step

make 01-tcp-echo
make 02-resp-parser
make 03-get-set
make 04-expiry
make 05-aof-persistence
make 06-rdb-snapshots
make 07-pubsub
make 08-replication
make 09-graceful-shutdown
make 10-capstone

Sibling courses

Same architecture in other languages:

Each language exposes different idioms for the same shape:

Concept Python Go Node
Concurrency Threads (step 7+) or selectors (step 9) Goroutines from step 1 Event loop, single-thread
Shared state dict + lock map + sync.RWMutex Map (no lock needed)
Background work threading.Thread or asyncio go func() setInterval, async functions
BGSAVE os.fork() with COW Snapshot + goroutine save async fs.writeFile
Graceful shutdown signal.signal + flag context.Context + WaitGroup process.on(SIGTERM) + drain

Verifying your setup

make test

Runs an automated structural test suite.

License

MIT.

About

Build your own Redis from scratch in Node.js. 10 incremental steps: TCP echo → RESP → KV → expiry → AOF → RDB → pub/sub → replication → graceful shutdown → benchmark. Sibling of build-your-own-redis-python and build-your-own-redis-go.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors