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.
- 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
PING [message]- Test connectivitySET key value- Set a key-value pairGET key- Retrieve a value by key
HSET hash field value- Set a field in a hashHGET hash field- Get a field from a hashHGETALL hash- Get all fields and values from a hash
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 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)│
└─────────────────┘ └─────────────────┘
- Main Server (
main.go): TCP listener and connection handling - RESP Protocol (
resp.go): Complete RESP parser and serializer - Command Handler (
handler.go): Command dispatch and data operations
- Go 1.19 or later
# 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-serverThe server will start listening on port 6379 (Redis default port).
# 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 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
+OKpackage 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
}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
- Add command handler in
handler.go:
func myCommand(args []Value) Value {
// Implementation
return Value{Type: SimpleString, Str: "OK"}
}- Register in
Handlersmap:
var Handlers = map[string]HandlerFunc{
// ... existing commands
"MYCOMMAND": myCommand,
}go test ./...# 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-macThis 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
- 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
- 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)
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-command) - Commit changes (
git commit -am 'Add new command') - Push to branch (
git push origin feature/new-command) - Create a Pull Request
- 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
This project is open source and available under the MIT License.
- Inspired by the official Redis implementation
- RESP protocol specification from Redis documentation
- Go community for excellent networking libraries
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Note: This is a learning project and not intended for production use. For production Redis deployments, use the official Redis server.