Architecture Overview

Shard-per-core distributed runtime with unified append log storage.

Design Principles

  1. The log is the database. All writes go through the Unified Append Log (UAL). The Raft consensus log and the storage log are the same thing — no separate WAL.
  2. Thread-per-core, shared-nothing. Each CPU core runs an independent shard. Shards never share mutable state; they communicate via lock-free MPSC inboxes.
  3. Projections, not tables. KV, Queue, Stream, and Time-Series are deterministic projections derived from the UAL. On recovery, projections are rebuilt by replaying the log.
  4. Handler self-registration. The Dispatcher is a ~300-line opcode→handler lookup table. Each domain subsystem registers its own handlers — the Dispatcher imports nothing.

System Layout

┌───────────────────────────────────────────────────────────────┐
│                           NODE                                │
│                                                               │
│  Acceptor Thread     TCP accept → peek → route → hand-off     │
│                                                               │
│  Shard 0 Thread      Reactor → Dispatcher → Partitions        │
│  Shard 1 Thread      Reactor → Dispatcher → Partitions        │
│  Shard N Thread      Reactor → Dispatcher → Partitions        │
│                                                               │
│  Dashboard Thread    HTTP REST API → Inbox queries            │
│  Metrics Thread      Prometheus /metrics → per-shard agg      │
└───────────────────────────────────────────────────────────────┘

Acceptor

Dedicated thread running a TCP accept loop. On each new connection:

  1. Accept the socket
  2. Peek at the first bytes to detect the protocol (binary or HTTP)
  3. Read the namespace and key from the first request
  4. Hash them → slot → shard
  5. Hand off the file descriptor to the target shard via a pipe

Connections have shard affinity — once assigned, a connection stays on its shard to avoid cross-core cache thrashing.

Shard

Each shard is a dedicated OS thread pinned to a CPU core that owns:

ComponentPurpose
ReactorSingle kqueue/io_uring event loop handling all I/O, timers, and inbox draining
DispatcherOpcode→handler table for request routing
ConnectionPoolAll connections assigned to this shard
PartitionThe shard's Raft group: its UAL and projections
InboxMPSC ring buffer for receiving cross-shard messages
Slab allocatorFor cross-shard payload lifetimes

The shard's main loop:

loop {
    reactor.poll()                  // I/O events
    processEvents()                 // handle ready connections
    drainInbox()                    // cross-shard messages
    waiter_pool.expireTimeouts()    // blocking operation expiry
    taskScheduler.tick()            // background tasks
}

Dispatcher

Table-driven routing with a flat 256-slot handler array. Each subsystem (KV, Stream, Queue, TS, Actions, Workflows, Processing) calls dispatcher.register(.opcode, handler) during initialization — the Dispatcher never imports domain code.

Router

Hash-based slot assignment:

