How aloima/tellydb works
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: databases.c Database manager · server.c Server lifecycle · resp.c RESP codec · commands.c Command dispatch
Components 19
Server I/O
Concurrent connection and socket handling.
- Server lifecycle · Owns listeners, clients, and response I/O.
src/server/server.c - Client sessions · Tracks client authentication and pipeline state.
src/server/client.c - Socket I/O
src/server/io.c - Command assembly · Assembles incoming client bytes into commands.
src/server/read_command.c - Authentication · Enforces passwords and command permissions.
src/server/auth.c
RESP & Dispatch
Wire parsing, encoding, and command routing.
- RESP codec · RESP2/RESP3 parsing and response encoding.
src/resp/resp.c - Command dispatch · Routes parsed commands to handlers.
src/server/commands.c - Command lookup
src/utils/command_hash.gperf
Serialized Execution
One transaction thread serializes mutations.
- Transaction coordinator · Queues work for sequential command execution.
src/transactions/transactions.c - Transaction thread · Executes commands atomically despite concurrent I/O.
src/transactions/thread.c - Command handlers · KV, list, hash-table, database, and generic commands.
In-Memory Database
Selected databases and typed values.
- Database manager · Manages selected databases, keys, and deletion.
src/database/databases.c - KV values
src/database/kv.c - Lists & hash tables · Custom in-memory collection-backed value domains.
src/utils/hashtable.c
Persistence
Durable database-file generation and saving.
- SAVE / BGSAVE · Synchronous or background save entry points.
src/commands/database/save.c - Database file generation · Separates durable file creation from memory state.
src/database/file/generate.c - Database file
src/database/file/file.c
Other components
- Server entry
src/telly.c - Runtime configuration
src/utils/config.c
Connections 21
- 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