# aloima/tellydb architecture

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

- Interactive diagram: https://gitdiagram.com/aloima/tellydb
- Repository: https://github.com/aloima/tellydb

## Overview

#### Purpose and Entry Point
TellyDB is a C in-memory, Redis-client-compatible key-value database using RESP2/RESP3. The server executable starts at `src/telly.c`, with public/global declarations in `headers/telly.h`. Build packaging supports a native Meson binary and a containerized server via `meson.build` and `Dockerfile`.

#### Server and I/O Boundary
The server owns listener/client lifecycle, authentication state, and network reads/writes. I/O is multi-threaded, separating socket work from serialized database mutation:
- `src/server/server.c`
- `src/server/client.c`
- `src/server/io.c`
- `src/server/events.c`

Incoming client bytes are read and assembled into commands by `src/server/read_command.c`; responses are returned through the server I/O layer. Clients may pipeline commands.

#### RESP Protocol and Command Dispatch
RESP2/RESP3 parsing and response encoding form the wire-protocol boundary:
- `src/resp/resp.c`
- `src/resp/data.c`
- `src/resp/inline.c`

Parsed commands enter server dispatch in `src/server/commands.c`. Command definitions are organized by database, generic, KV, list, and hash-table families under `src/commands/`; a generated perfect-hash input (`src/utils/command_hash.gperf`) likely supports efficient command lookup.

#### Serialized Transactions and Command Execution
A dedicated transaction thread executes commands sequentially, establishing atomic command execution despite concurrent I/O. Transaction coordination, thread management, and execution-scoped variables reside in:
- `src/transactions/transactions.c`
- `src/transactions/thread.c`
- `src/transactions/variables.c`

Redis-style transaction commands (`MULTI`, `EXEC`, `DISCARD`) are implemented alongside general commands in `src/commands/generic/`.

#### Database Model and In-Memory Storage
The database layer manages multiple selected databases, keys, deletion, and typed values:
- `src/database/databases.c`
- `src/database/kv.c`
- `src/database/get.c`
- `src/database/set.c`

Command handlers translate protocol arguments into database operations. Supported value domains include scalar KV operations, lists, and hash tables, with handlers respectively rooted at `src/commands/kv/`, `src/commands/list/`, and `src/commands/hashtable/`. Core in-memory containers are custom implementations under `src/utils/hashtable.c`, `src/utils/linkedlist.c`, and `src/utils/vector.c`.

#### Persistence and Background Saving
Database state is persisted to a database file. File generation, sizing, and file-level operations are separated from the in-memory database:
- `src/database/file/file.c`
- `src/database/file/generate.c`
- `src/database/file/size.c`

`SAVE` and `BGSAVE` command paths in `src/commands/database/save.c` and `src/commands/database/bgsave.c` invoke synchronous or external-thread persistence. The stated atomic save behavior is a key boundary between the serialized in-memory state and durable output.

#### Security, Configuration, and Runtime Dependencies
Authentication and per-command permissions are enforced before protected command execution:
- `src/server/auth.c`
- `src/server/password.c`
- `src/commands/generic/auth.c`

Runtime configuration is handled by `src/utils/config.c` with declarations in `headers/config.h`. Material native dependencies are OpenSSL, jemalloc, and GMP; no external database or cloud service is required. Deployment exposes the database server port (documented Docker usage maps `6379`).

