Architecture

One Rust process, scalwsd, per instance. An optional controller adds and removes instances on one host. Every request is served by an instance; the controller only observes and manages them.

Control plane and data plane

By default (listen: inherited) the controller opens each listener once and hands it to its instances, which all accept from that one kernel queue; with listen: reuseport every instance binds its own SO_REUSEPORT socket and the kernel spreads new connections across the group by hash. A controller acting as a reverse proxy was rejected: an extra hop for every byte and a single choke point.

  • The controller never accepts, proxies or terminates client traffic and holds no TLS keys.
  • If the controller stops, instances keep serving. On restart it re-adopts them after checking PID, start time and executable.
  • Scaling adds capacity for new connections. In-flight requests are never migrated.
  • With the default listener_mode: shared, scalwsd’s request path is unchanged; regression gates compare it with a frozen baseline.
ADR-0033 →

control plane

scalws-controller observe → classify → decide → lifecycle state dir: generations, instance records (0600) · local Unix socket API, no remote mode

data plane

ClientsTCP :80 / :443
Linux kernelshared accept queue
scalwsd × N each terminates TLS, serves HTTP, cache, static, proxy, FastCGI
ApplicationsPHP-FPM, upstreams (external endpoints)
No controller proxy hop in the normal HTTP data path. The kernel hands each new connection to one instance; the connection, its keep-alive requests and its HTTP/2 streams stay there.

Request pipeline

No LLM, network control-plane call or other optional subsystem is on the request path. It depends only on in-process, immutable state: one atomic snapshot load, one hash lookup for the host, a short scan of the app’s routes, then the handler.

  1. Accept + connection capSemaphore; accept pauses when full, the kernel backlog absorbs the burst
  2. TLS handshakerustls, SNI resolver, ALPN h2 and http/1.1, handshake timeout
  3. HTTP/1.1 and HTTP/2Header size and count limits, read and idle timeouts, h2 stream and reset limits
  4. Host and tenant lookupExact and single-label wildcard hosts; duplicate or missing Host rejected
  5. Request limitsURI length, Content-Length precheck, streaming body cap, per-frame read timeout
  6. SecurityPer-source rate and connection limits, strict SNI (421), deny paths (404)
  7. CacheOpt-in: explicit freshness only, Vary variants, stale-while-revalidate, purge
  8. RoutingLongest segment-aligned prefix on the normalised path
  9. HandlerStatic files, reverse proxy, PHP over FastCGI, Node and Python pools, fixed responses
  10. Metrics and logsPrometheus exposition, JSON access logs without query strings

Configuration reloads build a complete snapshot first (validate, then prepare: files, certificates, upstreams) and swap it atomically. In-flight requests keep the snapshot they started with. A failed reload changes nothing. Architecture document →

Bottleneck classifier

Behind ScalWS, PHP-FPM, Node.js and uvicorn usually saturate their own CPUs long before the web tier does. Cloning scalwsd there costs CPU and memory and changes nothing, so the controller classifies before it acts.

PressureScore v1 (ADR-0034)

cpu      = instance CPU / CPUs assigned
lag      = clamp(loop lag p99 / lag_saturated)
inflight = clamp(in-flight / (loops × 256))
latency  = clamp(p99 / p99_target)  # optional

cpu_lag       = min(cpu, lag + 0.5)
PressureScore = max(cpu_lag, inflight, latency)

Defaults: lag_saturated 5 ms, 256 in-flight requests per loop, no latency target. High CPU alone is not pressure: a busy instance that keeps up has little loop lag. High CPU with rising lag is. Every component is exported and every evaluation is logged with its evidence, including the reason for doing nothing. No learning component.

Adaptive Runtime: how ScalWS decides when to add instances

Throughput
11.8k req/s
p99
rising
Class
PhpSaturated
Action
None

class=PhpSaturated action=None reason="php CPU 1.00/1, web tier cpu_lag 0.41: adding scalws instances would not help"

PHP-FPM is at its CPU allotment. The web tier is busy but keeping up: its event loops show no lag.

Measured: D1, php, 64 connections. Pressure bars and values inside log lines are illustrative.

Scaling buys elasticity, not free CPU efficiency

Web-tier paths scale close to linearly with instances, but every instance brings its own CPU. Against one instance given the same CPUs, N instances deliver 1.00× to 1.05×. The value is adding and removing capacity as load changes. Each instance costs about 30 to 34 MiB on the web-tier paths.

Burst, idle to 1024 connections Autoscaling in web-tier mode, min 1, max 3, one CPU per instance. Three bursts in a row, zero errors; instances counted at the end of each burst.

1 instance · 1 CPU

req/s 47.0k
p99 24 ms

3 instances · 3 CPUs

req/s 95.6k
p99 18 ms

3 instances · 3 CPUs

