A lightweight, Redis-compatible key-value cache server implemented in Go that supports basic Redis commands and the RESP (Redis Serialization Protocol).
-
Main Server (
main.go)- TCP server listening on port 6379 (Redis default)
- Handles client connections and command routing
- Implements the main event loop for processing client requests
-
Command Handler (
handler.go)- Maps Redis commands to handler functions
- Manages in-memory data structures with thread-safe operations
- Implements command validation and error handling
-
RESP Protocol Implementation (
resp.go)- Complete RESP (Redis Serialization Protocol) parser and serializer
- Supports all RESP data types: strings, bulk strings, arrays, integers, errors, and null values
- Handles both reading from and writing to client connections
-
AOF Persistence (
aof.go)- Append Only File implementation for data durability
- Logs every write operation to disk for crash recovery
- Thread-safe operations with automatic disk syncing
- Replays commands on server restart to restore state
- String Storage: Thread-safe map for key-value pairs using
sync.RWMutex - Hash Storage: Nested map structure for hash operations with concurrent access protection
- AOF Persistence: Append Only File logging for data durability and crash recovery
- Uses
sync.RWMutexfor concurrent read/write operations - Multiple readers can access data simultaneously
- Writers get exclusive access to prevent data races
- PING - Health check command
PING- Returns "PONG"PING <message>- Returns the provided message
-
SET - Set a key-value pair
SET <key> <value>- Stores key-value pair- Returns "OK" on success
-
GET - Retrieve a value by key
GET <key>- Returns the value for the given key- Returns null if key doesn't exist
-
HSET - Set a field in a hash
HSET <key> <field> <value>- Sets field-value pair in hash- Returns "OK" on success
-
HGET - Get a field from a hash
HGET <key> <field>- Returns the value for the field- Returns null if key or field doesn't exist
-
HGETALL - Get all fields and values from a hash
HGETALL <key>- Returns array of field-value pairs- Returns null if key doesn't exist
- COMMAND - Returns empty array (basic implementation)
- Go 1.25.2 or later
-
Clone or navigate to the project directory:
cd /path/to/redis -
Run the server:
go run . -
The server will start listening on port 6379:
listening on port 6379
You can test the server using the official Redis CLI:
# Connect to the server
redis-cli -p 6379
# Test basic commands
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET mykey "Hello World"
OK
127.0.0.1:6379> GET mykey
"Hello World"
127.0.0.1:6379> HSET user:1 name "John"
OK
127.0.0.1:6379> HGET user:1 name
"John"
127.0.0.1:6379> HGETALL user:1
1) "name"
2) "John"This implementation fully supports the RESP protocol, making it compatible with standard Redis clients. The protocol handles:
- Simple Strings:
+OK\r\n - Bulk Strings:
$5\r\nhello\r\n - Arrays:
*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n - Integers:
:1000\r\n - Errors:
-ERR error message\r\n - Null Values:
$-1\r\n
This implementation includes Append Only File (AOF) persistence for data durability:
- Command Logging: Every write operation (SET, HSET) is logged to disk immediately
- Append-Only: Commands are always appended to the end of the AOF file, never modified
- Crash Recovery: On server restart, all logged commands are replayed to restore exact state
- Thread-Safe: Uses mutex protection for concurrent access to AOF file
- Auto-Sync: Background goroutine syncs data to disk every second
- High Durability: Minimal data loss even on unexpected crashes
- Human Readable: AOF files contain actual Redis commands for debugging
- Incremental: Only new commands are appended, efficient storage
- Exact Recovery: Reconstructs the exact state before crash
The AOF file stores commands in RESP format, making it compatible with Redis clients:
*3
$3
SET
$3
key
$5
value
*4
$4
HSET
$4
user
$4
name
$4
John
redis/
├── main.go # Main server entry point
├── handler.go # Command handlers and data storage
├── resp.go # RESP protocol implementation
├── aof.go # AOF persistence implementation
├── go.mod # Go module definition
└── README.md # This file
This project serves as a learning implementation of Redis-like functionality and demonstrates:
- TCP server programming in Go
- Protocol implementation (RESP)
- Concurrent data access patterns
- Command pattern implementation
- Error handling and validation