Redis built from scratch in Go.
basic.ops.webm
stream.webm
tx.webm
- GET
- SET (with TTL)
- PING
- ECHO
- KEYS (only * pattern)
- TYPE
- XADD - Add entries to a stream
- XRANGE - Get range of entries (inclusive of start/end IDs)
- XREAD - Read entries newer than given ID, blocking available.
- MULTI - Start a transaction
- EXEC - Execute a transaction
- DISCARD - Discard a transaction
- WATCH - Watch keys for changes (optimistic locking) (CAS)
- UNWATCH - Stop watching keys
- Full RESP V2 (Redis Serialization Protocol) support
- Handles RESP data types:
- Simple Strings
- Errors
- Integers
- Bulk Strings
- Arrays
- Supports multiple concurrent clients using go-routines
- Thread-safe operations with mutex locks
- RDB file support (read-only, no RDB file saving/creation)
- Automatic loading of RDB files on startup
To clone and run locally, follow these steps:
- Clone the repository:
git clone https://github.com/yourusername/Redis-From-Scratch.git cd Redis-From-Scratch
- Initialize Go Modules:
go mod init github.com/manish-singh-bisht/Redis-From-Scratch go mod tidy
- Build and run
go build -o rds ./rds
- Use any Redis client to connect to the server on the port 9379.
- Using redis-cli, connect using
redis-cli -p 9379
- Run the supported commands mentioned above.
# Start redis-cli
redis-cli
# Basic key-value operations
1. SET mykey "Hello World"
2. GET mykey
3. SET mykey-with-ttl "I will expire" EX 10 # Expires in 10 seconds
4. KEYS * # List all keys
5. TYPE mykey # Get type of key
# Simple commands
6. PING # Returns PONG
7. ECHO "Hello" # Returns Hello
# Add entries to a stream
1. XADD mystream * name "John" age "25" # Auto-generated ID
# Read from stream
2. XRANGE mystream - + # Read all entries
# Read new entries
3. XREAD STREAMS mystream $ # Read entries newer than last seen
4. XREAD BLOCK 3000 STREAMS mystream $ # Block for 3 seconds waiting for new entries
# Basic transaction
1. MULTI # Start transaction
SET user:1 "John"
SET user:2 "Jane"
2. EXEC # Execute transaction
# Transaction with optimistic locking
3. WATCH user:1 # Watch for changes
MULTI
SET user:1 "John Doe"
EXEC # Will fail if user:1 was modified by another client
We'd love your help! Here's a simple guide to contributing:
- Fork & Clone
- Create a Branch
- Make Your Changes
- Open a Pull Request