A lightweight Redis-like server implementation in Go that supports RESP2, pub/sub functionality, and key expiration.
- 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
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.
- Go 1.24 or higher
- Windows, Linux, or macOS
- Clone the repository:
git clone [your-repository-url]
- Build the project:
go build -o build/Run.exe ./app
Start the server with default settings (port 6379):
./build/Run
Specify a custom address:
./build/Run -addr localhost:1234
-
PING
- Format: PING
- Response: PONG
*1\r\n$4\r\nPING\r\n
-
ECHO
- Format: ECHO message
- Response: Returns the message
*2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n
-
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
-
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
-
SUBSCRIBE
- Format: SUBSCRIBE channel
- Response: Subscription confirmation
*2\r\n$9\r\nSUBSCRIBE\r\n$7\r\nchannel\r\n
-
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
The server uses RESP2 (Redis Serialization Protocol) for communication. Basic formats:
- Simple Strings:
+String\r\n
- Errors:
-Error message\r\n
- Integers:
:1000\r\n
- Bulk Strings:
$5\r\nhello\r\n
- Arrays:
*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n
- 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
Run all tests:
go test -v ./app
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.
Backend Developer — Specialized in SaaS, distributed systems design, and cloud infrastructure.