Skip to content

pushpitkamboj/OpenRedis

Repository files navigation

OpenRedis

OpenRedis is a small Redis-inspired server written in C++.

It speaks a narrow slice of the Redis protocol, listens on port 6379, and keeps data in memory. The goal here feels closer to "understand how Redis works by building the core loop yourself" than "replace Redis".

What is implemented

  • RESP array + bulk-string command parsing
  • TCP server on 0.0.0.0:6379
  • In-memory string storage
  • In-memory list storage
  • Per-client handling with detached threads

Supported commands

  • PING
  • PING <message>
  • ECHO <message>
  • SET <key> <value>
  • SET <key> <value> PX <milliseconds>
  • GET <key>
  • RPUSH <key> <value...>
  • LPUSH <key> <value...>
  • LRANGE <key> <start> <stop>
  • LLEN <key>

How much Redis is here?

A small but real subset.

  • Data types implemented: 2 Strings and lists.
  • Redis commands implemented: 8 PING, ECHO, SET, GET, RPUSH, LPUSH, LRANGE, LLEN.
  • Extra Redis-style option implemented: 1 PX on SET.

How much Redis is not here?

Not implemented:

  • Persistence (RDB, AOF)
  • Replication, Sentinel, Cluster
  • Transactions (MULTI / EXEC)
  • Pub/Sub
  • Streams
  • Hashes, sets, sorted sets
  • Auth, ACLs, config, Lua scripting, modules
  • Common commands like DEL, EXPIRE, TTL, INCR, MGET, TYPE, EXISTS
  • Automated tests in this repo

Behavioral gaps vs real Redis:

  • Expiration is only handled for SET ... PX ..., and keys are cleaned up lazily on GET
  • TTL is only used for string keys, not list keys
  • Shared state is global and not protected with locks, so concurrent access is not production-safe (we use multithreading, instead of async approach for serving multiple users)
  • The server reads into a fixed 1024-byte buffer and assumes a full command arrives cleanly
  • String keys and list keys live in separate maps, so wrong-type errors are not enforced like real Redis

Run it

Option 1: native

This repo already expects cmake and vcpkg.

Prerequisites:

  • a C++23-capable compiler
  • cmake
  • vcpkg
  • VCPKG_ROOT set to your local vcpkg checkout

Example:

export VCPKG_ROOT=/path/to/vcpkg
./program.sh

That builds the server and starts it on port 6379.

Option 2: Docker

If you want the most reproducible path, use Docker:

docker build -t openredis .
docker run --rm -it -p 6379:6379 openredis

If port 6379 is already busy on your machine, map another host port instead:

docker run --rm -it -p 6380:6379 openredis

Try it

With redis-cli if you already have it

redis-cli ping
redis-cli echo hello
redis-cli set name pushpit
redis-cli get name
redis-cli set session token PX 1000
redis-cli rpush tasks one two three
redis-cli lrange tasks 0 -1
redis-cli llen tasks

With raw RESP over nc

This works without any Redis tooling.

printf '*1\r\n$4\r\nPING\r\n' | nc localhost 6379
printf '*2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n' | nc localhost 6379
printf '*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n' | nc localhost 6379
printf '*2\r\n$3\r\nGET\r\n$3\r\nfoo\r\n' | nc localhost 6379
printf '*5\r\n$3\r\nSET\r\n$7\r\nsession\r\n$3\r\nabc\r\n$2\r\nPX\r\n$4\r\n1500\r\n' | nc localhost 6379
printf '*4\r\n$5\r\nRPUSH\r\n$5\r\nqueue\r\n$3\r\none\r\n$3\r\ntwo\r\n' | nc localhost 6379
printf '*4\r\n$6\r\nLRANGE\r\n$5\r\nqueue\r\n$1\r\n0\r\n$2\r\n-1\r\n' | nc localhost 6379
printf '*2\r\n$4\r\nLLEN\r\n$5\r\nqueue\r\n' | nc localhost 6379

How to test it today

There is no automated test suite in the repo right now, so the practical test loop is:

  1. start the server
  2. send commands with redis-cli or nc
  3. check the returned RESP output and server logs

A quick manual checklist:

  1. PING returns PONG
  2. ECHO hello returns hello
  3. SET followed by GET returns the stored value
  4. SET key value PX 1000, wait a second, then GET key returns null
  5. RPUSH or LPUSH changes list length
  6. LRANGE key 0 -1 returns the full list
  7. LLEN key matches the number of pushed items

Project shape

  • src/Server.cpp: socket server and shared in-memory stores
  • src/command_parser.cpp: RESP command parsing and command dispatch
  • src/commands/: per-command handlers
  • src/include/resp.h and src/include/resp.cpp: RESP read/write helpers

About

Built in-memory database in C++ from scratch.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors