Skip to content

Open a fifth connection so held polls have two read slots - #1378

Open
rmcdaniel wants to merge 1 commit into
mainfrom
general/server-1372-two-read-slots
Open

rmcdaniel wants to merge 1 commit into
mainfrom
general/server-1372-two-read-slots

Conversation

@rmcdaniel

@rmcdaniel rmcdaniel commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Part of #1372.

General decided "yes" on #1372: add a fifth pool connection. This PR does that and addresses the review of head 74d3684a. The targets are only partly met. The Evidence section has the numbers and the reason.

The later-probe slot-wait fix was split out to #1380 at review, so this PR no longer contains it.

Change

Five connections. POOL_CONNECTIONS goes from 4 to 5: a request pool of 4 plus the maintenance connection.

  • Held polls' reads take the request pool less two, so there are two read slots.
  • The transition queue's holder takes one more slot.
  • Producers and registrations keep the last connection.
  • DW_WORKER_LONG_POLL_RESERVED_HTTP_WORKERS=2 leaves two probe permits.

Split statement-cache budget (review item 2a).

  • The maintenance connection keeps at most 48 statements, or none when the cache is off.
    • This goes through Backend::maintenance_connect_options, used where Store::with_maintenance_pool opens that pool.
    • That needs a new get_statement_cache_capacity getter on both vendored drivers' connect options (documented in DURABLE-PATCH.md).
  • The request connections keep 192 by default. That is above the Native: hot-path statements exceed sqlx's statement cache, causing prepare/close churn #1174 working set (MEASURED_WORKING_SET = 166, restored) and fits the 20-node budget: 20 × (4 × 192 + 48) = 16320 ≤ 16382.
  • MAX is now 4083, the largest value that keeps one node within the default limit.
  • The hot-path qualifications run 180 statements again, on MySQL and PostgreSQL.

Per-queue poll wakes (poll_wakes.rs).

  • Before: one Notify woke every held poll on the node at every commit.
  • Now a transaction records the namespace and queue of every ready task create_task_dispatched creates, and its commit wakes only those queues' held workflow and activity polls.
  • The hottest commits now wake only those queues, plus the waiters that are not held worker polls (query-task polls; update, query and validation result waits), instead of every poll. These are starts, signals, and workflow and activity task completions. A workflow task completion also wakes its run's queue, whose other workflow tasks it unfences.
  • Every other commit still wakes everyone.
  • A missed wake costs latency until the next timed read, never a task.

Idle-read pacing (review item 3).

  • A held poll's timed reads back off to a ceiling that grows with the number of polls waiting on the node: 250 ms for up to 31, then 8 ms per waiting poll, at most 2 s.
  • No pause is shorter than a quarter of the ceiling.
  • Same-node wakes still read at once.
  • A held poll's later probes check its queue for abandoned workflow task leases at most once a second per queue per node. Every poll's first probe still checks. A lease is abandoned only after its owner has gone 30 s without a heartbeat, so a check a second later finds the same leases. These checks were about half of all held-poll reads at 64 to 256 idle polls (measured below).

Harnesses committed (review item 4).

  • runtime::mysql::held_poll_bench (mysql_held_poll_bench.rs): ignored benchmarks for 8, 64 and 256 held polls, with cfg(test) read-slot wait statistics in probe_connections.rs.
  • tests/execution/hot_path_cache.rs: an ignored statement-cache measurement. It reports prepares, closes and hit rate from the server's Com_stmt_* counters, and per-connection cached statements when performance_schema is on. The per-route hit rates in the first revision came from a scratch hook in the vendored driver that is not committed.

Docs.

  • docs/server-reference.md: capacity, the reserved-connection mapping, and the statement cache row. It cites Serve high held-poll counts without a single node-wide probe read slot #1372 for about 126 statements and Native: hot-path statements exceed sqlx's statement cache, causing prepare/close churn #1174 for about 166 on the PHP SDK workload.
  • rust/README.md: the cache section, the formula, and the "Four connections" line.
  • Helm chart docs: deferred to release preparation.
    • The chart's CI requires a Chart.yaml.version bump for any change under k8s/helm/durable-workflow.
    • The chart pins Server 3.30.0, which still opens four connections, so bumping the chart now would publish docs that are wrong for the image it ships.
    • When a chart release pairs with the server release that includes this PR, the chart README.md and UPGRADING.md need the text below.
    • The first push of this revision included them; CI's chart job then failed on the missing version bump.

