respkv

A Redis-compatible key-value server in Go.

How it works

respkv speaks the Redis protocol (RESP2), so redis-cli, redis-benchmark and Redis client libraries work with it. It keeps data in memory with key expiration, persists changes to an append-only file, accepts TLS connections and exposes Prometheus metrics and pprof.

  • One goroutine per connection, pipelining with a single flush per batch of buffered commands
  • Append-only file with always / everysec / no fsync policies and crash recovery
  • Graceful shutdown on SIGTERM, idle and write timeouts, structured slog logging
  • TopicsGoTCPRESPConcurrencyPersistenceTLS

Benchmark

ops/s SET GET SET ×16 GET ×16
respkv 241k 257k 1.42M 2.69M
respkv + AOF 236k 256k 564k 2.54M
Redis 7 288k 307k 1.89M 2.02M
Redis 7 + AOF 298k 307k 1.09M 1.99M

redis-benchmark, 50 clients, 100k random keys, 64-byte values; pipelined = 16 commands. Requests per second, same machine over loopback: AMD Ryzen AI 7 PRO 350, 8 cores, 16 threads.

What I learned

It had been a while since I wrote a network server, so I started from a bare TCP echo server and added one layer at a time: commands, the store, expiration, RESP, persistence, TLS.

  • Servers and clientsA connection is a stream, not a series of messages: a command can arrive in pieces, or several can arrive at once. respkv runs only complete commands and sends replies once the input is drained, so a pipelined batch costs one write instead of one per command.
  • Goroutines in practiceMy first real use of them: one per connection, plus background ones that expire keys and sync the file. Stopping them cleanly took more thought than starting them. A context closes the listener and interrupts blocked reads, and the server waits for every connection to finish.
  • Shared stateEvery connection works on one map behind a read-write mutex. A read never returns an expired key, even before the background sweep removes it.
  • Writes that survive a crashThe append-only file showed what durability costs: even with one fsync a second, pipelined SETs drop from 1.42M to 564k per second. A write reaches the file before the client gets its reply, a cut-off last record is dropped on startup, and expirations are stored as absolute times, so a restart does not extend them.