A simple Redis-like server implemented in Rust with TCP protocol support. This implementation provides an in-memory key-value store with basic Redis commands.
- TCP server listening for connections
- Simple text-based protocol for commands
- In-memory hash map for storing key-value pairs
- Basic Redis commands: GET, SET, DEL, EXISTS, PING, KEYS, EXPIRE, TTL, FLUSHALL
- Key expiration (TTL) support
- Simple pattern matching for KEYS command
- Rust and Cargo (latest stable version recommended)
Clone this repository:
git clone https://github.com/yourusername/redis-rust.git
cd redis-rustStart the Redis server with the convenience script:
./start_server.shThe script accepts the following options:
--port=PORT- Set the Redis server port (default: 6379)--log=LEVEL- Set logging level (default: info)
Or run the server directly:
cargo runBy default, the server will listen on 127.0.0.1:6379. You can change the port by setting the REDIS_PORT environment variable:
REDIS_PORT=6380 cargo runYou can also configure logging level by setting the RUST_LOG environment variable:
RUST_LOG=debug cargo runThis project includes a simple test client:
cargo run --bin clientFor a quick demonstration of the server's capabilities, run:
./redis_demo.shThe demo script will run various Redis commands and display their outputs. You need to have the server running separately before running the demo.
SET key value- Set a key-value pairSET key value EX seconds- Set a key with an expiration timeGET key- Get the value for a keyDEL key- Delete a keyEXISTS key- Check if a key exists (returns 1 if exists, 0 if not)EXPIRE key seconds- Set a key's time to live in secondsTTL key- Get the remaining time to live of a keyKEYS pattern- Find all keys matching the pattern (e.g., KEYS *)FLUSHALL- Remove all keys from the databasePING- Test server connectionHELP- Display available commands
> SET name John
OK
> GET name
John
> SET session123 active EX 30
OK
> TTL session123
30
> EXISTS name
1
> KEYS *
name
session123
> DEL name
1
> GET name
(nil)
> FLUSHALL
OK
To run the integration tests:
cargo testThe tests verify basic functionality such as:
- Setting and getting values
- Key expiration
- Pattern matching with KEYS
- Database clearing with FLUSHALL
- Uses Tokio for async I/O
- Thread-safe in-memory storage with Mutex
- Simple text-based protocol (not RESP)
- Automatic key expiration with background cleanup task
- The server is designed for learning purposes and might not handle high loads
- For production use, consider using the actual Redis server