Skip to content

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.

  • 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.

  1. The listener accepts a TCP connection.
  2. Zedis allocates a client slot from a fixed pool.
  3. The client coroutine reads from the socket and parses a RESP command (zero-copy from the read buffer when the whole command is buffered).
  4. The memory gate evicts to the KV budget, or rejects denyoom commands with -OOM.
  5. The command executes inline against the shared Store; writes are queued for the AOF.
  6. The response is serialized into a StackWriter and written to the socket.
  7. 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.

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:

  • SAVE uses 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).

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

  1. If no key is ready, replies already batched for the client are flushed, so a pipeline is not held back by the parked command.
  2. The client registers a Waiter for its keys and parks on a zio.Event with the command’s timeout.
  3. After each command, Server.notify_blocked wakes waiters on keys that the command may have filled (LPUSH, RPUSH, LINSERT, LMOVE and friends, including commands run by EXEC and scripts).
  4. 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.

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 by EXEC. INFO commandstats, INFO stats, and SLOWLOG share the same counters.
  • The store tracks hits, misses, expired keys and evicted keys; the KV allocator tracks its memory high-water mark.
  • METRICS renders the same counters as Prometheus text; INFO remains the source for redis_exporter-style scraping.
  • slowlog-log-slower-than, slowlog-max-len, latency-monitor-threshold, and command-timeout-ms are runtime-mutable with CONFIG SET.

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.

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.

  • 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.

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