A lightweight Redis CLI client built in C++.
This project implements a custom command-line Redis client that connects over TCP, formats commands in RESP, parses Redis responses, supports both one-shot command execution and interactive REPL, and includes basic Pub/Sub subscription handling.
- Connects to Redis using TCP sockets
- Supports both IPv4 and IPv6 (
AF_UNSPECaddress resolution) - Configurable host and port:
-h <host>(default:127.0.0.1)-p <port>(default:6379)
- Graceful disconnect handling
- Interactive REPL mode (default when no command args are passed)
- One-shot mode (execute one command and exit)
- Converts tokenized CLI input into Redis RESP command format:
*<argc>\r\n$<arg_len>\r\n<arg>\r\n
- Handles quoted command arguments in input parsing
Parses and prints:
- Simple Strings (
+) - Errors (
-) - Integers (
:) - Bulk Strings (
$) - Arrays (
*) - Nulls as
(nil)where applicable
- Readline-based input
- Command history support
- Built-in commands:
helpquitexit
- Detects
SUBSCRIBEcommand in REPL - Enters subscription mode
- Polls both terminal input and Redis socket simultaneously
- Streams incoming published messages
- Exits subscription mode with
exit/quit(sendsUNSUBSCRIBE)
redisCLI/
├── Client/
│ ├── main.cpp
│ ├── headers/
│ │ ├── CLI.h
│ │ ├── CommandHandler.h
│ │ ├── RedisClient.h
│ │ └── ResponseParser.h
│ └── src/
│ ├── CLI.cpp
│ ├── CommandHandler.cpp
│ ├── RedisClient.cpp
│ └── ResponseParser.cpp
├── Makefile
└── README.md
- Linux
g++(C++17)makelibreadline(development package)- Running Redis server (local or remote)
Example (Debian/Ubuntu):
sudo apt update
sudo apt install -y g++ make libreadline-dev redis-serverFrom project root:
makeOutput binary:
bin/my_redis_cli
Available Make targets:
make # release build (default)
make debug # debug build
make run # build + run
make clean # remove build and bin
make rebuild # clean + build
make help # list targets./bin/my_redis_cliCustom host and port:
./bin/my_redis_cli -h 127.0.0.1 -p 6379
./bin/my_redis_cli -h localhost -p 6379./bin/my_redis_cli PING
./bin/my_redis_cli INFO
./bin/my_redis_cli DBSIZE
./bin/my_redis_cli FLUSHDBOne-shot with custom host/port:
./bin/my_redis_cli -h 127.0.0.1 -p 6379 PING./bin/my_redis_cli SET mykey hello
./bin/my_redis_cli GET mykey
./bin/my_redis_cli SET greeting "hello world"
./bin/my_redis_cli APPEND greeting "!!!"
./bin/my_redis_cli STRLEN greeting
./bin/my_redis_cli DEL mykey./bin/my_redis_cli EXISTS greeting
./bin/my_redis_cli EXPIRE greeting 60
./bin/my_redis_cli TTL greeting
./bin/my_redis_cli TYPE greeting
./bin/my_redis_cli KEYS "*"./bin/my_redis_cli LPUSH mylist a b c
./bin/my_redis_cli RPUSH mylist d
./bin/my_redis_cli LRANGE mylist 0 -1
./bin/my_redis_cli LPOP mylist
./bin/my_redis_cli RPOP mylist./bin/my_redis_cli HSET user:1 name Alice age 24
./bin/my_redis_cli HGET user:1 name
./bin/my_redis_cli HGETALL user:1
./bin/my_redis_cli HKEYS user:1
./bin/my_redis_cli HVALS user:1./bin/my_redis_cli SADD tags redis cpp cli
./bin/my_redis_cli SMEMBERS tags
./bin/my_redis_cli SISMEMBER tags redis
./bin/my_redis_cli SREM tags cli./bin/my_redis_cli ZADD leaderboard 100 alice 80 bob 120 charlie
./bin/my_redis_cli ZRANGE leaderboard 0 -1 WITHSCORES
./bin/my_redis_cli ZREVRANGE leaderboard 0 -1 WITHSCORES
./bin/my_redis_cli ZSCORE leaderboard alice./bin/my_redis_cli MULTI
./bin/my_redis_cli SET tx:key 1
./bin/my_redis_cli INCR tx:key
./bin/my_redis_cli EXECTerminal 1 (subscriber in REPL):
./bin/my_redis_cliThen type:
SUBSCRIBE news
Terminal 2 (publisher):
./bin/my_redis_cli PUBLISH news "hello subscribers"
./bin/my_redis_cli PUBLISH news "another message"Back in terminal 1, type:
exit
to leave subscription mode.
Inside interactive mode:
help→ print client helpquitorexit→ close REPL
Start against local Redis:
make && ./bin/my_redis_cliRun quick health check:
./bin/my_redis_cli PINGRun against remote Redis:
./bin/my_redis_cli -h 10.0.0.25 -p 6379 PING- The client parses RESP2 response types.
- Command support is determined by your Redis server; this CLI forwards commands and prints parsed responses.
- If connection fails, appropriate errors are shown in the terminal.