Skip to content
DrunkTraderPublic

About

a redis cli implementation in C++17

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

mazoni-redis CLI

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.

Capabilities

1. Connection and Networking

  • Connects to Redis using TCP sockets
  • Supports both IPv4 and IPv6 (AF_UNSPEC address resolution)
  • Configurable host and port:
    • -h <host> (default: 127.0.0.1)
    • -p <port> (default: 6379)
  • Graceful disconnect handling

2. Command Modes

  • Interactive REPL mode (default when no command args are passed)
  • One-shot mode (execute one command and exit)

3. RESP Command Encoding

  • 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

4. Redis Response Parsing (RESP2)

Parses and prints:

  • Simple Strings (+)
  • Errors (-)
  • Integers (:)
  • Bulk Strings ($)
  • Arrays (*)
  • Nulls as (nil) where applicable

5. REPL Experience

  • Readline-based input
  • Command history support
  • Built-in commands:
    • help
    • quit
    • exit

6. Pub/Sub Subscription Flow

  • Detects SUBSCRIBE command in REPL
  • Enters subscription mode
  • Polls both terminal input and Redis socket simultaneously
  • Streams incoming published messages
  • Exits subscription mode with exit / quit (sends UNSUBSCRIBE)

Project Structure

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

Requirements

  • Linux
  • g++ (C++17)
  • make
  • libreadline (development package)
  • Running Redis server (local or remote)

Example (Debian/Ubuntu):

sudo apt update
sudo apt install -y g++ make libreadline-dev redis-server

Build and Run

From project root:

make

Output 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

Redis CLI Usage Examples

A. Start interactive mode

./bin/my_redis_cli

Custom host and port:

./bin/my_redis_cli -h 127.0.0.1 -p 6379
./bin/my_redis_cli -h localhost -p 6379

B. One-shot mode (single command)

./bin/my_redis_cli PING
./bin/my_redis_cli INFO
./bin/my_redis_cli DBSIZE
./bin/my_redis_cli FLUSHDB

One-shot with custom host/port:

./bin/my_redis_cli -h 127.0.0.1 -p 6379 PING

C. String operations

./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

D. Key operations

./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 "*"

E. List operations

./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

F. Hash operations

./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

G. Set operations

./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

H. Sorted Set operations

./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

I. Transactions

./bin/my_redis_cli MULTI
./bin/my_redis_cli SET tx:key 1
./bin/my_redis_cli INCR tx:key
./bin/my_redis_cli EXEC

J. Pub/Sub examples

Terminal 1 (subscriber in REPL):

./bin/my_redis_cli

Then 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.

K. REPL built-ins

Inside interactive mode:

  • help → print client help
  • quit or exit → close REPL

Common Run Patterns

Start against local Redis:

make && ./bin/my_redis_cli

Run quick health check:

./bin/my_redis_cli PING

Run against remote Redis:

./bin/my_redis_cli -h 10.0.0.25 -p 6379 PING

Notes

  • 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.

About

a redis cli implementation in C++17

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages