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".
- 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
PINGPING <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>
A small but real subset.
- Data types implemented:
2Strings and lists. - Redis commands implemented:
8PING,ECHO,SET,GET,RPUSH,LPUSH,LRANGE,LLEN. - Extra Redis-style option implemented:
1PXonSET.
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 onGET - 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
This repo already expects cmake and vcpkg.
Prerequisites:
- a C++23-capable compiler
cmakevcpkgVCPKG_ROOTset to your localvcpkgcheckout
Example:
export VCPKG_ROOT=/path/to/vcpkg
./program.shThat builds the server and starts it on port 6379.
If you want the most reproducible path, use Docker:
docker build -t openredis .
docker run --rm -it -p 6379:6379 openredisIf port 6379 is already busy on your machine, map another host port instead:
docker run --rm -it -p 6380:6379 openredisredis-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 tasksThis 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 6379There is no automated test suite in the repo right now, so the practical test loop is:
- start the server
- send commands with
redis-cliornc - check the returned RESP output and server logs
A quick manual checklist:
PINGreturnsPONGECHO helloreturnshelloSETfollowed byGETreturns the stored valueSET key value PX 1000, wait a second, thenGET keyreturns nullRPUSHorLPUSHchanges list lengthLRANGE key 0 -1returns the full listLLEN keymatches the number of pushed items
src/Server.cpp: socket server and shared in-memory storessrc/command_parser.cpp: RESP command parsing and command dispatchsrc/commands/: per-command handlerssrc/include/resp.handsrc/include/resp.cpp: RESP read/write helpers