Skip to content
Geoff-RobinPublic

About

redis written in go with persistence :)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

toyredis

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.

Run it

Go 1.26 or newer is expected.

go run ./cmd/toyredis

The 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:1

Available 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.

Supported commands

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.

Test it

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.

About

redis written in go with persistence :)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages