A Redis-compatible server implementation in C, supporting basic RESP protocol commands.
- ✅ PING/PONG - Basic connectivity test
- ✅ SET - Store key-value pairs
- ✅ GET - Retrieve values by key
- ✅ Multi-threaded - Handles multiple concurrent clients
- ✅ RESP Protocol - Full RESP encoding/decoding support
- ✅ Thread-safe - Hash map with mutex protection
redis-c/
├── src/
│ ├── main.c # Server main loop and command handling
│ ├── decode.c # RESP protocol decoder
│ ├── encode.c # RESP protocol encoder
│ ├── hash_map.c # Thread-safe hash map implementation
│ ├── resp.h # RESP types and declarations
│ └── hash_map.h # Hash map declarations
├── CMakeLists.txt # CMake build configuration
└── run_server.sh # Helper script to run server
- CMake (version 3.13 or higher)
- GCC or compatible C compiler (C11 standard)
- redis-cli for testing
Install on Ubuntu/Debian:
sudo apt-get install cmake build-essential redis-tools# Clone or navigate to project directory
cd redis-c
# Build the project
mkdir -p build
cd build
cmake ..
make
cd ..The executable will be created at build/redis.
./run_server.sh./build/redisThe server will start and listen on port 6379. You'll see:
Redis server starting...
Server listening on port 6379
Ready to accept connections.
Press Ctrl+C to stop the server.
Use redis-cli to test the server. It's the official Redis command-line interface and handles RESP protocol automatically.
# Ubuntu/Debian
sudo apt-get install redis-tools
# macOS
brew install redisTerminal 1 - Start the server:
./run_server.shTerminal 2 - Connect with redis-cli:
redis-cli -p 6379Once connected, you can use normal Redis commands:
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET mykey "Hello World"
OK
127.0.0.1:6379> GET mykey
"Hello World"
127.0.0.1:6379> GET nonexist
(nil)
127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> GET foo
"bar"
127.0.0.1:6379> EXITYou can also run commands directly without entering interactive mode:
redis-cli -p 6379 PING
# Output: PONG
redis-cli -p 6379 SET mykey "Hello"
# Output: OK
redis-cli -p 6379 GET mykey
# Output: "Hello"redis-benchmark is the official Redis performance testing tool. It's much faster and more accurate than custom scripts.
# Ubuntu/Debian
sudo apt-get install redis-tools
# macOS
brew install redisTerminal 1 - Start the server:
./run_server.shTerminal 2 - Run benchmark:
# 1 million SET operations with 50 concurrent clients
redis-benchmark -h localhost -p 6379 -t set -n 1000000 -c 50
# 1 million GET operations
redis-benchmark -h localhost -p 6379 -t get -n 1000000 -c 50
# PING test
redis-benchmark -h localhost -p 6379 -t ping -n 100000 -c 50We provide a convenient wrapper script:
# Default: 1M SET requests with 50 clients
./tests/redis-benchmark.sh
# Custom options
./tests/redis-benchmark.sh -n 1000000 -c 50 -t set
./tests/redis-benchmark.sh -n 100000 -c 10 -t get
# Run all tests (PING, SET, GET)
./tests/redis-benchmark.sh --all
# See all options
./tests/redis-benchmark.sh --help====== SET ======
1000000 requests completed in 45.23 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save":
host configuration "appendonly": no
multi-thread: no
0.00% <= 1 milliseconds
95.50% <= 2 milliseconds
99.90% <= 3 milliseconds
100.00% <= 4 milliseconds
22105.12 requests per second
| Command | Description |
|---|---|
redis-benchmark -h localhost -p 6379 -t set -n 1000000 -c 50 |
1M SET operations, 50 clients |
redis-benchmark -h localhost -p 6379 -t get -n 1000000 -c 50 |
1M GET operations, 50 clients |
redis-benchmark -h localhost -p 6379 -t ping -n 100000 -c 50 |
100K PING operations |
redis-benchmark -h localhost -p 6379 -t set,get -n 100000 -c 10 |
Mixed SET/GET test |
Commands are sent in RESP (Redis Serialization Protocol) format:
*N- Array with N elements$N- Bulk string of length N+text- Simple string-text- Error:number- Integer
The command SET mykey "Hello World" in RESP format:
*3 ← Array with 3 elements
$3 ← Bulk string of length 3
SET ← Command name
$5 ← Bulk string of length 5
mykey ← Key
$11 ← Bulk string of length 11
Hello World ← Value
Note: You don't need to know RESP format when using redis-cli - it handles this automatically!
- Multi-threaded server using
pthreadfor concurrent client handling - Thread-safe hash map with mutex protection for GET/SET operations
- RESP parser with proper error handling and bounds checking
- Memory management with proper cleanup of all allocated resources
| Command | Description | redis-cli Example |
|---|---|---|
PING |
Returns PONG | PING |
SET key value |
Store key-value pair | SET foo bar |
GET key |
Retrieve value by key | GET foo |
- Invalid commands return error responses
- Incomplete messages are buffered until complete
- Protocol errors are handled gracefully
cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
makemain.c- Server socket handling, command processingdecode.c- RESP parsing from network bytesencode.c- RESP encoding to network byteshash_map.c- Dictionary/hash table implementation
# Find and kill existing process
lsof -ti:6379 | xargs kill -9
# Or
pkill -f redis- Make sure the server is running in another terminal
- Check that port 6379 is not blocked by firewall
- Verify server started successfully (check for "Server listening" message)
# Install redis-tools
sudo apt-get install redis-tools # Ubuntu/Debian
brew install redis # macOS- Check server logs for error messages
- Make sure you're using correct Redis command syntax
- Verify the server is running and listening on port 6379
.codecrafters/- Keep if submitting to CodeCrafters platform, otherwise not neededyour_program.sh- Keep if submitting to CodeCrafters platform, otherwise not neededCMakeLists.txt- Required for buildingsrc/- Source code (required)
run_server.sh- Helper script (optional, but recommended)tests/redis-benchmark.sh- Benchmark script using redis-benchmark (recommended)tests/stress-test.sh- Custom stress test script (alternative)build/- Build directory (auto-generated, can be deleted)
-
Build:
mkdir -p build && cd build && cmake .. && make && cd ..
-
Run server:
./run_server.sh
-
Test:
redis-cli -p 6379
Then type commands like
PING,SET foo bar,GET foo
This project is part of the CodeCrafters Redis challenge.