How ananth-swamy/redis-from-scratch works
Project type and purpose
• It’s a single-process, in-memory key-value store (a hobby Redis clone) written in Go.
• Purpose: accept client connections over TCP using the Redis RESP protocol, execute basic commands (GET, SET, HGET, etc.), and periodically persist the in-memory data to disk.
File structure
Top-level files map directly to core components:
• main.go → application entry point, server setup, load snapshot, start listener
• handler.go → command dispatcher (maps parsed requests to store/rdb operations)
• resp.go → RESP protocol parser + serializer
• store.go → in-memory data structures (maps, hashes) and query API
• rdb.go → persistence subsystem (gob encoder/decoder, periodic snapshot)
• rdb.go’s dump.rdb file → on-disk snapshot format
README for additional insights
• Server listens on TCP port 6379.
• Uses Go’s standard library only (net, encoding/gob, time, etc.).
• Snapshot interval = 1s; restores state on startup.
• Supported commands cover string and hash operations.
a. Main components
• Client(s) (e.g., redis-cli)
• TCP Listener (net.Listener on port 6379)
• RESP Protocol Layer (resp.go)
• Command Handler (handler.go)
• In-Memory Store (store.go)
• Persistence Manager (rdb.go)
• Disk (dump.rdb file)
b. Relationships & interactions
1. Clients connect → TCP Listener
2. Listener reads raw bytes → RESP Parser → Request objects
3. Handler routes requests → Store or Persistence Manager
4. Store executes GET/SET/HASH ops → returns result → Handler
5. Handler serializes response → RESP Serializer → Listener → Client
6. Independently, Persistence Manager wakes every 1s → snapshots Store → writes dump.rdb
7. On startup, main.go invokes Persistence Manager to load dump.rdb → pre-populate Store
c. Architectural patterns / design principles
• Layered architecture:
• Network Layer (listener)
• Protocol Layer (RESP parser/serializer)
• Application Layer (command handler)
• Data Layer (in-memory store + persistence)
• Asynchronous background task (snapshotter via goroutine + time.Ticker)
• Separation of concerns: each .go file encapsulates one responsibility
d. Key technologies/frameworks
• Go standard library: net, bufio, encoding/gob, sync, time
• RESP protocol (custom implementation in resp.go)
• Gob-based binary snapshots (rdb.go)