Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A custom-built Redis clone implementing core features like in-memory data storage, RDB persistence, and master-slave replication.

License

Notifications You must be signed in to change notification settings

varunarora1606/Redix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔴 Redix – A Distributed In-Memory DataStore like Redis in Go

Redix is a distributed, in-memory key-value data store written from scratch in Go. It replicates the core functionality of Redis, including command parsing, RDB persistence, TCP networking, and master-slave replication — designed for high performance, concurrency, and a deeper understanding of distributed systems.


🚀 Features

  • ⚡ In-memory key-value store with TTL support
  • 💬 RESP (REdis Serialization Protocol) parser and encoder
  • 💾 RDB persistence with SAVE and BGSAVE
  • 🔄 Master-slave replication using REPLICAOF, PSYNC, REPLCONF, and WAIT
  • 🧠 Core Redis commands: PING, ECHO, SET, GET, DEL, KEYS, INFO, etc.
  • 🔌 Custom TCP server using Go's net package
  • 🔒 Role-based connection handling (client/master/slave)
  • 🧪 Clean modular codebase with extensibility in mind

📁 Directory Structure

Redix/
├── main.go              # Entry point of the Redis server
├── resp/                # RESP protocol parser and encoder
├── rdb/                 # RDB file handling (load/save/stream)
├── store/               # In-memory key-value store with TTL
├── testdata/            # Sample data directory (RDB files)

🛠️ Getting Started

Prerequisites

  • Go 1.20+
  • (Optional) redis-cli for interaction

Build

git clone https://github.com/varunarora1606/Redix.git
cd Redix
go build -o redix

Run

./redix --dir=./testdata --filename=dump.rdb --port=8000

⚙️ Usage

You can use redis-cli or telnet to interact with the server:

redis-cli -p 8000

Basic Commands

SET name varun
GET name
DEL name
KEYS *
SAVE
BGSAVE
INFO replication

🔁 Replication

Make a node a replica

REPLICAOF 127.0.0.1 8000

Stop replication (become master)

REPLICAOF NO ONE

Consistency

Ensure WAIT <replicas> <timeout-ms> is used to wait for ACKs from slaves:

WAIT 1 1000

💾 Persistence

  • SAVE: Synchronously saves the in-memory store to an RDB file
  • BGSAVE: Asynchronously saves in the background
  • Data is auto-loaded from the RDB file on server start

🧠 Supported Commands

Command Description
PING Health check
ECHO msg Echo the message
SET key val [PX ms] Set key with optional expiry
GET key Get value by key
DEL key Delete a key
KEYS * Get all keys
SAVE Save to RDB synchronously
BGSAVE Save to RDB asynchronously
INFO replication Get replication details
REPLICAOF host port Become replica of a master
WAIT n t Wait for n replicas or t ms
PSYNC Used during replication handshake
REPLCONF Used during replication handshake

🔧 Internals

  • Custom TCP connection handling
  • RESP parsing with proper protocol handling
  • Replication handshake protocol (PING, REPLCONF, PSYNC)
  • RDB file streaming during full sync
  • Offsets and replication IDs for syncing state
  • Periodic ACKs to maintain replication health

🧪 Example

Run master on port 8000:

./redix --port=8000

Run slave on port 8001:

./redix --port=8001

Connect to the slave:

redis-cli -p 8001
REPLICAOF 127.0.0.1 8000

Set key from master:

redis-cli -p 8000
SET hello world

Get key from slave:

redis-cli -p 8001
GET hello
# Output: "world"

👨‍💻 Author

Varun Arora
🔗 @VarunArora80243


📝 License

MIT License. See LICENSE for details.


💡 Acknowledgements


About

A custom-built Redis clone implementing core features like in-memory data storage, RDB persistence, and master-slave replication.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages