A minimal Redis-like cache server written in C.
This project is for people who want to understand how a networked cache works under the hood: sockets, select, RESP parsing, command dispatch, an in-memory key-value store, and TTL handling without hiding the interesting parts behind a framework.
Fork it if you want a compact C systems project that is still small enough to read in one sitting and practical enough to extend.
- Accepts TCP clients with a single-threaded
selectloop - Speaks the Redis Serialization Protocol subset used by normal Redis clients
- Supports
PING,SET,GET,DEL,EXPIRE, andTTL - Stores string keys and values in memory
- Deletes expired keys lazily when they are touched
- Includes focused C tests for the store, RESP parser, and command layer
Run these commands from the repository root:
cd server
make clean
make all
make test
./build/server 6379In another terminal, talk to it with redis-cli:
redis-cli -p 6379 PING
redis-cli -p 6379 SET hello world
redis-cli -p 6379 GET hello
redis-cli -p 6379 EXPIRE hello 10
redis-cli -p 6379 TTL hello
redis-cli -p 6379 DEL helloExpected replies:
PONG
OK
"world"
(integer) 1
(integer) 10
(integer) 1
Use the Makefile in server/. The project intentionally keeps the build simple and explicit.
cd server
make all # builds ./build/server
make test # runs all test binaries
make clean # removes generated build filesBefore opening a pull request or sharing a fork, run:
cd server
make clean && make all && make testThe generated server/build/ directory is ignored by git. Do not commit compiled binaries.
server/
Makefile
src/
main.c # CLI entrypoint: ./server <port>
headers/ # Public module interfaces
core/
server.c # Socket lifecycle and select loop
client.c # Per-client input buffers
resp.c # RESP parser and response formatting
commands.c # Command dispatch and command behavior
store.c # In-memory key-value store with TTL
tests/
test_store.c
test_resp.c
test_commands.c
The server currently supports RESP arrays of bulk strings, which is what redis-cli sends for normal commands.
Supported commands:
PING [message]SET key valueGET keyDEL key [key ...]EXPIRE key secondsTTL key
The following iterations will cover:
- Persistence
- Replication
- Authentication
- Transactions
- Inline Redis commands
- Advanced Redis data structures
Keep changes small and testable.
For behavior changes, add or update a test in server/tests/ first, then run:
cd server
make clean && make all && make testIf that command does not pass, the change is not ready to share.