Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis-like Server in Go

A lightweight, Redis-compatible server implementation written in Go. This project implements the core Redis protocol (RESP) and supports essential Redis commands with in-memory storage.

🚀 Features

  • Full RESP Protocol Support: Complete implementation of Redis Serialization Protocol
  • Concurrent Connections: Handles multiple clients simultaneously using goroutines
  • Thread-Safe Operations: Protected data stores with proper mutex locking
  • Redis-Compatible Commands: Supports essential Redis operations
  • In-Memory Storage: Fast key-value and hash data structures
  • TCP Server: Runs on Redis default port 6379

📋 Supported Commands

Key-Value Operations

  • PING [message] - Test connectivity
  • SET key value - Set a key-value pair
  • GET key - Retrieve a value by key

Hash Operations

  • HSET hash field value - Set a field in a hash
  • HGET hash field - Get a field from a hash
  • HGETALL hash - Get all fields and values from a hash

🏗️ Architecture

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   TCP Server    │ -> │  RESP Parser    │ -> │ Command Handler  │
│   (Port 6379)   │    │   (Reader)       │    │   (Dispatcher)   │
└─────────────────┘    └─────────────────┘    └─────────────────┘
                                                       │
                                                       ▼
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│  Key-Value      │    │     Hash        │    │   RESP Writer   │
│   Store         │    │    Store        │    │   (Response)    │
│ (map[string]    │    │(map[string]map[ │    └─────────────────┘
│     string)     │    │    string]string)│
└─────────────────┘    └─────────────────┘

Components

  1. Main Server (main.go): TCP listener and connection handling
  2. RESP Protocol (resp.go): Complete RESP parser and serializer
  3. Command Handler (handler.go): Command dispatch and data operations

🛠️ Installation

Prerequisites

  • Go 1.19 or later

Build and Run

# Clone the repository
git clone https://github.com/Avik-creator/redis_implementation.git
cd redis_implementation

# Build the server
go build -o redis-server

# Run the server
./redis-server

The server will start listening on port 6379 (Redis default port).

💻 Usage

Connect with Redis CLI

# Connect to the server
redis-cli -p 6379

# Test connection
127.0.0.1:6379> PING
PONG

# Set and get values
127.0.0.1:6379> SET name "Redis Go"
OK
127.0.0.1:6379> GET name
"Redis Go"

# Hash operations
127.0.0.1:6379> HSET user:1 name "John"
(integer) 1
127.0.0.1:6379> HSET user:1 age "30"
(integer) 1
127.0.0.1:6379> HGET user:1 name
"John"
127.0.0.1:6379> HGETALL user:1
1) "name"
2) "John"
3) "age"
4) "30"

Connect with Telnet

# Connect using telnet
telnet localhost 6379

# Send RESP commands (PING example)
*1\r\n$4\r\nPING\r\n
+PONG

# Send SET command
*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n
+OK

Connect Programmatically

package main

import (
    "net"
    "fmt"
    "bufio"
)

func main() {
    conn, _ := net.Dial("tcp", "localhost:6379")
    defer conn.Close()

    // Send PING
    conn.Write([]byte("*1\r\n$4\r\nPING\r\n"))

    // Read response
    reader := bufio.NewReader(conn)
    response, _ := reader.ReadString('\n')
    fmt.Println(response) // +PONG
}

🔧 Development

Project Structure

redis_go/
├── main.go        # Server entry point and connection handling
├── resp.go        # RESP protocol implementation
├── handler.go     # Command handlers and data stores
├── go.mod         # Go module definition
└── README.md      # This file

Adding New Commands

  1. Add command handler in handler.go:
func myCommand(args []Value) Value {
    // Implementation
    return Value{Type: SimpleString, Str: "OK"}
}
  1. Register in Handlers map:
var Handlers = map[string]HandlerFunc{
    // ... existing commands
    "MYCOMMAND": myCommand,
}

Running Tests

go test ./...

Building for Production

# Build optimized binary
go build -ldflags="-s -w" -o redis-server

# Cross-compile for different platforms
GOOS=linux GOARCH=amd64 go build -o redis-server-linux
GOOS=darwin GOARCH=arm64 go build -o redis-server-mac

📚 RESP Protocol

This implementation fully supports Redis RESP (Redis Serialization Protocol):

  • Simple Strings: +OK\r\n
  • Errors: -ERR message\r\n
  • Integers: :123\r\n
  • Bulk Strings: $5\r\nhello\r\n
  • Arrays: *2\r\n$3\r\nGET\r\n$3\r\nkey\r\n

⚡ Performance

  • Concurrent: Handles multiple connections simultaneously
  • Memory Efficient: Uses sync.RWMutex for read-heavy workloads
  • Zero Copy: RESP parsing minimizes allocations
  • Buffered I/O: Uses bufio for efficient TCP operations

🚨 Limitations

  • In-Memory Only: Data is lost on server restart
  • No Persistence: No RDB/AOF file support
  • Basic Commands: Limited to essential Redis operations
  • No Clustering: Single instance only
  • No Authentication: Open access (not production-ready)

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-command)
  3. Commit changes (git commit -am 'Add new command')
  4. Push to branch (git push origin feature/new-command)
  5. Create a Pull Request

Areas for Contribution

  • Add more Redis commands (DEL, EXPIRE, TTL, etc.)
  • Implement data persistence (RDB/AOF)
  • Add authentication and access control
  • Implement pub/sub functionality
  • Add clustering support
  • Performance optimizations
  • Comprehensive test suite

📄 License

This project is open source and available under the MIT License.

🙏 Acknowledgments

  • Inspired by the official Redis implementation
  • RESP protocol specification from Redis documentation
  • Go community for excellent networking libraries

📞 Support


Note: This is a learning project and not intended for production use. For production Redis deployments, use the official Redis server.

About

A Redis-compatible server in Go — full RESP protocol, concurrent clients over goroutines, thread-safe in-memory storage.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages