Internals: decision records
ADR-0044: Fountain accept policy (opt-in)
Status: accepted, opt-in, 2026-10-09; connection cap added 2026-10-15
Context
All event loops of one scalwsd wait on a shared accept queue (ADR-0029, ADR-0033). The loop that wakes first takes a new connection, so under a burst one loop can take most of them and keep them for their lifetime: measured 0 % / 98 % / 29 % / 94 % CPU across four loops under 64 keep-alive connections. Balancing connection counts evenly (ADR-0038, step 3a) made the loops even but cost CPU per request (+5–12 %) and TLS p99 (2.65 → 10.26 ms): concentrated loops batch more work per wake-up. Neither “first come” nor “even” is best.
Decision
server.accept_policy: shared | fountain (default shared, unchanged behaviour).
With fountain the loops fill in order, like the tiers of a fountain: loop 0 takes new
connections; loop i takes one only while every loop before it is full, and otherwise
leaves it to them for up to 2 ms (a stuck loop never strands a connection).
- Full is the loop’s busy share, read every 20 ms from Tokio’s own busy-time counter (no cost on the request path), smoothed over about two samples, with hysteresis: full from 85 %, free again below 70 %.
- Bursts: each connection accepted in a 20 ms window beyond the previous window’s count adds an estimated 10 % until the next sample. A steady stream of new connections adds nothing (its load is in the measured share); 64 connections opened at once spread over the loops (about nine per loop before the next one starts).
- Connection cap (2026-10-15): a loop also counts as full while it holds
max(8, ceil(2 × live connections / accepting loops))live connections, twice its fair share. A loop at its cap leaves new connections to the loops after it for the same bounded 2 ms. Only connections accepted before the current 20 ms window count against the cap; those of the current window are the surge estimate’s (see “Connection cap”). - Established connections never move.
Measurements (development VM, 4 loops; bench/fountain.sh, bench/multi.sh)
Open loop, a new connection per request (the case the policy is for), 3 rounds:
| Load | shared CPU µs/req | fountain CPU µs/req | p99 |
|---|---|---|---|
| 10 000 req/s | 154–161 | 112–136 | equal (2.8–3.1 ms) |
| 20 000–40 000 req/s (connect-rate bound) | 118–126 | 97–124 | equal |
| 512 keep-alive connections, 40 000 req/s | 47–51 | 36–50 | 4.1–5.9 → 5.0–8.6 ms |
Mixed load (TLS + HTTP/2 small files with HTTP/1 1 MiB files at the same time): HTTP/2 +20 %, 1 MiB −15 %: capacity moves between the two, no net gain.
Closed loop, 64 keep-alive connections, 5 runs (spread 3–12 %): static −4 %, 1 MiB −2 %, respond −5 %, proxy −7 %, cache hits +3 %, TLS + HTTP/2 +6 %, PHP +6 % (CPU per request −7 %).
Consequences
fountainlowers server CPU per request by up to ~20 % at moderate load with many short-lived connections: CPU left to PHP-FPM, Node.js, Python or databases on the same cores (hosting servers). Throughput and latency stay the same there.- It is not the default: with few long-lived connections it is at best even with
shared, and it can shift capacity between workloads of different weight. - A first version that counted 2 % per new connection regardless of the steady rate left two of four loops without connections in the closed-loop benchmark (−6 … −19 %); the surge-only estimate fixed that.
EPOLLEXCLUSIVE(nginx’s way: the kernel wakes one waiter that is idle inepoll_wait) was examined and is not usable here. Tokio registers the listener in its own epoll without the flag and does not expose that registration; nesting a per-loop epoll that holds the listener withEPOLLEXCLUSIVEdoes not help: measured on Linux 6.8 (bench/probes/epollexclusive-nested.py), one connection wakes 1 of 4 threads waiting directly but 4 of 4 when the exclusive epoll is nested, because the kernel only stops waking at an epoll with a thread blocked on it. Patching mio is not enough either: it always addsEPOLLRDHUP, which the kernel rejects withEPOLLEXCLUSIVE, and Tokio’s edge-triggered readiness makes the one woken loop take a whole burst. A dedicated acceptor thread per loop would not see the loop’s load and would add a hand-off per connection.
Connection cap (2026-10-15)
Why
In the Kubernetes ingress benchmark (docs/benchmarks/2026-10-14-xeon-k8s-ingress.md,
round 2) fountain placed 256 keep-alive connections on three of 16 loops: they arrive
over several 20 ms windows, so the surge estimate does not see a burst, and they carry no
load yet when accepted. Once the load starts, loops 0 and 1 sit at about 90 % busy and
the connections stay there: p99 1.8x to 7x that of shared at a fixed rate, 0.92x to
0.96x of its requests/s.
Rule
A loop is full when it is full by its busy share (see “Decision”) or its live
connection count reaches cap = max(MIN_CAP, ceil(2 × total_live / accepting_loops)),
with MIN_CAP = 8. So at
least half of the loops get connections once there are more than 4 × loops of them, and
a light load (a browser opens up to six connections per host) still stays on loop 0.
- Live connections are counted per loop by a guard that the connection task holds: every
way a connection ends (closed, PROXY header rejected or late, TLS handshake failed,
source limit, shutdown) leaves the count exactly once.
shareddoes not count (one branch on the policy, no atomic on its path). - A loop at its cap also steps back itself (2 ms at most). All loops wake for a new connection; without this an idle loop at the cap still wins the race about as often as the next one.
- Only connections accepted before the current 20 ms window count against the cap.
The first version counted every live connection; under a high rate of short
connections a loop accepts a run of them before their tasks run, was at its cap for a
moment, stepped back, and the later loops stepped back for it in turn: 10 000 new
connections per second fell to 4 650 req/s, p50 55 ms (
bench/fountain.sh, bench host). Connections of the current window are the surge estimate’s, as before. The price: a loop’s count islive - recent(connections accepted in the current window are subtracted, whether they are still open or not), so a steady stream of short connections can mask the cap. A loop that holds 30 long-lived connections and accepts 30 short ones per window counts as holding almost none while the stream lasts. The busy rule still applies to such a loop. - Not with a socket per loop. With
listener_mode: reuseport(orSCALWS_REUSEPORT=1) every loop has its ownSO_REUSEPORTsocket and the kernel picks the loop; a connection queued on a loop’s socket can only be accepted by that loop. Deferring there (to an earlier loop with room, or at the cap) would only delay it by 2 ms per accept, at most about 500 accepts per second per loop. Such listeners never defer, and scalwsd logs once at startup thatfountainhas no effect with per-loop sockets. The shared queue (default) and inherited listeners (one queue shared by all loops) are not affected. - No configuration key; the busy rule, its hysteresis and the surge estimate are unchanged.
Measurements (bench host, 2× E5-2650 v2, server on 0-3 or 0-7, load on node 1)
Default path (shared), bench/multi.sh MODE=release, 5 runs, new = 00ae108,
base = 1ce4f54: requests/s new / base static-small 0.998, respond 0.990, proxy 0.990,
cache-hit 0.990 (gate 0.98), CPU per request 1.002 to 1.010. The policy check is one
branch; the 1 % is within the day’s A/B spread (the first run of the same shared path,
f596f46, gave 0.984 to 0.990).
Open loop, 4 loops, bench/fountain.sh, median of 3 rounds, CPU per request (us):
| load | shared | fountain before | fountain with cap |
|---|---|---|---|
| 10 000 new connections/s | 128.2 | 122.4 (−4 %) | 122.1 (−5 %) |
| 20 000 new connections/s | 120.6 | 116.0 (−3 %) | 116.8 (−3 %) |
| 40 000 new connections/s (CPU bound) | 108.2 | 107.3 | 107.7 |
| proxy, 20 000 new connections/s | 153.3 | 150.9 | 152.1 |
| 512 keep-alive, 40 000 req/s | 61.7 | 60.4 | 61.7 |
The saving is kept (smaller on the bench host than on the development VM); p99 as before (1.9 ms
against 1.1 ms for shared at 10 000/s, 13 to 14 ms against 2.2 ms at 20 000/s).
Closed loop, keep-alive connections opened at once, median of 3 rounds, requests/s:
| load | shared | fountain before | fountain with cap | fountain loop CPU % |
|---|---|---|---|---|
| 4 loops, static, 64 conns | 68 191 | 50 917 | 51 389 | 100/100/60/58 |
| 4 loops, proxy, 64 conns | 45 317 | 32 721 | 32 854 | 100/100/59/58 |
| 4 loops, static, 256 conns | 67 703 | 66 626 | 66 526 | 100/100/100/96 |
| 8 loops, static, 64 conns | 136 395 | 81 403 | 82 526 | 100/100/58/58/57/57/57/57 |
| 8 loops, proxy, 64 conns | 71 178 | 45 301 | 44 879 | 100/100/58/57/56/56/56/56 |
| 8 loops, static, 256 conns | 136 369 | 122 165 | 123 247 | 100/100/100/91/90/91/91/94 |
All connections arrive in one window here, so the cap does not apply to them and the
result is that of the surge estimate: the loop that is awake keeps accepting while it is
full (it never steps back for being busy), loops 0 and 1 end up with most connections.
That loss (0.6x to 0.75x of shared with 64 connections) predates the cap; the
2026-10-09 figures (−4 % to −7 %) came from the 4-CPU development VM.
Kubernetes ingress (16 loops; docs/benchmarks/2026-10-14-xeon-k8s-ingress.md, section
“Fountain with the connection cap”): p99 at 19 000 req/s 2.46 ms (before 2.80; shared
1.57; tuned ingress-nginx 2.05), with Ingress churn 2.35 ms (before 11.15), with backend
churn 2.54 ms (before 6.08); requests/s 0.92x to 0.98x of shared (before 0.92x to
0.96x); controller CPU at the fixed rate 0.88x of shared, as before. Loop CPU at the
fixed rate: 84/78/44/35 % and twelve loops at about 4 % (before 90/89/46 % and 13 at 4 %).
Not done: stepping back while full from a surge
An experiment (not merged) let a loop also step back while it is full from the surge
estimate alone. Closed loop with 8 loops and 64 connections then reached 0.95x of
shared (130 888 req/s), 256 connections 1.00x; but 20 000 and 40 000 new connections
per second fell to about 17 000 req/s and proxied new connections to 12 500 req/s (p99
29 to 41 ms): several loops step back at the same time and the accept rate collapses, the
same mechanism as the first cap version. A version that steps back only while a later
loop has room and is not stepping back itself would be the next candidate.
Consequences of the cap
fountainno longer piles a slow trickle of keep-alive connections onto one or two loops; the CPU saving with many short connections is unchanged.- Connections opened in one burst are still placed by the surge estimate and stay
concentrated (closed-loop figures above; the remaining p99 gap in the Kubernetes
benchmark).
sharedstays the default and the recommendation for ingress mode.