Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis Clone in Rust

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.


Features

Networking

  • TCP server using Tokio
  • Concurrent client handling
  • Async socket communication
  • Non-blocking I/O

RESP Protocol Support

  • Simple Strings
  • Errors
  • Integers
  • Bulk Strings
  • Arrays
  • Null values

Redis-like Commands

  • PING
  • SET
  • GET
  • DEL
  • TTL
  • DBSIZE
  • FLUSHALL

Key Expiration

  • EX support (seconds)
  • PX support (milliseconds)
  • Automatic cleanup of expired keys

Architecture

image

Tech Stack

Technology Purpose
Rust Systems programming language
Tokio Async runtime
Bytes Efficient byte storage
TCP Sockets Client-server communication

Project Structure

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 handling

How It Works

1. TCP Server

The server listens on:

127.0.0.1:6379

Whenever a new client connects:

  • A new async Tokio task is spawned
  • Each client gets handled independently
  • Multiple clients can interact simultaneously

2. RESP Protocol Parsing

Redis uses RESP (Redis Serialization Protocol).

Example request:

*2
$4
PING
$4
PONG

The parser:

  • Reads raw TCP bytes
  • Detects RESP data types
  • Converts them into Rust enums

3. In-Memory Database

Data is stored using:

HashMap<Bytes, ValueWithExpiry>

Wrapped inside:

Arc<Mutex<_>>

This allows:

  • Shared ownership
  • Thread safety
  • Concurrent access

Installation

Clone Repository

git clone https://github.com/yourusername/redis-clone-rust.git
cd redis-clone-rust

Build Project

cargo build

Run Server

cargo run

Server output:

Redis clone listening on 127.0.0.1:6379
Ready to accept connections!

Testing With redis-cli

Connect using:

redis-cli -p 6379

Example 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) 1

Example RESP Serialization

Rust Enum

RespValue::SimpleString("OK".to_string())

Serialized Output

+OK\r\n

Concurrency Model

This project uses:

tokio::spawn()

to handle clients concurrently.

Each connection:

  • Runs independently
  • Shares the same database
  • Uses thread-safe synchronization

Key Rust Concepts Used

  • Ownership & Borrowing
  • Arc
  • Mutex
  • Async/Await
  • Enums
  • Pattern Matching
  • TCP Networking
  • Shared State Management

Author

Anirudh Patwal

GitHub: https://github.com/yourusername


License

This project is licensed under the MIT License.

About

A basic Redis Clone in Rust

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages