# emmmik/klyn architecture

> Architecture diagram of the GitHub repository emmmik/klyn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/emmmik/klyn
- Repository: https://github.com/emmmik/klyn

## Overview

klyn is a Redis-compatible in-memory key-value server. A client sends RESP over TCP; the server parses a frame, dispatches its command to the database, encodes the response, and writes it back. The database provides key operations, counters, and lazy TTL handling, while AOF logging and startup replay provide persistence. The README documents concurrency and additional command capabilities; the graph groups related runtime behavior rather than listing every command. Edges follow shown calls or explicit README flows; AOF replay details are documented but only partly excerpted.

Main entry points: [`src/db.rs`](https://github.com/emmmik/klyn/blob/main/src/db.rs) (Command operations), [`src/main.rs`](https://github.com/emmmik/klyn/blob/main/src/main.rs) (Connection handler), [`src/parser.rs`](https://github.com/emmmik/klyn/blob/main/src/parser.rs) (RESP parser), [`src/encoder.rs`](https://github.com/emmmik/klyn/blob/main/src/encoder.rs) (RESP encoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_network["TCP server"]
  node_listener["TCP listener<br/>[main.rs]"]
  node_connection["Connection handler<br/>[main.rs]"]
  node_dispatcher["Command router<br/>[main.rs]"]
end

subgraph group_protocol["RESP protocol"]
  node_parser["RESP parser<br/>[parser.rs]"]
  node_frame["RESP frame<br/>[frame.rs]"]
  node_encoder["RESP encoder<br/>[encoder.rs]"]
end

subgraph group_storage["Database and persistence"]
  node_database["Command operations<br/>[db.rs]"]
  node_memory[("In-memory state<br/>[db.rs]")]
  node_aof[("AOF log<br/>[klyn.aof]")]
  node_replay["Startup replay<br/>[main.rs]"]
end

node_client(("Redis client"))

node_client -->|"sends RESP"| node_listener
node_listener -->|"spawns handler"| node_connection
node_connection -->|"parses request"| node_parser
node_parser -->|"produces frames"| node_frame
node_connection -->|"routes command"| node_dispatcher
node_dispatcher -->|"invokes operations"| node_database
node_database -->|"reads and mutates"| node_memory
node_database -->|"appends writes"| node_aof
node_database -->|"encodes log entries"| node_encoder
node_database -->|"returns response frames"| node_frame
node_dispatcher -->|"encodes response"| node_encoder
node_encoder -->|"serializes frames"| node_frame
node_connection -->|"writes response"| node_client
node_aof -->|"restores history"| node_replay
node_replay -->|"parses log frames"| node_parser
node_replay -->|"rebuilds state"| node_memory

click node_listener "https://github.com/emmmik/klyn/blob/main/src/main.rs"
click node_connection "https://github.com/emmmik/klyn/blob/main/src/main.rs"
click node_parser "https://github.com/emmmik/klyn/blob/main/src/parser.rs"
click node_frame "https://github.com/emmmik/klyn/blob/main/src/frame.rs"
click node_encoder "https://github.com/emmmik/klyn/blob/main/src/encoder.rs"
click node_dispatcher "https://github.com/emmmik/klyn/blob/main/src/main.rs"
click node_database "https://github.com/emmmik/klyn/blob/main/src/db.rs"
click node_memory "https://github.com/emmmik/klyn/blob/main/src/db.rs"
click node_aof "https://github.com/emmmik/klyn/blob/main/klyn.aof"
click node_replay "https://github.com/emmmik/klyn/blob/main/src/main.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_listener,node_connection,node_dispatcher toneBlue
class node_parser,node_frame,node_encoder,node_client toneAmber
class node_database,node_memory,node_aof,node_replay toneMint
```

## Components

### TCP server

- [TCP listener](https://github.com/emmmik/klyn/blob/main/src/main.rs): `src/main.rs`
- [Connection handler](https://github.com/emmmik/klyn/blob/main/src/main.rs): `src/main.rs`
- [Command router](https://github.com/emmmik/klyn/blob/main/src/main.rs): `src/main.rs`

### RESP protocol

- [RESP parser](https://github.com/emmmik/klyn/blob/main/src/parser.rs): `src/parser.rs`
- [RESP frame](https://github.com/emmmik/klyn/blob/main/src/frame.rs): `src/frame.rs`
- [RESP encoder](https://github.com/emmmik/klyn/blob/main/src/encoder.rs): `src/encoder.rs`

### Database and persistence

- [Command operations](https://github.com/emmmik/klyn/blob/main/src/db.rs): `src/db.rs`
- [In-memory state](https://github.com/emmmik/klyn/blob/main/src/db.rs): `src/db.rs`
- [AOF log](https://github.com/emmmik/klyn/blob/main/klyn.aof): `klyn.aof`
- [Startup replay](https://github.com/emmmik/klyn/blob/main/src/main.rs): `src/main.rs`

### Other components

- **Redis client**

## Connections

- Redis client → TCP listener: sends RESP (evidence: `README.md`)
- TCP listener → Connection handler: spawns handler (evidence: `src/main.rs`)
- Connection handler → RESP parser: parses request (evidence: `src/main.rs`)
- RESP parser → RESP frame: produces frames (evidence: `src/parser.rs`)
- Connection handler → Command router: routes command (evidence: `src/main.rs`)
- Command router → Command operations: invokes operations (evidence: `src/main.rs`)
- Command operations → In-memory state: reads and mutates (evidence: `src/db.rs`)
- Command operations → AOF log: appends writes (evidence: `src/db.rs`)
- Command operations → RESP encoder: encodes log entries (evidence: `src/db.rs`)
- Command operations → RESP frame: returns response frames (evidence: `src/db.rs`)
- Command router → RESP encoder: encodes response (evidence: `src/main.rs`)
- RESP encoder → RESP frame: serializes frames (evidence: `src/encoder.rs`)
- Connection handler → Redis client: writes response (evidence: `README.md`)
- AOF log → Startup replay: restores history (evidence: `README.md`)
- Startup replay → RESP parser: parses log frames (evidence: `src/main.rs`)
- Startup replay → In-memory state: rebuilds state (evidence: `src/main.rs`)
