Skip to content

JoeBosch/Redis-Go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis-Go

A lightweight Redis-like server implementation in Go that supports RESP2, pub/sub functionality, and key expiration.

Features

  • Basic Redis commands (PING, ECHO, GET, SET)
  • Thread-safe in-memory key-value storage
  • Key expiration with millisecond precision
  • Publish/Subscribe channel messaging (PUBLISH, SUBSCRIBE)
  • RESP2 (Redis Serialization Protocol) support

Why I Built This

I wanted to explore how Redis worked under the hood, especially in-memory databases, concurrent data access, subscribe publish. Redis is fast, powerful, and extremely common due to its ease of use. This project also was my first time using Go and helped me to get familiar with many of the key features.

Requirements

  • Go 1.24 or higher
  • Windows, Linux, or macOS

Building the Project

  1. Clone the repository:

git clone [your-repository-url]

  1. Build the project: go build -o build/Run.exe ./app

Running the Server

Start the server with default settings (port 6379):

./build/Run

Specify a custom address:

./build/Run -addr localhost:1234

Supported Commands

Basic Commands

  1. PING

    • Format: PING
    • Response: PONG
    *1\r\n$4\r\nPING\r\n
    
  2. ECHO

    • Format: ECHO message
    • Response: Returns the message
    *2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n
    
  3. SET

    • Format: SET key value [PX milliseconds]
    • Response: OK
    *3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n
    

    With expiry:

    *5\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n$2\r\npx\r\n$3\r\n100\r\n
    
  4. GET

    • Format: GET key
    • Response: Value or nil if key doesn't exist
    *2\r\n$3\r\nGET\r\n$3\r\nkey\r\n
    

Pub/Sub Commands

  1. SUBSCRIBE

    • Format: SUBSCRIBE channel
    • Response: Subscription confirmation
    *2\r\n$9\r\nSUBSCRIBE\r\n$7\r\nchannel\r\n
    
  2. PUBLISH

    • Format: PUBLISH channel message
    • Response: Number of clients message was delivered to
    *3\r\n$7\r\nPUBLISH\r\n$7\r\nchannel\r\n$7\r\nmessage\r\n
    

RESP Protocol Format

The server uses RESP2 (Redis Serialization Protocol) for communication. Basic formats:

  1. Simple Strings: +String\r\n
  2. Errors: -Error message\r\n
  3. Integers: :1000\r\n
  4. Bulk Strings: $5\r\nhello\r\n
  5. Arrays: *2\r\n$5\r\nhello\r\n$5\r\nworld\r\n
  6. Null: $-1\r\n

All inputs should be in the form of an array with bulk strings. The other types are for responses.

For more information: https://redis.io/docs/latest/develop/reference/protocol-spec/#resp-versions

Testing

Run all tests: go test -v ./app

Example Usage with NetCat

You can interact with the server using NetCat or Redis-cli:

ncat -C localhost 6379

redis-cli -h localhost -p 6379

Once connected, enter the command as an array of bulk strings including newlines.

Author

Joseph Bosch

Backend Developer — Specialized in SaaS, distributed systems design, and cloud infrastructure.

LinkedIn

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages