A fully-featured Redis server implementation written in Rust, built as part of the CodeCrafters Redis Challenge. This implementation supports a comprehensive set of Redis commands, data structures, and advanced features including replication, persistence, transactions, and pub/sub.
- Connection:
PING,ECHO,INFO - String Operations:
GET,SET,INCR - Key Management:
TYPE,KEYS
- Lists:
LPUSH,RPUSH,LPOP,BLPOP,LRANGE,LLEN - Sorted Sets (ZSet):
ZADD,ZCARD,ZCOUNT,ZRANK,ZRANGE,ZREM,ZSCORE - Streams:
XADD,XDEL,XLEN,XRANGE,XREAD - Geospatial:
GEOADD,GEOPOS,GEODIST,GEOSEARCH
- Transactions:
MULTI,EXEC,DISCARD - Pub/Sub:
SUBSCRIBE,UNSUBSCRIBE,PUBLISH - Replication: Master-slave replication with
REPLCONF,PSYNC,WAIT - Persistence: RDB file format support with expiration tracking
- Configuration:
CONFIG GET - ACL (Access Control Lists):
ACL WHOAMI,ACL GETUSER,ACL SETUSER,AUTH
src/
├── main.rs # Entry point and server initialization
├── frame/ # RESP (REdis Serialization Protocol) implementation
│ ├── mod.rs
│ ├── decode.rs # RESP protocol parsing
│ ├── encode.rs # RESP protocol serialization
│ ├── frame.rs # Frame type definitions
│ └── debug.rs # Debug implementations
├── server/ # Command handlers
│ ├── server.rs # Core server logic and command dispatch
│ ├── string.rs # String commands
│ ├── list.rs # List commands
│ ├── zset.rs # Sorted set commands
│ ├── stream.rs # Stream commands
│ ├── geospatial.rs # Geospatial commands
│ ├── transaction.rs # Transaction support
│ ├── pubsub.rs # Pub/Sub implementation
│ ├── replication.rs # Replication logic
│ ├── persistence.rs # Configuration and persistence
│ ├── acl.rs # Access control
│ ├── misc.rs # Miscellaneous commands
│ └── errors.rs # Error handling
├── store/ # Data storage layer
│ ├── mod.rs
│ ├── value.rs # Value type implementations
│ ├── stream.rs # Stream entry handling
│ └── info.rs # Server info
├── rdb/ # RDB persistence
│ ├── mod.rs
│ └── decode.rs # RDB file parsing
├── parser.rs # Frame parser
└── slave.rs # Slave replication handler
Implements the RESP (REdis Serialization Protocol) supporting:
- Simple strings, bulk strings, errors
- Integers, doubles, booleans
- Arrays, maps, sets
- RDB file transfers
- In-memory key-value store with multiple data types
- TTL/expiration support with priority queue
- Geospatial indexing using geohash encoding
- Stream entries with time-based IDs
- Master-slave architecture
- Full resynchronization with RDB snapshots
- Incremental replication with command propagation
- Offset tracking and acknowledgments
- Rust 1.88 or higher
- Cargo
# Clone the repository
git clone <repository-url>
cd redis-rust
# Build the project
cargo build --release# Run on default port (6379)
./your_program.sh
# Run on custom port
./your_program.sh --port 6380./your_program.sh --dir /path/to/data --dbfilename dump.rdb./your_program.sh --port 6380 --replicaof localhost 6379# Run tests
cargo test
# Run with debug logging
DEBUG=true cargo run# Connect with redis-cli
redis-cli -p 6379
# String operations
SET mykey "Hello"
GET mykey
INCR counter
# List operations
LPUSH mylist "world"
LPUSH mylist "hello"
LRANGE mylist 0 -1
# Sorted sets
ZADD leaderboard 100 "player1"
ZADD leaderboard 200 "player2"
ZRANGE leaderboard 0 -1MULTI
SET key1 "value1"
SET key2 "value2"
EXEC# Terminal 1 (Subscriber)
SUBSCRIBE mychannel
# Terminal 2 (Publisher)
PUBLISH mychannel "Hello subscribers!"GEOADD locations 13.361389 38.115556 "Palermo"
GEOADD locations 15.087269 37.502669 "Catania"
GEODIST locations "Palermo" "Catania" km
GEOSEARCH locations FROMLONLAT 15 37 BYRADIUS 200 kmXADD mystream * sensor-id 1234 temperature 25.5
XRANGE mystream - +
XREAD BLOCK 1000 STREAMS mystream 0- Built on Tokio for high-performance async networking
- Non-blocking command execution
- Concurrent client handling
- Zero-copy buffer management with
bytescrate - Efficient data structure implementations
- Smart expiration management
- Full RESP2/RESP3 protocol support
- Proper error handling and reporting
- Type checking and validation
- Geohash encoding/decoding
- Haversine distance calculation
- Efficient radius queries
- Single-threaded event loop: Similar to Redis's architecture
- Async I/O: Non-blocking operations for high throughput
- In-memory storage: Fast read/write operations
- Efficient serialization: Minimal overhead in RESP encoding/decoding
This is an educational implementation with some intentional simplifications:
- Single-threaded execution model
- In-memory only (no AOF persistence)
- Simplified cluster support
- Limited to subset of Redis commands
This project was completed as part of the CodeCrafters challenge. While it's primarily for educational purposes, suggestions and improvements are welcome!
This project is part of the CodeCrafters curriculum and follows their guidelines.
- CodeCrafters for the excellent learning platform
- Redis for the original design and protocol specification
- The Rust community for amazing async libraries (Tokio, bytes, etc.)