Skip to content

ProgrammedByHussain/GoRedis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoRedis - Redis-like Key-Value Store

A lightweight, production-ready Redis-compatible key-value store implementation in Go. This server implements a subset of the RESP (REdis Serialization Protocol) and can be used with standard Redis clients.

Features

  • RESP Protocol Support: Implements Redis Serialization Protocol for compatibility with Redis clients
  • Concurrent Connections: Thread-safe handling of multiple client connections
  • Core Commands: Supports essential Redis commands:
    • SET - Store key-value pairs
    • GET - Retrieve values by key
    • HELLO - Server handshake command
    • CLIENT - Client management command
  • Graceful Shutdown: Proper cleanup of connections and resources
  • Context Support: Full context-based cancellation for request handling
  • Production Ready: Clean architecture, proper error handling, and structured logging

Architecture

/
├── cmd/
│   └── server/
│       └── main.go          # Application entry point
├── internal/
│   ├── server/
│   │   └── server.go        # Server lifecycle and connection management
│   ├── peer/
│   │   └── peer.go          # Client connection handling
│   ├── protocol/
│   │   ├── commands.go      # Command definitions
│   │   └── parser.go        # RESP protocol parsing
│   └── storage/
│       ├── kv.go            # Key-value store implementation
│       └── errors.go        # Storage error types
├── tests/
│   └── integration_test.go  # Integration tests
├── Makefile
├── go.mod
└── README.md

Component Overview

  • Server: Manages the TCP listener, peer connections, and command routing
  • Peer: Handles individual client connections and message reading
  • Protocol: Implements RESP protocol parsing and command definitions
  • Storage: Thread-safe in-memory key-value store

Installation

Prerequisites

  • Go 1.22 or later
  • Make (optional, for using Makefile commands)

Build from Source

# Clone the repository
git clone < https://github.com/ProgrammedByHussain/GoRedis >
cd goredisclone-master

# Build the server
make build
# or
go build -o bin/goredis ./cmd/server

Usage

Running the Server

Start the server with default settings (listens on port 5001):

make run
# or
./bin/goredis

Start the server on a custom port:

./bin/goredis --listenAddr :6379

Connecting with redis-cli

You can connect to the server using the official Redis CLI:

redis-cli -p 5001

Once connected, you can use supported commands:

> SET foo bar
OK
> GET foo
"bar"
> HELLO 3
%1
server:redis
> CLIENT SETNAME myclient
OK

Using with Redis Client Libraries

The server is compatible with standard Redis client libraries. Example using go-redis:

import (
    "context"
    "github.com/redis/go-redis/v9"
)

rdb := redis.NewClient(&redis.Options{
    Addr: "localhost:5001",
})

err := rdb.Set(context.Background(), "key", "value", 0).Err()
val, err := rdb.Get(context.Background(), "key").Result()

Configuration

Command-Line Options

  • --listenAddr: TCP address to listen on (default: :5001)
    • Example: --listenAddr :6379 or --listenAddr 0.0.0.0:5001

Environment Variables

Currently, configuration is done via command-line flags. Future versions may support environment variables.

Development

Running Tests

make test
# or
go test -v ./tests/...

Protocol Compatibility

This implementation supports a subset of the RESP protocol:

  • Simple Strings: +OK\r\n
  • Bulk Strings: $5\r\nhello\r\n
  • Arrays: *2\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n
  • Maps: %2\r\n$6\r\nserver\r\n$5\r\nredis\r\n

Supported Commands

Command Description Example
SET key value Store a key-value pair SET foo bar
GET key Retrieve a value by key GET foo
HELLO version Server handshake HELLO 3
CLIENT SETNAME name Set client name CLIENT SETNAME myclient

Acknowledgments

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published