Release note

  • A native node opens five database connections instead of four. Held polls get two read slots.
  • Commits wake only the held polls of the queues they make ready. Idle held polls pace their timed reads by load.
  • DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY (request connections) now defaults to 192, down from 200, and accepts up to 4083, down from 4095. The maintenance connection keeps at most 48.
  • MySQL clusters keep nodes × (4 × capacity + min(capacity, 48)) + other clients ≤ max_prepared_stmt_count; that allows 20 nodes at the default.
  • DW_WORKER_LONG_POLL_RESERVED_HTTP_WORKERS=2 leaves two probe permits.

For the chart release that pairs with this server change

In k8s/helm/durable-workflow/README.md, under "Native capacity and storage", replace the first bullet:

  • Each native pod opens at most five database connections (a request pool of four and the maintenance loop's). Size the database's connection limit for five per pod at the highest replica count (server.autoscaling.maxReplicas when autoscaling), plus one for a schema upgrade. On MySQL also keep pods × (4 × DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY + min(DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY, 48)) plus other clients' statements within max_prepared_stmt_count (816 per pod at the default capacity of 192, about 20 pods on a default server).

Add this to docs/UPGRADING.md for that release:

  • Connection limit: five database connections per native pod instead of four. Size the connection limit for five per pod, plus one for a schema upgrade.
  • MySQL prepared statements: the guidance replicas × 4 × 200 no longer applies. Each request connection caches up to 192 statements by default and the maintenance connection up to 48. Keep replicas × (4 × capacity + min(capacity, 48)) plus other clients' statements within max_prepared_stmt_count: 816 per pod at the default, about 20 pods on a default server. Recompute max_prepared_stmt_count, or DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY, if either was sized for the old formula.
  • Capacity bound: DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY (or the statement-cache-capacity DB_URL parameter) accepts 0 to 4083. An explicit value from 4084 to 4095, accepted before, now stops startup.
  • Reserved connections: DW_WORKER_LONG_POLL_RESERVED_HTTP_WORKERS=2 leaves two probe permits on the request pool of four.

Evidence

Statement cache at the default (committed harness, MariaDB 10.11 with performance_schema on): 2.52 prepares and 0 closes per workflow, 99.71 % hit rate (200 workflows). At the end, the cached statements were 8 on the maintenance connection and 124, 126, 149 and 160 on the four request connections: all within 192, and the largest exactly at the 160 the first revision proposed.

From the first revision's capacity sweep (same workload; per-connection working set 123 to 126):

Capacity Prepares / workflow Closes / workflow Hit rate
200 2.50 0 99.71 %
160 2.52 0 99.70 %
128 2.51 0.26 99.71 %
100 17.42 15.70 97.96 %

Held polls, with the committed harness.

starts/s start p50 / p99 read-slot wait p50 / p99 reads dispatch p50 / p99 empty poll p50 / p99 probe pool_timeout / poll errors
pre-#1358, 8 27.2 / 27.0 147–148 / 171–172 ms 0 / 0.4–1.9 ms 2.2k 16 / 63 ms 2.01 / 2.02 s 0 / 0
main, 8 25.5 / 25.0 / 25.5 157–160 / 178–195 ms 0 / 6–10 ms 2.0k 16–17 / 73–81 ms 2.01 / 2.02 s 0 / 0
branch, 8 25.5 / 25.3 157–159 / 183–186 ms 0 / 3.7–3.8 ms 1.7k 17 / 83–86 ms 2.01 / 2.01 s 0 / 0
pre-#1358, 64 14.5 / 14.6 274–281 / 419–430 ms 16–17 / 55–57 ms 16.2–16.4k 144–147 / 369–377 ms 2.05–2.07 / 2.31–2.33 s 0 / 0
main, 64 18.6 / 18.2 / 18.1 207–223 / 352–433 ms 87–94 / 143–155 ms 6.9–7.0k 105–125 / 335–387 ms 2.13 / 2.42–2.45 s 0 / 0
branch, 64 14.4 / 14.3 282–284 / 447–452 ms 0.4 / 48–50 ms 6.6k 71 / 347–354 ms 2.03–2.05 / 2.34–2.38 s 0 / 0
pre-#1358, 256 10.1 / 10.5 212–279 / 1147–1200 ms 117–121 / 336–433 ms 19.1–19.2k 212–241 / 653–736 ms 2.20 / 2.64–2.85 s 0 / 0
main, 256 15.3 / 15.1 / 14.8 195–222 / 1127–1282 ms 369–399 / 608–653 ms 8.7–8.8k 261–276 / 1015–1216 ms 2.58–2.60 / 2.99–3.08 s 0 / 0
branch, 256 8.1 / 8.2 444–452 / 1168–1271 ms 40–42 / 269–320 ms 13.5k 249–271 / 651–734 ms 2.11 / 2.52–2.53 s 0 / 0

Against the targets:

  • Empty polls on time: met. At 256 polls p99 is 2.52 s, against main's 3.0 s and pre-Keep registrations and producers answering through database stalls #1358's 2.64–2.85 s. At 64 polls it is at or below main.
  • Zero probe 503s: met. There were no probe pool_timeouts and no poll errors in any run.
  • 256-poll dispatch p99 near 0.56 s: partly met. It is 0.65–0.73 s, against main's 1.0–1.2 s. That matches the pre-Keep registrations and producers answering through database stalls #1358 level measured in the same session (0.65–0.74 s); the 0.56 s figure came from an earlier session.
  • 64-poll dispatch: p50 improves from 105–125 ms to 71 ms. p99 is unchanged (about 0.35 s).
  • Producer throughput and start latency no worse than main: not met.
    • At 64 polls: 14.3–14.4 starts/s against main's 18.1–18.6 (−21 %). Start p50 is 282 ms against 207–223 ms, and p99 is 447–452 ms against 352–433 ms.
    • At 256 polls: 8.1–8.2 starts/s against 14.8–15.3 (−45 %). Start p50 is 444–452 ms against 195–222 ms.

Why producers are worse.

The transition queue at 256 polls. Measured with scratch counters (ReadPace returns by cause) and the totals above.

  • Every idle poll's first probe claims under the transition lock unconditionally, the PHP-parity "first probe claims" behaviour. With 2 s polls that is about one claim per poll cycle.
  • Starts plus poll cycles come to about 130 transitions/s in every tree at 256 polls. Each row below is poll cycles plus starts, per second:
  • The serialised transition queue is saturated. Each improvement that lets idle polls complete more cycles on time takes transition capacity from producers.
  • The pacing steps show this directly. The wake and ceiling step gave 10.5–11.5 starts/s, the floor step 10.1–10.5, and the recovery throttle step 8.1–8.2. Each step served more poll cycles: 1,890, then 1,890, then 1,960.

Read concurrency at 64 polls. The queue is not saturated here (about 57–61 transitions/s), and this branch makes about the same number of reads as main (6.6k against 6.9k) with almost no read-slot waiting.

  • Producers are still slower: 14.4/s here and 14.5/s on pre-Keep registrations and producers answering through database stalls #1358 (3 read slots), against main's 18.1–18.6/s (1 read slot).
  • A scratch variant of this branch with one read slot gets 15.8–16.0 starts/s at 64 polls and 13.8–14.5 at 256, near main. But its dispatch p99 returns to about 1 s and its empty-poll p99 to about 3 s.
  • On this 2-CPU database, two concurrent readers take CPU from the single writer.

The benchmark's 2 s polls. They multiply the per-cycle costs. Per cycle these are: admission, the first probe's claim, and the abandoned-lease check. SDK-typical 20–55 s polls amortise those 10–25×.

Options, not done here so as not to change claim semantics or tune silently:

  1. Gate an idle poll's first-probe claim behind the same lock-free readiness read later probes use. That takes about 130 claims/s at 256 polls off the transition queue. It changes the PHP-parity first-probe behaviour, so it needs a decision.
  2. Give the transition queue's work priority over idle timed reads, for example by deferring timed reads while transitions are queued, bounded by the ceiling.
  3. Make the read-slot count configurable, so CPU-limited databases can trade dispatch latency for producer throughput.
  4. Rerun on a production-sized database and with SDK-typical poll timeouts.

Other tests

  • poll_wakes:
    • a commit wakes only the queues it made ready;
    • waiting polls are counted;
    • a queue is checked for abandoned leases at most once per interval.
  • execution::poll_wake_tests::hot_commits_wake_the_queues_they_make_ready: a SQLite runtime. A start, a workflow task completion that schedules an activity on another queue (that queue and the run's own are woken), an activity completion and a signal each wake exactly the expected queues. The signal is shown to have made the run's task ready.
  • poll_readiness::the_timed_read_ceiling_grows_with_waiting_polls_within_bounds.
  • backend::maintenance_cache_tests::the_maintenance_connection_keeps_a_small_cache_or_none for MySQL and PostgreSQL, including capacity 0.
  • statement_cache: the bound, the defaults and the per-node total.
  • Locally, before the rebase onto 803ef851 (Bound silent database peers during release and in-flight statements #1375):
    • lib suite: 656 passed;
    • MySQL pool, stall and hot-path (180 statements) qualifications: 17 passed;
    • PostgreSQL qualifications: 49 passed.
  • After the rebase onto 803ef851:
    • lib suite: 662 passed;
    • MySQL pool, stall, hot-path and carried-poll qualifications: 20 passed;
    • PostgreSQL qualifications: 51 passed.

Trade-offs

  • Lower producer throughput on a CPU-limited database under many held polls, as above.
  • Cross-node latency. Work committed by another server process is seen within the timed-read ceiling: up to 2 s with 250+ idle polls on a node, against 250 ms before. Same-node work wakes polls at once.
  • Abandoned-lease recovery on held polls' later probes may run up to 1 s later than before.
  • One more database connection per pod.

🤖 Generated with Claude Code

…eady, and pace idle reads

With one node-wide read slot, held polls' reads queued behind each other at
production poll counts (Server #1372).

Five connections: a node now opens a request pool of four plus the
maintenance connection, so held polls' reads take two slots, the transition
queue's holder one more, and producers and registrations keep the last
connection. DW_WORKER_LONG_POLL_RESERVED_HTTP_WORKERS=2 leaves two probe
permits.

Statement cache: the maintenance connection keeps at most 48 prepared
statements (none when the cache is off), through a backend hook and a new
get_statement_cache_capacity getter on both vendored drivers' connect
options, so the request connections keep 192 by default: above the #1174
working set (166) and within MySQL's default max_prepared_stmt_count at 20
nodes (20 x (4 x 192 + 48) = 16320). The capacity bound becomes 4083. The
hot-path qualifications run 180 statements again.

Per-queue wakes: one Notify woke every held poll on the node at every
commit. A transaction now records the namespace and queue of each ready task
create_task_dispatched creates and its commit wakes only those queues' held
workflow and activity polls (poll_wakes.rs). Starts, signals and workflow and
activity task completions wake those queues (a completion also its run's
queue, whose other tasks it unfences) and the waiters that are not held
worker polls (query-task polls, update, query and validation result waits),
instead of every poll; every other commit still wakes everyone. A missed wake
costs latency until the next timed read, never a task.

Idle-read pacing: a held poll's timed reads back off to a ceiling that grows
with the polls waiting on the node (250 ms up to 31, then 8 ms each, at most
2 s), and no pause is shorter than a quarter of it. A held poll's later
probes check its queue for abandoned workflow task leases at most once a
second per queue per node; every poll's first probe still checks. Those
checks were about half of all held-poll reads with many idle polls.

Harnesses: runtime::mysql::held_poll_bench (8, 64 and 256 held polls with
producers, reporting read-slot waits, poll answer and dispatch latency and
start throughput) and tests/execution/hot_path_cache.rs (statement cache
prepares, closes and hit rate per workflow), both ignored benchmarks.

Docs: capacity and the reserved-connection mapping, the statement cache
default, bound and formula (server reference, rust README). The Helm
chart's README and UPGRADING change when a chart release pairs with the
server release that includes this, as chart changes require a chart version
bump.

Part of #1372.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmcdaniel
rmcdaniel force-pushed the general/server-1372-two-read-slots branch from ebd5dc5 to 048b36c Compare November 26, 2026 18:55
@sonarqubecloud

Copy link
Copy Markdown

@rmcdaniel

Copy link
Copy Markdown
Member Author

Held, not merged (General). At 048b36c7 CI is green, but the targets are only partly met.

The measured cause is the serialized transition queue saturating at about 130 transitions/s. Each idle poll's first probe claims under the lock (PHP parity), and the 2 s bench polls make that roughly one claim per cycle.

Deciding needs either a benchmark with SDK-typical poll timeouts (20–55 s) or a decision on the first-probe claim (options in the description). The later-probe fix was split out and merged as #1380.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant