# Replica keys on the proxy path (1.1.1, Task 12)

URL: https://scalws.com/docs/benchmarks/2026-11-22-replica/
Source: `docs/benchmarks/2026-11-22-replica.md` in the ScalWS repository

Question: what do the 1.1.1 replica keys cost a proxied 1 KiB response, against the same
route without them, and against nginx?

Status: after the retry fix (commit d953ff6b) the gate PASSES on both layouts: 0.994 at
1 CPU and 0.986 at 6 CPUs (run 2 below). The first measurement (runs 1 and diagnostic, kept
below as history) had failed at 1 CPU (0.942 and 0.964): the route `retry` key copied the
request head on every request; without it the row was at 0.995 and 0.999.

## Results: run 2, after the retry fix (3 rounds, replica-off and replica-on)

- Build: tree of commit d953ff6b ("Proxy: retry keeps the request head without a
  per-request clone"; the tree of 1457e156 plus the fix), `tools/pgo-build.sh` with
  `PGO_FEATURES=scalwsd/http3`, checkout `[private host path]`.
  - scalwsd sha256 `89c6f2afa1df07ad2adf70566c5c47e6161d9a2dafb9048cc42e382eb534ff9a`
  - scalws-bench-upstream sha256 `2a391a7f73d6ea8229afb2d915bdf2db7061945ab3c67d38cd2d7c6b8cdcb65e`
    (unchanged)
  - `bench/replica.sh` md5 1a8184d5467c299da73b730d9d8b9653 (unchanged), run with
    `SERVERS="scalws-off scalws-on"`, same pinning and durations as run 1.
- Quiet at start: nothing else running; 1-minute load 1.81, the tail of the PGO build that
  finished a minute earlier (the script's check is under 2). Raw rows: `[private host path]` on xeon,
  copied below.

| CPUs | Row | req/s | CPU us/req | p99 ms | Ratio to replica-off |
|---:|---|---:|---:|---:|---|
| 1 | replica-off | 17210 | 58.1 | 4.04 | 1.000 |
| 1 | replica-on | 17182 | 58.2 | 4.07 | 0.994 (0.966-1.000) PASS |
| 6 | replica-off | 100093 | 59.9 | 0.90 | 1.000 |
| 6 | replica-on | 98712 | 60.8 | 0.91 | 0.986 (0.978-1.007) PASS |

Per-round req/s (off / on): 1 CPU 17291 / 17182, 17087 / 16503, 17210 / 17212; 6 CPUs
98953 / 99646, 100093 / 98712, 100744 / 98499. Non-2xx answers: 0. CPU per request at 1 CPU
is now equal (58.2 against 58.1 us; it was 61.1 against 58.6 before the fix). One round at
1 CPU (round 2, 0.966) is below 0.97; the other two are at 0.994 and 1.000.

The fix: `forward` keeps the prepared head across attempts and sets each attempt's `Host`
in place. The own client (the default) encodes the head from a reference before its future
starts, so no attempt copies it, whatever the `retry.on` conditions (`connect_error`,
`timeout` and `status_5xx` alike). The hyper client (`upstream_client: hyper`, the fallback)
owns what it sends, so it still gets a copy for an attempt that may be followed by another,
as before. `pipeline_cost` row `proxy+retry` (the ScalPanel retry on a failover route):
26.0 allocations per request, equal to `proxy+failover` and `proxy+pool`; it was 31.0 before
the fix (5 allocations: the `HeaderMap` copy).

## Setup (all runs; build of runs 1, perf and diagnostic)

- Host xeon: 2x Xeon E5-2650 v2 (no AVX2), kernel 7.0.0-43-generic, quiet at start
  (load under 2, nothing else running). Server on socket 0 (CPU 0 for 1 CPU, CPUs 0-5 for
  6 CPUs), bench upstream on 6,7,22,23, oha 1.16.0 on socket 1 (8-15,24-31), 64 connections,
  5 s warm-up and 15 s per row, 3 rounds, server order rotated per round.
- Build (runs 1, perf and diagnostic): tree of commit 1baaba26 (branch `replica`, Tasks 1 to 11) plus the two new bench
  files of this commit, `tools/pgo-build.sh` with `PGO_FEATURES=scalwsd/http3`, checkout
  `[private host path]`.
  - scalwsd sha256 `82ad4c78a35ae248ded960b7aa327888cb41253884a91912d610d7e321c9bd09`
  - scalws-bench-upstream sha256 `2a391a7f73d6ea8229afb2d915bdf2db7061945ab3c67d38cd2d7c6b8cdcb65e`
  - nginx 1.31.6 (`/opt/nginx-1.31.6/sbin/nginx`).
- Script: `bench/replica.sh` (new, modelled on `bench/edge.sh`; edge.sh is bound to the
  ScalPanel edge fixture, so the rows live in their own script) and
  `bench/configs/nginx-replica.conf`. `replica.sh` md5 on xeon equals the local file
  (1a8184d5467c299da73b730d9d8b9653 for the diagnostic run, bbabf455c98fd0a470ebfdd3287edf02
  for run 1, which lacks only the optional `scalws-noretry` row).
- Both pools have the bench upstream (127.0.0.1:9000) as their only member, so the upstream
  sees the same load in every row. Every request carries `Host: www.example.com`.

Rows:

| Row | Route |
|---|---|
| replica-off | `backends: [{pool: p}, {pool: q}]`, no replica key (weighted 1/1, as 1.1.0) |
| replica-on | two pools with `active_health` (1 s, timeout 500 ms), `balance: failover`, `retry: {on: [connect_error, timeout], max: 1, only_idempotent: true}`, `write_backend: 0`, no sticky |
| replica-sticky | `balance: weighted` with `sticky: {cookie: sp, key_file}` (key file 0600) and `active_health` on both pools |
| replica-noretry | replica-on without `retry` (diagnostic, not in the default row set) |
| nginx-off | `upstream { server local; server local; }` (round robin) |
| nginx-on | `upstream { server local max_fails=1 fail_timeout=1s; server peer backup; }` |

Sticky is not part of replica-on: key list rule 3 allows `sticky` only with `balance:
weighted`; with failover it is warned about and ignored. replica-sticky is reported with its
ratio and not gated. It costs about 6.7 us per request without a cookie (measured: 65.3
against 58.6 us at 1 CPU, 6.6 us at 6 CPUs); oha sends no cookie, so every request makes a
new choice, signs it and sets a cookie.

## History: run 1 before the fix (3 rounds, all rows)

Medians of the three rounds; the ratio is the median of the per-round ratios (min-max in
brackets). Raw rows: `[private host path]` on xeon, copied below.

| CPUs | Row | req/s | CPU us/req | p99 ms | Ratio to replica-off | Ratio to nginx (same variant) |
|---:|---|---:|---:|---:|---|---|
| 1 | replica-off | 17074 | 58.6 | 4.07 | 1.000 | 0.968 (0.952-0.982) |
| 1 | replica-on | 16370 | 61.1 | 4.34 | 0.942 (0.938-0.963) FAIL | 0.903 (0.886-0.922) |
| 1 | replica-sticky | 15317 | 65.3 | 4.51 | 0.881 (0.879-0.899) | n/a |
| 1 | nginx-off | 17916 | 55.8 | 3.90 | n/a | n/a |
| 1 | nginx-on | 18066 | 55.4 | 3.86 | n/a | n/a |
| 6 | replica-off | 98707 | 60.7 | 0.93 | 1.000 | 0.941 (0.937-0.948) |
| 6 | replica-on | 95544 | 62.8 | 1.01 | 0.978 (0.966-0.984) PASS | 0.912 (0.910-0.922) |
| 6 | replica-sticky | 89133 | 67.3 | 1.06 | 0.908 (0.903-0.918) | n/a |
| 6 | nginx-off | 104281 | 57.4 | 0.94 | n/a | n/a |
| 6 | nginx-on | 104789 | 57.1 | 0.90 | n/a | n/a |

nginx's own cost of backup plus max_fails against two servers: 1.010 (1 CPU) and 1.006
(6 CPUs). Non-2xx answers: 0.

Per-round req/s (round: off / on / sticky / nginx-off / nginx-on):

| CPUs | R1 | R2 | R3 |
|---:|---|---|---|
| 1 | 17377 / 16370 / 15317 / 17950 / 18122 | 17074 / 16436 / 15001 / 17384 / 17817 | 17061 / 16003 / 15330 / 17916 / 18066 |
| 6 | 98707 / 96503 / 89133 / 104917 / 104652 | 98870 / 95461 / 89762 / 104281 / 104877 | 97096 / 95545 / 89116 / 103676 / 104789 |

## History: diagnostic run before the fix (3 more rounds, off, on, noretry)

Same binary, same checkout, run after the perf run. Medians.

| CPUs | Row | req/s | CPU us/req | Ratio to replica-off |
|---:|---|---:|---:|---|
| 1 | replica-off | 16911 | 59.1 | 1.000 |
| 1 | replica-on | 16165 | 61.9 | 0.964 (0.950-0.979) FAIL |
| 1 | replica-noretry | 16927 | 59.1 | 0.995 (0.980-1.015) |
| 6 | replica-off | 99526 | 60.2 | 1.000 |
| 6 | replica-on | 97091 | 61.8 | 0.982 (0.967-1.001) PASS |
| 6 | replica-noretry | 97346 | 61.6 | 0.999 (0.975-1.004) |

Over both sets the 1 CPU ratio never reaches 0.97 as a median (0.942, 0.964, and 0.952 in
the perf run); 6 CPUs passes in both sets (individual rounds as low as 0.966).

## History: the missed gate, where the time went

`perf record -F 999 -g` on CPU 0 (1-CPU layout) during the measured 15 s, replica-off against
replica-on (`[private host path]`, round 2, 250 136 and 234 331 requests; the perf run itself gave
0.952, 15899 against 16704 req/s). Samples per 1000 requests: 56.8 (off) and 60.6 (on), +6.7%.
The profile is flat (the largest user symbol is 2.9%), most of it kernel TCP and nftables.
Symbols that exist only with the replica keys, or grow most, user space, in samples per
1000 requests:

| Symbol | off | on | Delta |
|---|---:|---:|---:|
| `http::header::map::HeaderMap::clone` | 0.00 | 0.26 | +0.26 |
| `drop_glue::<HeaderMap>` | 0.00 | 0.13 | +0.13 |
| `PoolProxyHandler::selected` | 0.00 | 0.14 | +0.14 |
| `route_select::select` (failover choice) | 0.00 | 0.11 | +0.11 |
| `PoolProxyHandler::choose` | 0.15 | 0.00 | -0.15 |
| `scalws_server::pipeline::handle` | 1.54 | 1.78 | +0.24 |
| `own_client::Lease::poll_head` | 0.83 | 1.09 | +0.26 |
| `_mi_page_malloc_zero` (allocator) | 0.22 | 0.33 | +0.11 |

The head clone and its drop (+0.39 per 1000, 0.7% of all samples) are the only new work of
that size; the rest is spread over the pipeline and kernel TCP symbols and mostly reflects
the extra allocator and memory traffic (the on row also serves fewer requests per sample
window, which shifts per-request shares of the TCP path). The diagnostic row confirms it:
without `retry` the route costs nothing measurable (0.995 and 0.999).

Cause in the code: `crates/scalws-proxy/src/pool.rs`, `forward` builds the attempt loop with
`retries > 0` when the route has `retry` (`Replay::new(body, retries > 0)`), and the loop
keeps a copy of the request head via `clone_head` (a full `HeaderMap` clone, one allocation
per header block) for the next attempt even when the first attempt succeeds, which is almost
every request. A fix that clones the head only when an attempt fails and a retry follows (or
that retains the head by value for the first attempt and rebuilds it from the prepared
parts), would remove this. `pipeline_cost` does not see it because no row combines `retry`
with a pool route; a row `proxy+retry` should be added with the fix.

## `pipeline_cost` rows

After the fix (tree of d953ff6b), `cargo test --release -p scalws-server pipeline_cost --
--ignored --nocapture` on xeon: every row keeps its allocation count; `proxy+retry` is new.

| Row | ns/request | allocations/request | bytes/request |
|---|---:|---:|---:|
| proxy | 68085 | 22.0 | 7809 |
| proxy+pool | 71753 | 26.0 | 9493 |
| proxy+active_health | 73466 | 26.0 | 9493 |
| proxy+read_only | 73262 | 26.0 | 9493 |
| proxy+backends | 74154 | 26.0 | 9493 |
| proxy+failover | 72861 | 26.0 | 9493 |
| proxy+retry | 73012 | 26.0 | 9493 |
| proxy+sticky | 77257 | 27.0 | 9585 |
| proxy+cookie | 76055 | 27.0 | 9585 |
| proxy+networks | 69810 | 22.0 | 7809 |
| proxy+access_log | 71920 | 26.0 | 8540 |
| proxy+other_log | 69358 | 22.0 | 7809 |
| respond | 3700 | 15.0 | 5432 |
| respond+referer | 3785 | 15.0 | 5432 |
| respond+listeners | 3906 | 15.0 | 5432 |
| respond+xff | 5607 | 20.0 | 6094 |
| respond+countries | 5943 | 20.0 | 6094 |
| static | 9970 | 17.0 | 8256 |
| php | 11318 | 18.0 | 10496 |
| respond+cache | 3725 | 15.0 | 5432 |
| respond+cloudflare | 3991 | 15.0 | 5432 |
| respond+trusted | 5490 | 18.0 | 5492 |

The pool rows allocate 144 bytes less per request than before (9493 against 9637), with the
same allocation count (not investigated further). Before the fix, with the new row added, `proxy+retry`
was 31.0 allocations and 10501 bytes per request.

History, before the fix: same checkout (commit 1baaba26 and the bench files), unit time
includes an in-process upstream:

| Row | ns/request | allocations/request | bytes/request |
|---|---:|---:|---:|
| proxy | 68723 | 22.0 | 7809 |
| proxy+pool | 73160 | 26.0 | 9637 |
| proxy+active_health | 72961 | 26.0 | 9637 |
| proxy+read_only | 73411 | 26.0 | 9637 |
| proxy+backends | 73257 | 26.0 | 9637 |
| proxy+failover | 73579 | 26.0 | 9637 |
| proxy+sticky | 78126 | 27.0 | 9729 |
| proxy+cookie | 74526 | 27.0 | 9729 |
| proxy+networks | 70082 | 22.0 | 7809 |
| proxy+access_log | 72927 | 26.0 | 8541 |
| proxy+other_log | 69151 | 22.0 | 7809 |
| respond | 3467 | 15.0 | 5432 |
| respond+referer | 3534 | 15.0 | 5432 |
| respond+listeners | 3608 | 15.0 | 5432 |
| respond+xff | 5599 | 20.0 | 6094 |
| respond+countries | 5931 | 20.0 | 6094 |
| static | 9466 | 17.0 | 8256 |
| php | 10744 | 18.0 | 10496 |
| respond+cache | 3447 | 15.0 | 5432 |
| respond+cloudflare | 3708 | 15.0 | 5432 |
| respond+trusted | 5500 | 18.0 | 5492 |

The Task 1 to 7 rows are equal where the plan requires: `proxy+pool` =
`proxy+active_health` = `proxy+read_only` = `proxy+backends` = `proxy+failover` (26.0), and
`proxy+sticky` = `proxy+cookie` (27.0, the cookie header's own cost). None of the rows had
`retry`, which is why the clone above was invisible there.

## Other observations

- ScalWS without any replica key is at 0.94 to 0.97 of nginx on this two-pool route and
  1 KiB body (replica-off against nginx-off), at about 58.6 against 55.8 us of CPU per request
  at 1 CPU. This is not part of the replica gate and was not investigated here.
- `active_health` at 1 s on two pools (two probes per second) is not visible: replica-noretry
  equals replica-off.
