A lightweight Redis implementation written in Go. MiniRedis is a minimal but functional in-memory data store that supports core Redis features including persistence, pub/sub messaging, and multiple data types.
- Strings: Basic key-value storage with atomic operations
- Hashes: Collections of field-value pairs
- Sorted Sets (ZSets): Ordered collections with scores
- Persistence: Append-Only File (AOF) for durability
- Pub/Sub: Publish-subscribe messaging system
- Key Expiration: Automatic key expiration with background janitor process
- Multiple Databases: 16 isolated databases (Redis-compatible)
- TTL Management: Set and check key time-to-live
GET key- Get value of a keySET key value- Set a key to hold a string valueDEL key [key ...]- Delete one or more keysINCR key- Increment integer valueDECR key- Decrement integer valueMGET key [key ...]- Get multiple keysMSET key value [key value ...]- Set multiple keys
HSET key field value- Set hash fieldHGET key field- Get hash field valueHDEL key field- Delete hash fieldHGETALL key- Get all fields and valuesHEXISTS key field- Check if field existsHLEN key- Get number of fields
ZADD key score member- Add member with scoreZRANGE key start stop- Get members by index rangeZSCORE key member- Get member scoreZREM key member- Remove memberZCARD key- Get number of membersZRANGEBYSCORE key min max- Get members by score range
PUBLISH channel message- Publish message to channelSUBSCRIBE channel [channel ...]- Subscribe to channelsUNSUBSCRIBE channel [channel ...]- Unsubscribe from channels
PING- Ping the serverECHO message- Echo a messageEXISTS key [key ...]- Check if keys existEXPIRE key seconds- Set key expirationPERSIST key- Remove expirationTTL key- Get remaining time-to-liveFLUSHALL- Delete all keys from all databases
- Go 1.22.2 or higher
- Clone the repository:
cd /home/kavyan2/Desktop/Projects/MiniRedis- Build the project:
go build -o mini-redisStart the server:
./mini-redisThe server will start listening on localhost:6379 and output:
Server(Mini-Redis) is listening on port 6379 ...
[AOF] AOF initialized
Use any Redis client (redis-cli, telnet, etc.):
# Using redis-cli
redis-cli -p 6379
# Using netcat
nc localhost 6379Example session:
> PING
+PONG
> SET mykey "Hello"
+OK
> GET mykey
$5
Hello
> INCR counter
:1
> EXPIRE mykey 60
:1
> TTL mykey
:59
- main.go: Entry point, TCP server setup, and connection handling
- store.go: In-memory data storage with multiple databases and entry types
- commands.go: Command parser and execution logic
- aof.go: Append-Only File persistence mechanism
- pubsub.go: Publish-subscribe implementation
- parser.go: RESP (REdis Serialization Protocol) parser
- conn_context.go: Connection context management
- HelperFunctions.go: Utility functions
- Thread-Safe Storage: Uses RWMutex for concurrent access to data
- AOF Persistence: Logs all write operations to disk for durability
- Background Processes: Janitor goroutine for key expiration, fsync for AOF
- Database Isolation: 16 independent databases like Redis
- Per-Connection Write Mutex: Ensures atomic responses for pub/sub
MiniRedis uses an Append-Only File (AOF) for persistence:
- File:
appendonly.aof- Stores all write operations - Replay: AOF is replayed on server startup to restore state
- Fsync: Background process periodically flushes AOF to disk
- Recovery: The server automatically recovers from crashes by replaying the AOF
- Keys can be set with expiration times using the
EXPIREcommand - A background janitor process periodically checks and removes expired keys
- Use
PERSISTto remove expiration from a key - Use
TTLto check remaining time-to-live
- Time Complexity: Most operations are O(1) or O(n) where n is the size of the collection
- Memory: All data is stored in memory; AOF logs are written to disk
- Concurrency: Goroutine-based handling of multiple clients
- Network: Standard TCP protocol with RESP wire format
- Single-threaded command execution (one goroutine per client)
- In-memory storage only (with AOF persistence)
- No clustering or replication
- Limited to 16 databases
- Partial Redis compatibility (subset of commands)
- Additional data types (Lists, Sets)
- More string commands (GETRANGE, SETRANGE, STRLEN)
- Transaction support (MULTI/EXEC)
- Lua scripting support
- Replication support
- Clustering
- RDB snapshots in addition to AOF
- CONFIG commands for runtime configuration
MiniRedis/
├── main.go # Server entry point
├── store.go # In-memory storage
├── commands.go # Command handlers
├── aof.go # Append-only file persistence
├── pubsub.go # Pub/sub implementation
├── parser.go # RESP protocol parser
├── conn_context.go # Connection management
├── HelperFunctions.go # Utility functions
├── go.mod # Go module file
└── README.md # This file
This project is an educational implementation of Redis. Please refer to your local regulations and the Redis license if you plan to use this in production.
Feel free to fork, modify, and improve this implementation!