req/s 144.5k
p99 10 ms

Bars share one axis per metric, starting at zero. Lower p99 is better.

D1: N instances on SO_REUSEPORT vs one instance. 3 runs × 10 s, medians, one CPU per instance. Efficiency = rps(N) / (rps(1) × N). “vs 1 instance” compares with one instance given the same number of event loops and CPUs.
Scenario (connections)1 instance2 instancesefficiencyvs 1 instance, 2 loops3 instancesefficiencyvs 1 instance, 3 loops
respond (64)50.1k100.2k1.001.03×151.0k1.001.03×
respond (256)49.7k99.5k1.001.03×149.7k1.001.03×
static-small (64)38.0k75.3k0.991.05×112.4k0.991.04×
static-small (256)37.8k74.3k0.981.04×111.4k0.981.04×
static-1m (64)5.25k8.82k0.841.00×10.8k0.691.00×
cache-hit (64)40.8k80.8k0.991.05×121.7k1.001.05×
TLS + HTTP/2 (256)30.5k62.0k1.021.03×94.9k1.041.02×
proxy (256)21.6k42.6k0.991.04×63.6k0.981.03×
php (64)11.8k11.8k0.501.00×11.8k0.331.00×
D5: concurrency sweep with autoscaling on (web-tier mode, min 1, max 3), 20 s per point. Instances = active instances at the end of the point.
Pathconnectionsreq/sp99 mserrorsinstancesRSS MiB
respond849,6000.170132
respond3250,7000.660262
respond6499,5000.750395
respond128149,4000.980397
respond256149,2002.0003102
respond512147,4005.0103108
respond1024145,30010.1403121
static-small883,0000.120395
static-small32111,8000.350395
static-small64112,0000.740396
static-small128111,5001.380396
static-small256110,9002.6203105
static-small512110,6005.2903105
static-small1024108,90010.0503121

Host: bare metal, 2× Xeon E5-2650 v2 (Ivy Bridge EP, 8 cores per socket), 121 GB, Ubuntu 26.04, kernel 7.0, tcp_migrate_req=1, performance governor, turbo off. Topology separate from the release-mode comparisons: instance i on CPU i (one event loop each), upstream and PHP-FPM (4 workers) on CPU 3, oha on the other socket (CPUs 8-15, 24-31).

Lifecycle, generations and recovery

Readiness gates traffic: an instance is bound but not listening until the controller activates it. Removal drains, it does not kill.

Stopped

No process. The controller keeps a record per slot (web-0, web-1, ...) in its state directory: PID, CPU set, generation, state, timestamps. Files are 0600 and written atomically.

Starting

Process spawned with its instance id, an explicit configuration generation and its CPU set. It accepts no connections until it is told to activate, so it receives no traffic.

Warming

Configuration prepared, runtimes and caches initialised. The readiness probe answers with the generation number and state “warming”.

Ready

Readiness passed. Still not accepting connections. A start that does not reach Ready within start_timeout is a failure.

Active

The controller activates it (SIGUSR2): it starts accepting from the shared listening sockets (with listen: reuseport, its sockets join the group) and the kernel starts handing it new connections. Connections stay on the instance that accepted them.

Draining

It stops accepting first: with the default shared accept queue the other instances keep accepting from it; with listen: reuseport, queued connections migrate to them (net.ipv4.tcp_migrate_req=1). Keep-alive connections are told to close (Connection: close, HTTP/2 GOAWAY). In-flight requests finish until drain_timeout; a forced kill only after drain_timeout + kill_grace.

Failed is reachable from every state: process exit, start timeout, or drain timeout followed by a forced stop. Failed slots restart with exponential backoff (1 s to 60 s); more than max_restarts in restart_window leaves the slot failed instead of starting a fork storm. Liveness, readiness and saturation are separate: a saturated instance is healthy and is never restarted because of load.

Configuration generations

A generation is a validated, immutable copy of the configuration, written to a temporary name, fsynced and renamed. A reload rolls instances one at a time: start on the new generation, Ready, Active, then drain one old instance. Capacity never drops below the current count. Rollback is the same procedure towards the previous generation.

Anti-thrashing

Scale out after 3 to 5 s above the threshold, scale in after 60 s below it, a cooldown after every action (default 30 s), one action in flight, and hysteresis between scale_out_above (0.75) and scale_in_below (0.30). autoscaling: off freezes the instance count immediately.

Tested under load

Scale 1 → 2 → 3 → 1, rolling reload, reload while scaling, controller restart and a killed instance: zero failed requests. HTTP/2 (8 × 32 streams) across scale-in 3 → 1: no processed stream lost; the client’s queued, never-sent requests were canceled, which RFC 9113 §6.8 makes safe to retry.

ADR-0035 →

Known limitations

Adaptive Runtime final report →