Main entry points: [`src/database/databases.c`](https://github.com/aloima/tellydb/blob/master/src/database/databases.c) (Database manager), [`src/server/server.c`](https://github.com/aloima/tellydb/blob/master/src/server/server.c) (Server lifecycle), [`src/resp/resp.c`](https://github.com/aloima/tellydb/blob/master/src/resp/resp.c) (RESP codec), [`src/server/commands.c`](https://github.com/aloima/tellydb/blob/master/src/server/commands.c) (Command dispatch)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["Server I/O"]
  node_server["Server lifecycle<br/>server runtime<br/>[server.c]"]
  node_client["Client sessions<br/>connection state<br/>[client.c]"]
  node_io["Socket I/O<br/>I/O workers<br/>[io.c]"]
  node_read["Command assembly<br/>input reader<br/>[read_command.c]"]
  node_auth["Authentication<br/>access control<br/>[auth.c]"]
end

subgraph group_protocol["RESP &amp; Dispatch"]
  node_resp{{"RESP codec<br/>protocol codec<br/>[resp.c]"}}
  node_dispatch["Command dispatch<br/>router<br/>[commands.c]"]
  node_command_lookup["Command lookup<br/>perfect hash<br/>[command_hash.gperf]"]
end

subgraph group_execution["Serialized Execution"]
  node_transactions["Transaction coordinator<br/>serialized executor<br/>[transactions.c]"]
  node_transaction_thread["Transaction thread<br/>execution thread<br/>[thread.c]"]
  node_command_handlers["Command handlers<br/>command families"]
end

subgraph group_storage["In-Memory Database"]
  node_database[("Database manager<br/>multi-database store<br/>[databases.c]")]
  node_kv["KV values<br/>scalar storage<br/>[kv.c]"]
  node_collections["Lists &amp; hash tables<br/>typed values<br/>[hashtable.c]"]
end

subgraph group_persistence["Persistence"]
  node_save_commands["SAVE / BGSAVE<br/>persistence commands<br/>[save.c]"]
  node_file_generation["Database file generation<br/>snapshot writer<br/>[generate.c]"]
  node_database_file["Database file<br/>durable snapshot<br/>[file.c]"]
end

node_entry["Server entry<br/>executable<br/>[telly.c]"]
node_config["Runtime configuration<br/>[config.c]"]

node_entry -->|"starts"| node_server
node_config -.->|"configures"| node_server
node_server -->|"manages"| node_client
node_server -->|"runs"| node_io
node_io -->|"receives bytes"| node_read
node_read -->|"parses"| node_resp
node_resp -->|"parsed command"| node_dispatch
node_dispatch -->|"resolves command"| node_command_lookup
node_dispatch -->|"authorizes"| node_auth
node_auth -->|"approved commands"| node_transactions
node_transactions -->|"serializes on"| node_transaction_thread
node_transaction_thread -->|"executes"| node_command_handlers
node_command_handlers -->|"database operations"| node_database
node_database -->|"stores scalars"| node_kv
node_database -->|"stores collections"| node_collections
node_command_handlers -->|"SAVE paths"| node_save_commands
node_save_commands -->|"writes snapshot"| node_file_generation
node_database -->|"serializes state"| node_file_generation
node_file_generation -->|"atomic durable output"| node_database_file
node_transaction_thread -->|"command response"| node_resp
node_resp -->|"encoded response"| node_io

click node_entry "https://github.com/aloima/tellydb/blob/master/src/telly.c"
click node_server "https://github.com/aloima/tellydb/blob/master/src/server/server.c"
click node_client "https://github.com/aloima/tellydb/blob/master/src/server/client.c"
click node_io "https://github.com/aloima/tellydb/blob/master/src/server/io.c"
click node_read "https://github.com/aloima/tellydb/blob/master/src/server/read_command.c"
click node_auth "https://github.com/aloima/tellydb/blob/master/src/server/auth.c"
click node_resp "https://github.com/aloima/tellydb/blob/master/src/resp/resp.c"
click node_dispatch "https://github.com/aloima/tellydb/blob/master/src/server/commands.c"
click node_command_lookup "https://github.com/aloima/tellydb/blob/master/src/utils/command_hash.gperf"
click node_transactions "https://github.com/aloima/tellydb/blob/master/src/transactions/transactions.c"
click node_transaction_thread "https://github.com/aloima/tellydb/blob/master/src/transactions/thread.c"
click node_database "https://github.com/aloima/tellydb/blob/master/src/database/databases.c"
click node_kv "https://github.com/aloima/tellydb/blob/master/src/database/kv.c"
click node_collections "https://github.com/aloima/tellydb/blob/master/src/utils/hashtable.c"
click node_save_commands "https://github.com/aloima/tellydb/blob/master/src/commands/database/save.c"
click node_file_generation "https://github.com/aloima/tellydb/blob/master/src/database/file/generate.c"
click node_database_file "https://github.com/aloima/tellydb/blob/master/src/database/file/file.c"
click node_config "https://github.com/aloima/tellydb/blob/master/src/utils/config.c"

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_server,node_client,node_io,node_read,node_auth toneBlue
class node_resp,node_dispatch,node_command_lookup toneAmber
class node_transactions,node_transaction_thread,node_command_handlers toneMint
class node_database,node_kv,node_collections toneRose
class node_save_commands,node_file_generation,node_database_file toneIndigo
class node_entry,node_config toneNeutral
```

## Components

### Server I/O

Concurrent connection and socket handling.

- [Server lifecycle](https://github.com/aloima/tellydb/blob/master/src/server/server.c): server runtime · Owns listeners, clients, and response I/O. · `src/server/server.c`
- [Client sessions](https://github.com/aloima/tellydb/blob/master/src/server/client.c): connection state · Tracks client authentication and pipeline state. · `src/server/client.c`
- [Socket I/O](https://github.com/aloima/tellydb/blob/master/src/server/io.c): I/O workers · `src/server/io.c`
- [Command assembly](https://github.com/aloima/tellydb/blob/master/src/server/read_command.c): input reader · Assembles incoming client bytes into commands. · `src/server/read_command.c`
- [Authentication](https://github.com/aloima/tellydb/blob/master/src/server/auth.c): access control · Enforces passwords and command permissions. · `src/server/auth.c`

### RESP & Dispatch

Wire parsing, encoding, and command routing.

- [RESP codec](https://github.com/aloima/tellydb/blob/master/src/resp/resp.c): protocol codec · RESP2/RESP3 parsing and response encoding. · `src/resp/resp.c`
- [Command dispatch](https://github.com/aloima/tellydb/blob/master/src/server/commands.c): router · Routes parsed commands to handlers. · `src/server/commands.c`
- [Command lookup](https://github.com/aloima/tellydb/blob/master/src/utils/command_hash.gperf): perfect hash · `src/utils/command_hash.gperf`

### Serialized Execution

One transaction thread serializes mutations.

- [Transaction coordinator](https://github.com/aloima/tellydb/blob/master/src/transactions/transactions.c): serialized executor · Queues work for sequential command execution. · `src/transactions/transactions.c`
- [Transaction thread](https://github.com/aloima/tellydb/blob/master/src/transactions/thread.c): execution thread · Executes commands atomically despite concurrent I/O. · `src/transactions/thread.c`
- **Command handlers**: command families · KV, list, hash-table, database, and generic commands.

### In-Memory Database

Selected databases and typed values.

- [Database manager](https://github.com/aloima/tellydb/blob/master/src/database/databases.c): multi-database store · Manages selected databases, keys, and deletion. · `src/database/databases.c`
- [KV values](https://github.com/aloima/tellydb/blob/master/src/database/kv.c): scalar storage · `src/database/kv.c`
- [Lists & hash tables](https://github.com/aloima/tellydb/blob/master/src/utils/hashtable.c): typed values · Custom in-memory collection-backed value domains. · `src/utils/hashtable.c`

### Persistence

Durable database-file generation and saving.

- [SAVE / BGSAVE](https://github.com/aloima/tellydb/blob/master/src/commands/database/save.c): persistence commands · Synchronous or background save entry points. · `src/commands/database/save.c`
- [Database file generation](https://github.com/aloima/tellydb/blob/master/src/database/file/generate.c): snapshot writer · Separates durable file creation from memory state. · `src/database/file/generate.c`
- [Database file](https://github.com/aloima/tellydb/blob/master/src/database/file/file.c): durable snapshot · `src/database/file/file.c`

### Other components

- [Server entry](https://github.com/aloima/tellydb/blob/master/src/telly.c): executable · `src/telly.c`
- [Runtime configuration](https://github.com/aloima/tellydb/blob/master/src/utils/config.c): configuration · `src/utils/config.c`

## Connections

- Server entry → Server lifecycle: starts
- Runtime configuration → Server lifecycle: configures
- Server lifecycle → Client sessions: manages
- Server lifecycle → Socket I/O: runs
- Socket I/O → Command assembly: receives bytes
- Command assembly → RESP codec: parses
- RESP codec → Command dispatch: parsed command
- Command dispatch → Command lookup: resolves command
- Command dispatch → Authentication: authorizes
- Authentication → Transaction coordinator: approved commands
- Transaction coordinator → Transaction thread: serializes on
- Transaction thread → Command handlers: executes
- Command handlers → Database manager: database operations
- Database manager → KV values: stores scalars
- Database manager → Lists & hash tables: stores collections
- Command handlers → SAVE / BGSAVE: SAVE paths
- SAVE / BGSAVE → Database file generation: writes snapshot
- Database manager → Database file generation: serializes state
- Database file generation → Database file: atomic durable output
- Transaction thread → RESP codec: command response
- RESP codec → Socket I/O: encoded response