hash(namespace, key's {tag} or whole key) → slot   (one of 4096)
slot % shard_count → shard_id

The slot count is fixed for every node and cluster, so a key's slot never changes; there is no partition count to configure. A key's {tag}, the text between its first { and the next }, is what routes, so keys sharing one live together. A request that names no key runs on shard 0.

If a request lands on the wrong shard, the handler detects the mismatch and forwards via the Inbox.

Write Path

1. Client sends request to Acceptor
2. Acceptor routes to correct Shard
3. Dispatcher maps opcode to Handler
4. Handler calls Raft.propose(entry)
5. Raft replicates to quorum
6. On commit: UAL.append(entry)
7. ProjectionRouter.apply(entry) → updates KV/Queue/Stream/TS
8. Response sent to client

Write latency: ~1 RTT to quorum + local UAL write.

Read Path

1. Client sends request to Shard
2. Dispatcher maps opcode to Handler
3. Handler queries Projection directly (no Raft needed)
4. If cross-shard: Inbox message → remote Shard → response
5. Response sent to client

Read latency: 0 RTT for local reads on the leader.

Cross-Shard Communication

Shards communicate via 32-byte envelopes on lock-free MPSC ring buffers:

┌─────┬───────────┬──────────────┬─────────────┬─────────────┬──────────┬─────────┐
│ tag │ src_shard │ partition_id │ payload_ptr │ payload_len │ sequence │ padding │
│ u8  │ u8        │ u16          │ ptr         │ u32         │ u64      │ 8B      │
└─────┴───────────┴──────────────┴─────────────┴─────────────┴──────────┴─────────┘

Payloads are allocated from the slab allocator, not embedded in the envelope. This keeps the MPSC ring's cache line footprint minimal.

Cross-shard requests

A node with several shards (a single node runs one per CPU by default; a cluster member runs one) hands each client request to the shard that owns its key. When that shard has no room for more from the shard your connection is on, the connection is paused, not refused: the node stops reading it until the request can go, so the requests behind it wait too and run in order, and waiting connections take turns a request at a time. A request that waits 3 s is refused with overloaded … was not run; one that goes is answered within about 3 s (at most 4) of when it first waited, or told it has no answer yet.

A request that takes something — a dequeue, a group read, a worker's await — is the exception: its answer carries what it took, so the node waits for that answer, up to 320 s. An SDK client times out first. What it took is leased: if the answer never reaches the client, a group read's records and a dequeued message are delivered again when their lease runs out.

A client that closes its connection, or shuts down its sending side, while its request waits is treated as gone, and the request is not run. When it closes, its blocking reads on other shards are dropped (or, if that shard is busy, end at their block time). The Flo SDKs send one request at a time on a connection, so they never reach the 64 or 8 limits below.

AnswerMeaningWhat to do
overloaded: shard N is not keeping up; this request was not run — back off and retryfor 3 s, shard N (the one that owns the key) did not free room for more requests from the shard your connection is onback off and retry
overloaded: shard N has no room for more blocking reads from shard M; this read was not run — retryfor 3 s, shard N held its share of blocking reads from shard M (the one your connection is on): many consumers long-polling itretry; if it persists, see flo_shard_connections_paused{reason="slots"}
overloaded: 64 requests on this connection are still waiting on other shards (the oldest on shard N); this request was not run — back off and retryfor 3 s, none of this connection's 64 unanswered requests was answeredback off and retry
overloaded: this connection has 8 blocking reads waiting already; this request was not run — retry once one returnsfor 3 s, none of this connection's 8 blocking reads returnedretry once one returns, or read on another connection
unavailable: shard N did not answer in T s — retryshard N did not answer a read within about 3 s (at most 4) of when it first waited — a blocking read, its block time laterretry
unavailable: shard N did not answer in T s — the request may still applyshard N did not answer a write in that time, or a request that takes something in 320 s; shard N has the request and may still run itread back before resending a write that is not idempotent; a group read's records and a dequeued message are delivered again when their lease runs out

Each shard keeps up to 4096 blocking reads and parked workers waiting (about 1.5 MB, allocated at start). On a node of several shards, 1024 of them are for the shard's own clients and 3072 for other shards', so neither side can crowd out the other; a connection's blocking reads, here and on other shards together, are at most 8 at a time. Past its part, a blocking read is answered at once, empty, and the log says waiter pool full (at most every 10 s).

The per-shard series for this traffic:

MetricDescription
flo_shard_cross_shard_requests_in_flight{class="client_forward"}Client requests sent to another shard and not yet answered
flo_shard_oldest_cross_shard_wait_secondsHow long the oldest of those has waited; a shard that stops answering shows here first
flo_shard_cross_shard_overloaded_total{class}Requests refused before they ran: a client answered overloaded after waiting (client_forward), or a workflow step failed (engine)
flo_shard_connections_paused{reason}Connections not being read: their next request cannot go yet — in_flight (64 of theirs unanswered), blocking_reads (8 blocking reads out), slots (the owning shard holds its share of this shard's requests — for blocking reads, often many long polls — or the request is queued behind connections already waiting for it), inbox (it has not taken this shard's earlier ones) — or unsent_answers (the client is not reading its answers)
flo_shard_cross_shard_timeouts_total{class="client_forward"}Requests another shard did not answer by their deadline (3 s; a blocking read, its block time and 3 s; a request that takes something, 320 s); their clients were told. A slow shard shows here, not only one that never answers
flo_shard_replies_dropped_totalAnswers lost between shards: one came twice, or long after its request was given up on. Alert on anything but 0

Clustering

ComponentPurpose
Raft groupOne log per cluster; the leader replicates every write to a majority before acknowledging it, and membership is a config entry in that log
Peer linkOne authenticated TCP link per node pair, carrying the Raft RPCs and writes forwarded to the leader

Ports

PortPurpose
9000Binary wire protocol (client traffic)
9001Prometheus metrics
9002Dashboard REST API

Performance

Benchmarked on the new architecture (Apple M-series, single core, ReleaseFast):

BenchmarkThroughputLatency
UAL append12.9M ops/sec77 ns/op