Tip
Build this project yourself, step by step, on Codealong.
This repository is the finished code. In the Advanced Go: Building your own Redis course you build it lesson by lesson, learning goroutines, channels, mutexes and pointers along the way. Pause the video, change the code, run it, and get instant feedback on each challenge.
A small in-memory, Redis-flavoured key-value store written in Go. It was built as the final project of the Advanced Go course on Codealong and is used to practice interfaces, concurrency, the decorator (middleware) pattern, and testing.
.
├── main.go # Demo: replays 1000 concurrent INCRs against the store
├── command.go # Command type, dispatch, and parallel RestoreOnBoot
├── service.go # SetKeyWithEncryption helper (base64-encodes values)
├── middleware.go # LoggingMiddleware: logs every Storer call
├── metrics_middleware.go # MetricsMiddleware: call counts and latency report
└── store/
├── store.go # Storer interface and shared errors
├── kv/ # Thread-safe map store (sync.RWMutex) with optional max size
└── ttl/ # Store whose keys expire after a TTL (lazy eviction)
Everything is built around one interface in store/store.go:
type Storer interface {
Get(key string) (string, error)
Set(key, value string) error
Incr(key string) (int, error)
Keys() []string
Delete(key string)
Len() int
}Shared errors: ErrEmptyKey, ErrStoreFull, ErrNotInteger.
store/kv: the main engine. Amap[string]stringguarded by async.RWMutex, so many readers run in parallel while writers take an exclusive lock.Incris a locked read-modify-write, so concurrent increments are never lost.NewStore(maxSize)caps the number of keys (0means unlimited) and returnsErrStoreFullwhen a new key would exceed it.store/ttl: a second engine where every value carries an expiry time. Expired keys are evicted lazily onGet, with no background goroutine. It is not safe for concurrent use and does not implementIncr, so it doesn't satisfyStoreryet.- Middlewares (
package main):LoggingMiddlewareandMetricsMiddlewarewrap anyStorerand delegate to it. Because they implementStorerthemselves, they can be stacked. The demo inmain.godoesn't use them, so wrap the store yourself to try them (for exampleNewLoggingMiddleware(kv.NewStore(0))).
RestoreOnBoot replays a slice of Commands (SET, INCR) against a Storer, one goroutine per command, and reports errors collected over a channel. main.go uses it to fire 1000 INCRs at one key and checks that none are lost, plus two deliberately bad commands (empty key, unknown op) to show error handling.
Requirements: Go 1.26 or newer.
git clone https://github.com/codealong-dev/building-redis-in-go.git
cd building-redis-in-go
go run . # run the demo
go test ./... # run the tests
go test -race ./... # run the tests with the race detectorExpected output of go run . (the error lines may appear in either order):
Error: key is mandatory
Error: unknown op "WAT"
expected: 1000
got: 1000
This is a learning project, not a production database.
MetricsMiddlewarecounters are not synchronized, so it isn't safe to use under concurrent load.getMissesand the latency totals are never updated, so theReportoutput for those is always zero.ttl.TTLStorehas no locking and noIncr. ItsSetsleeps for one second to simulate slow writes.kv.Store.Clonecopies the map without taking the lock, so don't call it while other goroutines are writing.- Data lives in memory only. There is no persistence.