A lightweight Redis-inspired in-memory database built from scratch in Rust using Tokio and the RESP protocol.
This project implements a multithreaded TCP server capable of handling multiple concurrent clients while supporting core Redis-like commands such as SET, GET, DEL, TTL, DBSIZE, and FLUSHALL.
- TCP server using Tokio
- Concurrent client handling
- Async socket communication
- Non-blocking I/O
- Simple Strings
- Errors
- Integers
- Bulk Strings
- Arrays
- Null values
PINGSETGETDELTTLDBSIZEFLUSHALL
EXsupport (seconds)PXsupport (milliseconds)- Automatic cleanup of expired keys
| Technology | Purpose |
|---|---|
| Rust | Systems programming language |
| Tokio | Async runtime |
| Bytes | Efficient byte storage |
| TCP Sockets | Client-server communication |
src/
│
├── main.rs # Entry point
├── resp.rs # RESP data types + serialization
├── parser.rs # RESP protocol parser
├── store.rs # In-memory database
├── commands.rs # Command handling logic
└── client.rs # TCP client handlingThe server listens on:
127.0.0.1:6379Whenever a new client connects:
- A new async Tokio task is spawned
- Each client gets handled independently
- Multiple clients can interact simultaneously
Redis uses RESP (Redis Serialization Protocol).
Example request:
*2
$4
PING
$4
PONGThe parser:
- Reads raw TCP bytes
- Detects RESP data types
- Converts them into Rust enums
Data is stored using:
HashMap<Bytes, ValueWithExpiry>Wrapped inside:
Arc<Mutex<_>>This allows:
- Shared ownership
- Thread safety
- Concurrent access
git clone https://github.com/yourusername/redis-clone-rust.git
cd redis-clone-rustcargo buildcargo runServer output:
Redis clone listening on 127.0.0.1:6379
Ready to accept connections!Connect using:
redis-cli -p 6379Example session:
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET name anirudh
OK
127.0.0.1:6379> GET name
"anirudh"
127.0.0.1:6379> DBSIZE
(integer) 1RespValue::SimpleString("OK".to_string())+OK\r\nThis project uses:
tokio::spawn()to handle clients concurrently.
Each connection:
- Runs independently
- Shares the same database
- Uses thread-safe synchronization
- Ownership & Borrowing
- Arc
- Mutex
- Async/Await
- Enums
- Pattern Matching
- TCP Networking
- Shared State Management
GitHub: https://github.com/yourusername
This project is licensed under the MIT License.