skip to main
← Writing2025-02-07 · Low-level design

Why shared-nothing scales on one machine

Redis runs client I/O and commands on one event loop. DragonflyDB gives each core its own shard and queue so threads never contend on a global lock.

low level designhigh level designmulti-threading

Redis made the single-threaded event loop famous for good reason. One loop is easy to trace in a debugger and fast whenever the working set fits on one CPU core. Once a workload outgrows that single core, adding threads around a shared hash table mostly buys lock contention. The bottleneck is shared mutable state.

The single-threaded event loop

Redis runs client I/O and command execution on one main loop. Socket multiplexing lets that single thread watch thousands of connections without blocking on a slow client.

  • The kernel reports ready sockets through epoll, kqueue, or select.
  • The event loop drains a bounded batch of commands, then polls for I/O again.
  • A read or recv call on an empty socket returns immediately so the loop can move to the next client.

In-memory lookups take nanoseconds while network packets take microseconds or milliseconds. One core can execute a huge pile of queued commands while the network cards wait on the wire. This breaks down when CPU-heavy commands saturate that single core while the other 31 cores on the box sit idle.

Single-threaded event loop

Shard per core

A shared-nothing engine pins one worker thread to each core and gives that thread exclusive ownership of a key shard. When a request arrives for key k, the router hashes k and sends the command to the core that owns that hash slot. That thread mutates its own memory without taking a mutex.

Adding threads only helps if you also partition ownership. Threads that share a lock just queue up in kernel space instead of user space.

Network layer

A pool of I/O threads accepts connections and parses client frames. Each connection runs inside a user-space fiber, so waiting on a socket yields to another fiber on the same OS thread.

  • Non-blocking sockets keep slow clients from pinning an OS thread.
  • Switching fibers in user space avoids a kernel context switch.
  • Lock-free message queues hand parsed commands to the owning storage core.
  • Network threads can run at lower scheduling priority so storage cores keep CPU time under load.

There are real traps here. One accidental blocking syscall stalls every connection multiplexed onto that I/O thread. Under-provisioning I/O threads starves the storage shards. Cancellation and timeout handling also get harder to trace once requests hop across fiber queues. When you move off blocking threads, the user-space scheduler becomes part of the storage engine's correctness.

Storage layer

Each storage thread owns its keyspace shard and its inbound work queue. A network fiber enqueues a message and awaits the completion future. Single-key operations stay on one core from start to finish.

  • Local reads and writes need no mutex because only one thread touches the shard.
  • Keeping a shard on one core keeps its hash buckets hot in L1 and L2 cache.
  • Adding CPU cores scales throughput linearly for single-shard commands.
  • A single-producer, single-consumer queue per core pair handles message passing without atomic read-modify-write contention.

The hard cases show up immediately. A single hot key can still peg one core while its neighbors sit idle. If a developer accidentally touches a shard from the wrong thread, you get silent memory corruption instead of a lock wait. Multi-key transactions have to coordinate across shards, which brings back two-phase locking or message barriers. Shared-nothing puts an explicit price tag on every cross-shard call.

Shard-per-core design

Anti-caching

RAM is still the most expensive part of an in-memory store. Anti-caching spills cold values to NVMe while keeping hot keys in memory. The shard keeps keys and compact metadata resident in RAM so it knows whether a value lives in memory or on disk, and faults cold values back in asynchronously on a miss.

  • Evict cold values based on memory pressure and recent access bits.
  • Keep key directory entries in memory so lookups never do a blind disk probe.
  • Issue disk reads asynchronously via io_uring so the shard thread keeps serving hot keys.
  • Wake the waiting fiber once the disk read completes.

You trade a single predictable latency number for a bimodal distribution, fast for RAM hits and slower for disk faults. For most workloads, paying a disk read on cold keys beats paying for enough RAM to hold data nobody has touched since last Tuesday.

When to use which

If your workload fits on one core, stick with a single event loop. It is simpler to build and simpler to operate. When you need all the cores on a modern machine, partition the data by core, forbid cross-thread mutation, and make cross-shard coordination obvious in the code.