A Redis clone implemented in Go with support for basic Redis commands, RESP (Redis Serialization Protocol) protocol, and AOF (Append Only File) persistence. This implementation focuses on simplicity and reliability while maintaining compatibility with standard Redis clients.
- RESP Protocol Implementation: Full support for Redis wire protocol
- Command Support:
- String Operations:
GET,SET - Hash Operations:
HGET,HSET,HGETALL - Server Operations:
PING
- String Operations:
- Persistence: Append-Only File (AOF) based persistence with automatic background syncing
- Concurrent Access: Thread-safe operations using mutex locks
- Standard Compatibility: Works with official Redis CLI and clients
- Go 1.19 or higher
- Redis CLI (for testing)
- Clone the repository:
git clone https://github.com/Pasa1912/Redis
cd Redis- Start the server:
go run *.goThe server will start on port 6379 (default Redis port).
redis-cli# Ping the server
redis-cli> PING
"PONG"
# Set a key
redis-cli> SET mykey "Hello, GoRedis!"
"OK"
# Get a key
redis-cli> GET mykey
"Hello, GoRedis!"# Set hash fields
redis-cli> HSET user:1 name "John Doe"
"OK"
redis-cli> HSET user:1 email "john@example.com"
"OK"
# Get a specific hash field
redis-cli> HGET user:1 name
"John Doe"
# Get all hash fields
redis-cli> HGETALL user:1
1) "name"
2) "John Doe"
3) "email"
4) "john@example.com"The project is organized into several key components:
Redis/
├── main.go # Server initialization and connection handling
├── handler.go # Command implementations
├── aof.go # Persistence layer
├── serializer.go # RESP protocol serialization
└── deserializer.go # RESP protocol deserialization
The RESP protocol implementation supports five data types:
- Simple Strings ("+")
- Errors ("-")
- Integers (":")
- Bulk Strings ("$")
- Arrays ("*")
The AOF persistence implementation features:
- Automatic background syncing every second
- Atomic writes using mutex locks
- Crash recovery through AOF replay
- Append-only format for data integrity
Thread safety is ensured through:
- Read-Write mutexes for data stores
- Separate mutex for AOF operations
- Connection limiting (5 concurrent connections)
Key components and their responsibilities:
-
main.go:
- Server initialization
- Connection handling
- Command routing
-
handler.go:
- Command implementations
- Data store management
- Thread-safe operations
-
aof.go:
- Persistence management
- Background syncing
- Recovery operations
-
serializer.go & deserializer.go:
- RESP protocol implementation
- Data type handling
- Wire format processing
Contributions are welcome! Some areas for potential improvement:
- Additional Redis commands
- Cluster support
- RDB persistence format
- Command pipelining
- Transaction support
This project is licensed under the MIT License - see the LICENSE file for details.
Give a ⭐️ if this project helped you!
Made with ❤️ using Go
