# ananth-swamy/redis-from-scratch architecture

> Architecture diagram of the GitHub repository ananth-swamy/redis-from-scratch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ananth-swamy/redis-from-scratch
- Repository: https://github.com/ananth-swamy/redis-from-scratch

## Overview

##### 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)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry point
    Entry["Application Entry Point<br/>(main.go)"]:::app
    click Entry "https://github.com/ananth-swamy/redis-from-scratch/blob/master/main.go"

    %% Main flow
    Client["Client (redis-cli)"]:::client
    TCP["TCP Listener<br/>(port 6379)"]:::network
    click TCP "https://github.com/ananth-swamy/redis-from-scratch/blob/master/main.go"
    Parser["RESP Parser"]:::network
    click Parser "https://github.com/ananth-swamy/redis-from-scratch/blob/master/resp.go"
    Handler["Command Handler"]:::app
    click Handler "https://github.com/ananth-swamy/redis-from-scratch/blob/master/handler.go"
    Store["In-Memory Store"]:::app
    click Store "https://github.com/ananth-swamy/redis-from-scratch/blob/master/store.go"
    Serializer["RESP Serializer"]:::network

    Entry --> TCP
    Client --> TCP
    TCP --> Parser
    Parser --> Handler
    Handler --> Store
    Store --> Handler
    Handler --> Serializer
    Serializer --> TCP
    TCP --> Client

    %% Persistence background
    Snapshot["Snapshot Goroutine"]:::persistence
    Dump["dump.rdb"]:::persistence
    Persist["Persistence Manager"]:::persistence
    click Persist "https://github.com/ananth-swamy/redis-from-scratch/blob/master/rdb.go"

    Snapshot -->|"every 1s"| Dump
    Dump --> Persist
    Persist --> Store

    %% Styles
    classDef client fill:#e0e0e0,stroke:#333,stroke-width:1px;
    classDef network fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef app fill:#90EE90,stroke:#333,stroke-width:1px;
    classDef persistence fill:#FFA500,stroke:#333,stroke-width:1px;
```
