Skip to content

Sagor0078/redis-clone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Redis Clone in Go

A lightweight, in-memory Redis clone built in Go🐹 over raw TCP, implementing core Redis functionality including transactions, pub/sub, expiration, and LRU eviction. System Architecture(Created by GPT-4o)


Features

Core Redis Commands

  • GET key
  • SET key value
  • DEL key
  • INCR key / DECR key
  • FLUSHALL
  • PING
  • QUIT

Expiration Support

  • EXPIRE key seconds β€” Set a timeout on a key
  • TTL key β€” Get remaining time to live
  • Automatic expiry with background eviction.

Transactions

  • MULTI β€” Start transaction
  • EXEC β€” Execute queued commands
  • DISCARD β€” Cancel transaction
  • Queues and executes atomic command blocks per connection

Publish/Subscribe

  • SUBSCRIBE channel
  • PUBLISH channel message
  • Real-time pub/sub system with multiple channels

LRU Cache Eviction

  • Auto-evicts least recently used keys when size threshold is exceeded
  • Built with container/list for efficient O(1) updates
  • Integrated into GET, SET, and DEL operations

RESP Protocol Support

  • Fully RESP-compliant parser (supports *, $, +, -, :)
  • Allows communication with Redis CLI or custom tools

Server

  • Listens on tcp://0.0.0.0:6380
  • Handles concurrent clients
  • Graceful error handling for malformed inputs

Project Structure

redis-clone/
β”œβ”€β”€ cmd/                         # Entrypoints for different binaries
β”‚   β”œβ”€β”€ bench/                   # Benchmark client
β”‚   β”‚   └── bench.go             # Runs benchmark tests on Redis clone
β”‚   └── server/                  # Main server entrypoint
β”‚       └── main.go              # Starts the Redis server
β”‚
β”œβ”€β”€ internal/                   # Core application logic, organized by domain
β”‚   β”œβ”€β”€ cache/                  # In-memory storage layer
β”‚   β”‚   β”œβ”€β”€ lru.go              # LRU eviction policy implementation
β”‚   β”‚   β”œβ”€β”€ lru_test.go
β”‚   β”‚   β”œβ”€β”€ store.go            # Key-value store with expiration
β”‚   β”‚   └── store_test.go
β”‚
β”‚   β”œβ”€β”€ command/                # RESP command parsing and execution
β”‚   β”‚   β”œβ”€β”€ handler.go          # Handles Redis commands: GET, SET, DEL, etc.
β”‚   β”‚   └── handler_test.go
β”‚
β”‚   β”œβ”€β”€ persistence/           # RDB/AOF Persistence mechanism
β”‚   β”‚   β”œβ”€β”€ rdb.go              # Dump/load logic for persistence
β”‚   β”‚   └── rdb_test.go
β”‚
β”‚   β”œβ”€β”€ protocol/              # RESP protocol handling
β”‚   β”‚   β”œβ”€β”€ buffer_writer.go    # Efficient buffered output
β”‚   β”‚   β”œβ”€β”€ parser.go           # RESP3-compatible parser
β”‚   β”‚   └── parser_test.go
β”‚
β”‚   β”œβ”€β”€ pubsub/                # Publish/Subscribe message broker
β”‚   β”‚   β”œβ”€β”€ pubsub.go
β”‚   β”‚   └── pubsub_test.go
β”‚
β”‚   β”œβ”€β”€ session/               # Connection/session management
β”‚   β”‚   β”œβ”€β”€ session.go
β”‚   β”‚   └── session_test.go
β”‚
β”‚   └── transaction/           # MULTI/EXEC transactions
β”‚       └── transaction.go
β”‚
β”œβ”€β”€ img/                        # Architecture or design diagrams
β”‚   β”œβ”€β”€ sys.png
β”‚   └── sys2.png
β”‚
β”œβ”€β”€ dump.rdb                    # Sample RDB file for persistence testing
β”œβ”€β”€ redis-clone                 # Built binary (created by Makefile)
β”œβ”€β”€ go.mod                      # Go module definition
β”œβ”€β”€ LICENSE
β”œβ”€β”€ Makefile                    # Automates build, test, benchmark, etc.
└── README.md                   # Project documentation

πŸ› οΈ How to Run

go run cmd/server/main.go

Connect using Redis CLI:

redis-cli -p 6380

Go CLI layout for benchnark:

go run cmd/benchmark/main.go -clients=50 -requests=100

Unit Test

Warning

Tests are currently under construction

Some parts of the system aren't fully covered yet, and there are a few known issues we're ironing out.

  • Running test for Cache package
go test ./internal/cache -v
  • Test file for the command package to test the Handle function,including GET, SET, SET EX, and DEL
go test ./internal/command -v
  • Running test for protocol package
go test ./internal/protocol -v
  • Running test for session package
go test ./internal/session -v
  • Running test for persistance package
go test ./internal/persistence -v
  • Running test for pubsub package
go test ./internal/pubsub -v

References

This project was inspired by a few projects, books and blog posts, it's based on them with things changed to the way I like

Contributing

Contribution Guidelines

  • Fork the repository
  • Create a new branch
# create a new branch
git checkout -b feature/my-update

# make your changes and push
git add .
git commit -m "Added my update"
git push origin feature/my-update
  • Write tests for new functionality (if possible)
  • Run tests locally
  • Submit a pull request with a clear description

πŸ“Œ Todo

  • Fix unit tests
  • Add proper LRU caching support
  • Add persistence (RDB/AOF)
  • Build a simple CLI client
  • Benchmarking tool

About

A lightweight, in-memory Redis clone built in Go🐹 over raw TCP, implementing core Redis functionality including transactions, pub/sub, expiration, and LRU eviction.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors