Simple Redis Client in Go
This challenge asks you to implement a basic Redis client in Go. Redis is an in-memory data store often used for caching and session management, and building a client allows you to interact with a Redis server programmatically. This exercise will test your understanding of network communication, protocol parsing, and data serialization in Go.
Problem Description
You are tasked with creating a simplified Redis client in Go that supports the SET, GET, and PING commands. The client should establish a connection to a Redis server, send commands, receive responses, and parse the responses to return the requested data. The client should handle basic errors, such as connection failures and invalid responses.
Key Requirements:
- Connection: Establish a TCP connection to a Redis server at a specified host and port.
- Command Sending: Construct Redis commands as strings and send them over the TCP connection.
- Response Receiving: Receive responses from the Redis server. Responses are expected to be strings.
- Response Parsing: Parse the received responses to extract the relevant data. For
GET, return the value associated with the key. ForPING, return "PONG". ForSET, acknowledge the command execution (no data needs to be returned). - Error Handling: Handle connection errors and invalid responses gracefully. Return appropriate error values when necessary.
Expected Behavior:
- The client should connect to the Redis server successfully.
SET key valueshould send the command to the server and returnOKupon successful execution.GET keyshould return the value associated with the key if it exists, otherwise return an empty string.PINGshould return "PONG".- The client should handle connection errors and invalid responses by returning an error.
Edge Cases to Consider:
- Redis server is unavailable (connection refused).
- Invalid commands are sent to the server.
- Key does not exist in Redis when
GETis called. - Server returns an unexpected response format.
Examples
Example 1:
Input: host = "localhost", port = 6379, command = "SET mykey hello"
Output: "OK"
Explanation: The client connects to the Redis server, sends the SET command, and receives "OK" in response.
Example 2:
Input: host = "localhost", port = 6379, command = "GET mykey"
Output: "hello"
Explanation: The client connects to the Redis server, sends the GET command, and receives "hello" in response, which was previously set.
Example 3:
Input: host = "localhost", port = 6379, command = "GET nonexistingkey"
Output: ""
Explanation: The client connects to the Redis server, sends the GET command, and receives an empty string because the key does not exist.
Example 4:
Input: host = "localhost", port = 6379, command = "PING"
Output: "PONG"
Explanation: The client connects to the Redis server, sends the PING command, and receives "PONG" in response.
Constraints
- The Redis server is assumed to be running on
localhost:6379by default, but the client should accept host and port as parameters. - The client should handle a maximum of 100 bytes for both the command and the response. Commands longer than this should result in an error.
- The client should be able to handle a single concurrent connection. No multi-threading is required.
- Error messages should be descriptive and informative.
Notes
- The Redis protocol is line-based. Commands and responses are terminated by
\r\n. - Focus on implementing the core functionality of sending commands and receiving responses. Advanced features like pipelining or scripting are not required.
- Consider using the
netpackage for TCP communication. - Error handling is crucial. Ensure your client gracefully handles potential errors.
- Start with the
PINGcommand to verify the connection before implementingSETandGET. - You don't need to implement a full-fledged client with all Redis commands. Focus on the specified commands.