Summary

Introduction to a tutorial series on building a Redis-like in-memory database from scratch in Go. The series covers RESP protocol parsing, goroutine-based connection handling, AOF persistence, and string/hash data structures. The goal is to illuminate how databases work internally — indexing, storage formats, recovery — which is rarely covered in typical tutorials.

介紹一系列從零開始用 Go 建構類 Redis 記憶體資料庫的教學,涵蓋 RESP 協定解析、goroutine 連線處理、AOF 持久化與資料結構實作,旨在揭示資料庫內部運作原理。

Key Points

  • What gets built: Redis clone supporting strings and hashes, RESP parser, multi-connection goroutines, AOF (Append Only File) persistence
  • Final codebase: just 4 files — aof.go, handler.go, main.go, resp.go
  • Why Go: simple code, existing Go databases (boltdb, diskv) as reference, goroutines for concurrency
  • Feynman framing: “What I cannot create, I do not understand” — building to understand, not just use
  • Prerequisites: basic Go reading ability and Redis command familiarity (non-Go readers can still follow the concepts)

Insights

The 4-file constraint is a teaching philosophy choice: keeping the implementation minimal forces the author to expose only the essential mechanisms. Production Redis is ~100K lines; distilling the core to 4 files shows which abstractions are truly load-bearing (RESP parsing, command dispatch, AOF persistence) versus which are performance/operational concerns.

AOF (Append Only File) is the simpler of Redis’s two persistence mechanisms (the other being RDB snapshots). Starting with AOF is the right pedagogical choice: it’s a log, not a snapshot, so recovery is conceptually straightforward — replay the log.

The RESP protocol (Redis Serialization Protocol) is simple enough to hand-implement but non-trivial: it uses length-prefixed encoding with type indicators (+, -, :, $, *). Understanding it demystifies Redis’s wire protocol, which transfers directly to understanding other Redis-compatible tools.

Connections

Raw Excerpt

After researching and reading various books, such as Database Internal, I decided to start building my own database and try to apply the concepts following the principle of “What I cannot create, I do not understand” - Richard Feynman.