Server architecture
Zedis runs on zio with exactly one executor (RuntimeOptions.executors = .exact(1)).
All client coroutines, parsing, command execution and response building run on that
one OS thread.
Threads And Tasks
Section titled “Threads And Tasks”- The main thread initializes the zio runtime, loads config, builds the server, and
runs the TCP accept loop (
Server.listen()). - Each accepted connection runs as an
Io.Group.async(...)coroutine on the single executor. Coroutines own socket reads, RESP parsing, command execution, mailbox flushing, and socket writes. - Commands run inline on the client coroutine. There is no store thread, no command queue, and no store lock.
- The AOF writer is a dedicated OS thread (
aof.Writer) that owns file writes and fsyncs. Commands only append to an in-memory queue. - The clock updater runs as a background coroutine, or as a detached OS thread when
the I/O backend has no concurrency (
Clock.start).
Application-owned threads are therefore: the main/executor thread, the AOF writer thread, and the optional clock fallback thread. There is no thread per client.
Request Flow
Section titled “Request Flow”- The listener accepts a TCP connection.
- Zedis allocates a client slot from a fixed pool.
- The client coroutine reads from the socket and parses a RESP command (zero-copy from the read buffer when the whole command is buffered).
- The memory gate evicts to the KV budget, or rejects
denyoomcommands with-OOM. - The command executes inline against the shared
Store; writes are queued for the AOF. - The response is serialized into a
StackWriterand written to the socket. - Pub/sub messages queued in the client’s mailbox are flushed after the command; the per-client delivery coroutine also drains them while the client is idle.
Serialization
Section titled “Serialization”With one executor, only one coroutine runs at a time, so command execution is serialized without a store lock. This relies on commands never suspending:
SAVEuses blocking file I/O, so a snapshot cannot interleave with another command (it stalls the server for its duration, like Redis’SAVE).- AOF queueing uses a spinlock, so contention with the writer thread cannot park the coroutine in the middle of a command.
- With the disk tier enabled, materializing a spilled value reads TidesDB synchronously on the executor. It stalls the event loop for the read but keeps the no-suspension invariant; Phase 5 moves these reads to a worker pool.
Socket I/O and appendfsync always durability waits can suspend, but only outside
command execution. If a handler ever needs to suspend, it must not mutate the store
around that suspension (or the single-executor/no-lock invariant no longer holds).
Blocking commands
Section titled “Blocking commands”BLPOP, BRPOP and BLMOVE obey the no-suspension rule by splitting the work.
Their handlers never wait: they pop when a key has data and reply nil otherwise,
which is also what MULTI/EXEC, Lua and AOF replay get. The waiting happens in
Client.handle, between commands (app/blocking.zig):
- If no key is ready, replies already batched for the client are flushed, so a pipeline is not held back by the parked command.
- The client registers a
Waiterfor its keys and parks on azio.Eventwith the command’s timeout. - After each command,
Server.notify_blockedwakes waiters on keys that the command may have filled (LPUSH, RPUSH, LINSERT, LMOVE and friends, including commands run by EXEC and scripts). - A woken client re-checks readiness and then runs the unmodified command, so the pop is logged to the AOF verbatim and only when it succeeds. A timeout writes nil without running anything, so nothing is logged.
Waiters wake in registration order but are not handed the element: a client that wakes after another has taken it simply parks again. A parked client does not read its socket, so a peer that disconnects is noticed only on wake or timeout.
Observability
Section titled “Observability”Server owns a stats.ServerStats, a fixed-size SLOWLOG ring and a
LATENCY monitor. Durations come from the monotonic .awake clock, not the
cached wall clock used for TTLs and log timestamps.
- Every command is timed around
execute_locked, including commands run byEXEC.INFO commandstats,INFO stats, andSLOWLOGshare the same counters. - The store tracks hits, misses, expired keys and evicted keys; the KV allocator tracks its memory high-water mark.
METRICSrenders the same counters as Prometheus text;INFOremains the source forredis_exporter-style scraping.slowlog-log-slower-than,slowlog-max-len,latency-monitor-threshold, andcommand-timeout-msare runtime-mutable withCONFIG SET.
Shutdown
Section titled “Shutdown”SIGINT and SIGTERM run a coroutine per signal. On receipt, requestShutdown
sets an atomic flag, wakes idle clients with a socket shutdown, and the accept
coroutines are cancelled. listen waits for the client group, then main
returns through Server.deinit: the AOF writer drains its queue, fsyncs, and
closes the file before the listeners are released. SHUTDOWN [NOSAVE|SAVE]
uses the same path.
Bounded commands
Section titled “Bounded commands”command-timeout-ms applies only to read-only commands. Handlers check a
deadline every ~1024 iterations and return -TIMEOUT; mutating commands are
never aborted mid-flight. This bounds a stall but does not make execution
concurrent: with one executor, a long KEYS still blocks other clients for its
duration. Use SCAN instead of KEYS for large keyspaces.
Coordination Primitives
Section titled “Coordination Primitives”ClientMailbox: spinlock-protected queue for pub/sub delivery (not lock-free), drained by the client coroutine and by the per-client delivery coroutine while the client is idle.- Client slots are preallocated and tracked with generation counters to avoid stale handle reuse during disconnect and reconnect cycles.
- AOF queue: spinlock shared with the writer thread; the critical section is the in-memory queue swap/append only.
Why One Executor
Section titled “Why One Executor”Store access is globally serialized either way. With multiple executors, coroutines
and the store bounce between threads on every command, adding contention and cache
pressure for no throughput benefit. On the benchmark workload one executor ran at
~2.66M rps on ~0.75 cores, while .auto ran at ~1.2M rps using ~4.5 cores (one
executor was also ~35% faster without pipelining).