toyredis is a small Redis-compatible database written from scratch in Go. It is intended for learning: the protocol, data structures, transactions, expiration, and persistence are implemented directly with the standard library and kept deliberately easy to follow.
It is a toy, not a production replacement for Redis. It uses one in-memory keyspace and a global execution lock, has no authentication or TLS, and implements a deliberately limited command set.
Go 1.26 or newer is expected.
go run ./cmd/toyredisThe server listens on :6379 and stores data in ./data by default. It speaks RESP2, so it can be used with redis-cli:
redis-cli SET greeting hello
redis-cli GET greeting
redis-cli HSET user:1 name Ada language Go
redis-cli HGETALL user:1Available flags:
-addr :6379
-dir data
-appendonly true
-snapshot-interval 5m
Set -snapshot-interval=0 to disable periodic snapshots or -appendonly=false for snapshot-only persistence. A clean shutdown always writes a snapshot.
| Area | Commands |
|---|---|
| Connection | PING, ECHO, QUIT, HELLO 2 |
| Strings | GET, SET (NX, XX, EX, PX), MGET, MSET, INCR, DECR, INCRBY, APPEND, STRLEN |
| Hashes | HSET, HGET, HMGET, HGETALL, HDEL, HEXISTS, HLEN, HINCRBY |
| Lists | LPUSH, RPUSH, LPOP, RPOP, LLEN, LRANGE, LINDEX |
| Sets | SADD, SREM, SISMEMBER, SMEMBERS, SCARD |
| Sorted sets | ZADD, ZREM, ZSCORE, ZCARD, ZRANGE (WITHSCORES), ZRANK, ZINCRBY |
| Keys | DEL, EXISTS, TYPE, DBSIZE, KEYS, SCAN, FLUSHDB |
| Expiration | EXPIRE, PEXPIRE, TTL, PTTL, PERSIST |
| Transactions | MULTI, EXEC, DISCARD, WATCH, UNWATCH |
For client compatibility, SELECT 0, CLIENT SETINFO, and a minimal COMMAND response are also accepted. Only database zero exists.
go test ./...
go test -race ./...
go vet ./...The tests cover RESP framing, all data structure families, expiration, watched transactions, TCP pipelining, snapshots, AOF replay, and crash-truncated AOF recovery.