This project is a lightweight Redis server implementation written in Go. It supports basic Redis commands and uses the Redis Serialization Protocol (RESP) for message parsing. The server is designed to be efficient and easy to extend with additional Redis commands.
- RESP (Redis Serialization Protocol) message parsing
- Concurrent handling of client connections
- Support for basic Redis commands: ECHO, PING, SET, GET, EXISTS, DEL
- Extensible architecture for adding new commands
- Thread-safe operations on shared data
- Go 1.15 or higher
- Clone the repository:
git clone https://github.com/re1nger/redis-go-server.git
- Navigate to the project directory:
cd redis-go-server
To start the Redis server, run the following command:
go run tcp.go
By default, the server listens on localhost:6379
.
You can interact with the server using any Redis client. Here are some example commands you can use:
SET key value
GET key
EXISTS key
DEL key
PING
ECHO message
The server exposes two main interfaces: Conn
and C2
.
The Conn
interface represents a client connection and provides methods for writing responses:
type Conn interface {
WriteError(msg string)
WriteString(str string)
WriteBulkString(bulk string)
WriteInt(num int)
WriteRaw(b []byte)
WriteBytes(b []byte)
}
The C2
struct represents a command received from a client:
type C2 struct {
args [][]byte
Raw []byte
}
args
: A slice of byte slices representing the command and its argumentsRaw
: The raw byte data of the entire command
To add new commands, modify the main()
function in tcp.go
. Add a new case to the switch statement, implementing the desired functionality. For example:
case "newcommand":
// Implement your new command here
conn.WriteString("New command executed")
The server uses a concurrent model where each client connection is handled in a separate goroutine. It maintains a thread-safe map of key-value pairs for data storage.
Key components:
Server
: Manages the TCP listener and client connectionsconn
: Represents an individual client connectionReader
andWriter
: Handle RESP protocol parsing and response formatting
- No persistence mechanism
- Transactions, pub/sub, or clustering is not implemented yet
Future improvements could include:
- Adding data persistence
- Implementing Redis clustering capabilities
- Enhancing error handling and logging
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.