Repository navigation
Commit b9cccf8
perf(db): jitter pool connection retirement; configurable reader floor and lifetime (#10047)
## Why
sqlx 0.9 retires a pool connection once it outlives one fixed
`max_lifetime`, which is 300 s here, hard-coded for the writer and
reader pools. A load burst makes every relay pod open many connections
within the same few seconds. With a fixed lifetime, those connections
then all close within the same few seconds. If load comes back after
that, the pod re-dials the whole cohort on the request path (TCP + TLS +
SCRAM for each connection). Pool metrics show this as a sawtooth in
database connections: a burst, a plateau lasting exactly `max_lifetime`,
then a cliff.
**Scope.** Jitter spreads retirement at release time while traffic is
running, so a burst's cohort doesn't close in one cliff at
`max_lifetime`. It does **not** keep capacity warm. After a lull long
enough for the surplus above the floor to retire, the next burst dials
that surplus again, as it would without jitter. A pool with no traffic
is still closed by sqlx's reaper in one pass. Keeping burst capacity
warm is a floor-sizing decision (`BUZZ_DB_READ_POOL_MIN`), outside this
PR.
The reader pool's warm floor is also fixed at one fifth of its ceiling,
with no way to raise it short of growing the whole pool.
## What
- **Lifetime jitter (writer and reader).** Each connection gets its own
retirement point in `[max_lifetime - jitter, max_lifetime]` and closes
the first time it is released after that point (`after_release`), once
its request is done. On the reader, the check runs before the existing
release check and skips that check's round trip. Jitter spreads when
connections close, not when they are dialed again. Below the pool floor,
sqlx's floor maintenance dials the replacement in the background. Above
the floor, a retired connection is dialed again only when a later
checkout finds the idle queue empty, so the timing of that dial depends
on demand, not on the jitter window. The writer floor is 2 (the
`DbConfig` default, not overridden by the relay); the reader floor is a
fifth of the reader ceiling, or `BUZZ_DB_READ_POOL_MIN`. sqlx's
`max_lifetime` still applies as before. sqlx checks it on release and in
its reaper pass, not on idle checkout, so a connection past it is
recycled on release or at the next reaper pass.
- **Per-connection point:** a hash of the connection's creation instant
at 10 µs resolution. sqlx 0.9 keeps no per-connection state that a hook
can attach to. Because sqlx and this code read the clock less than a
microsecond apart, the recomputed instant falls in the true bucket or
the next one. So a connection's point is approximately fixed: almost
always one of two candidates, both inside the window. A thread preempted
between sqlx's age read and the hook's clock read gets a fresh draw,
still inside the window. Connections dialed in parallel get independent
points.
- **Scope: jitter on release.** The point is checked only when a
connection is released, so the spread depends on how often the pool
hands out connections. sqlx's idle queue is FIFO, so each checkout takes
the longest-idle connection, and a pool serving steady traffic cycles
all its idle connections through the check well inside the window. A
quiet pool does not: with 50 idle connections and one checkout every
10–30 s, only a few pass through the check during the 60 s window.
sqlx's reaper closes the rest together, as it does today, and the next
burst re-dials them. The reaper runs every min(`max_lifetime`,
`idle_timeout`) = 300 s, so an untouched cohort closes at the first pass
after 300 s, at an age of up to about 600 s. With the defaults
`idle_timeout` (600 s) never fires, because the reaper re-queues each
kept connection with a fresh `idle_since`; this is existing sqlx
behavior. `BUZZ_DB_READ_POOL_MIN` is the lever for keeping a warm reader
floor through quiet periods. Closing the gap fully needs a
per-connection deadline in sqlx's own idle maintenance, which isn't
possible without changing sqlx, so that is left as a possible follow-up
if metrics still show a cliff. An earlier revision added a separate idle
sweep, but review found it reset sqlx's idle timestamps, could hold a
one-connection reader pool during a request's acquire budget, and
outlived dropped pools, so it was removed.
- **Metrics:** a new counter,
`buzz_db_pool_lifetime_retired_total{pool_role}`, counts retirements; it
is registered in `describe_db_pool_metrics()` and documented in
`deploy/charts/buzz/README.md`. The audit pool reports as `audit`,
separate from the serving `writer`, through a new
`DbConfig::writer_pool_role`.
- **`BUZZ_DB_MAX_LIFETIME_JITTER_SECS`** (default `60`, `0` disables)
sets the window width. It is clamped to the lifetime, and a warning is
logged when it exceeds half the lifetime.
- **`BUZZ_DB_MAX_LIFETIME_SECS`** (default `300`) makes the lifetime
ceiling configurable.
- **`BUZZ_DB_READ_POOL_MIN`** (default: a fifth of the reader ceiling)
overrides the reader warm floor, capped at the reader ceiling.
## Compatibility and rollback
- With no env changes, the only behavior change is jitter: connections
now retire somewhere in 240–300 s instead of at exactly 300 s. The mean
lifetime drops from 300 s to about 270 s, which is roughly 11% more
reconnects per connection. In exchange, while traffic continues, a
burst's connections stop closing all at once. Jitter spreads closes
only; it does not spread re-dials, because a closed connection above the
floor is dialed again only when demand needs it, and it does not stop a
burst from re-dialing surplus capacity after a lull.
- The 60 s jitter default applies to every `Db::new` /
`connect_writer_pool` user: buzz-admin, the deletion workers and
workflow, not just the relay. Only buzz-relay reads
`BUZZ_DB_MAX_LIFETIME_JITTER_SECS`, so the kill switch is relay-only.
Elsewhere, disabling jitter means reverting the commit.
- To restore the previous behavior exactly in the relay, set
`BUZZ_DB_MAX_LIFETIME_JITTER_SECS=0`. Reverting the commit also restores
it.
- The test-only `Db::read_pool_options(max)` keeps its 1-arg signature;
the configurable builder is `Db::read_pool_options_with(max, min,
lifetime)`. #9431's `span_route_tests` compile whichever PR merges
first.
- The audit pool reuses the writer config, so it gets the same jitter,
but it is labeled `audit`. The fence-probe and push-enqueue side pools
are unchanged.
## Rollout plan
1. **Staging first**, with the defaults (60 s jitter, 300 s lifetime,
reader floor = ceiling/5). After a burst, check:
- While traffic continues, `buzz_db_pool_connections{pool_role}` loses
its 300 s plateau-then-cliff and shows a spread over the 240-300 s
window instead. After traffic stops, the remaining surplus can still
close in one reaper pass; that is expected.
- `buzz_db_pool_lifetime_retired_total` runs at about pool size / 270 s
per pod under steady load. It counts retirements at release only, not
reaper closes, so a quiet pool's reaper cliff does not show up in it.
- Writer and reader `buzz_db_pool_acquire_duration_seconds` don't rise.
2. **Then production**, watching the same three signals.
3. **Kill switch:** `BUZZ_DB_MAX_LIFETIME_JITTER_SECS=0` (relay only).
It needs a values change plus a pod roll, so it isn't instant.
Otherwise, revert.
4. **Before raising `BUZZ_DB_READ_POOL_MIN`:** floor x relay pods,
including old and new pods overlapping during a rolling deploy, must fit
under the replica's `max_connections`.
5. **Mixed versions:** config and behavior only, with no schema or wire
change, so it is safe during a rolling deploy.
## Testing
- `cargo test -p buzz-db --lib` and `cargo test -p buzz-relay --lib
config::` pass. New unit tests check that:
- retirement points stay inside the window;
- points spread across the window for dials 10 ms apart and for a 10 ms
burst of 500 dials 20 µs apart, with every connection in that burst
getting its own point;
- a connection on a bucket boundary gets points inside the window;
- jitter is clamped, and zero jitter leaves retirement to sqlx;
- the reader-floor override is applied and capped;
- the production `connect_read_pool` applies the configured floor and
lifetime;
- env parsing works and falls back on bad values.
- New Postgres test
`trickle_cycles_idle_cohort_through_jittered_retirement` (writer): a
writer pool warms 12 connections in one burst (4 s lifetime, 3 s jitter,
floor 3), then serves one query every 20 ms. It asserts that retirements
land in several separate releases spread over at least 0.5 s, and that
the floor of 3 holds. With the release check disabled, it fails: all
nine surplus connections close within 0.18 s of each other at about 4 s.
- New Postgres test `retiring_reader_release_skips_the_release_check`
(reader, local Postgres 17, passes): connections released inside an open
transaction past their retirement point are counted as
`lifetime_retired_total` retirements, never as `open_transaction`
closes. A jitter-off control pool shows the release check does catch the
same session. The test records each cohort backend's pid and waits for
all 8 cohort backends to exit before asserting exact totals from the
final snapshot; each backend closes only after its release hook counted
it. Mutation (check-first): running the release check before the
retirement decision makes it fail with 8 `open_transaction` and 0
retired. Mutation (both): a variant that runs both the check and the
retirement fails with 8 `open_transaction` plus 7 retired, which the old
sum-threshold exit could have missed. The existing trickle test still
passes under the check-first mutation, which is why this test was
needed.
- New Postgres test
`reader_trickle_cycles_idle_cohort_through_jittered_retirement`, built
through `Db::connect_read_pool` (4 s lifetime, 3 s jitter, floor 3). It
asserts that at least 3 retirements span at least 500 ms, the floor
settles at 3, `buzz_db_pool_lifetime_retired_total{pool_role="reader"}`
is at least 3, and `buzz_db_reader_release_closed_total` stays 0 because
a retiring release closes before the release check. Removing the reader
hook's `if retire { return Ok(false) }` makes it fail: all 9 retirements
land in one reaper pass at 4.02-4.19 s.
- Local runs (2026-12-01), before the latest follow-up: `cargo clippy -p
buzz-db -p buzz-relay --all-targets -- -D warnings` and `cargo fmt`
clean; `buzz-db` lib 234/234; the two pool Postgres suites
(`pool_release_postgres_tests`, `reader_pool_postgres_tests`, 16 tests)
against local Postgres 17 passed 16/16 in each of 3 runs.
- Local runs on Postgres 17 (2026-12-01), after the latest follow-up:
`cargo clippy -p buzz-db -p buzz-relay --all-targets -- -D warnings`
clean; the pool suites (`pool_release_postgres_tests` and
`reader_pool_postgres_tests`) 17/17; `buzz-db` lib 234/234.
- After merging `origin/main` at 59a51c2: fmt and clippy clean,
`buzz-db` lib 234/234, and `buzz-relay` `config::tests::` + `metrics::`
79/79.
- The chart README now states the same scope: jitter spreads
release-time retirement while traffic runs; it does not keep capacity
warm through a lull; raise the floor to keep burst capacity warm.
- Not yet done: a local relay run.
## Follow-up commits
- `docs(db): drop staggered-redial claims; wait for every cohort close
in the ordering test`: the chart README, `.env.example` and buzz-db docs
no longer say jitter spreads replacement dials (it spreads closes; a
closed connection above the floor is dialed again only when demand needs
it), and the ordering test waits for every cohort close before asserting
exact totals.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: Jemiah Westerman <jemiah@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jemiah9000 <9424fd1a46ba04715d24ac3967d4a66d3cfcddd5fbe56a229c8386b4159810bf@buzz.block.builderlab.xyz>1 parent a12a999 commit b9cccf8
11 files changed
Lines changed: 854 additions & 21 deletions
File tree
- crates
- buzz-db/src
- runtime
- tests
- store
- buzz-relay/src
- deploy/charts/buzz
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
37 | 54 | | |
38 | 55 | | |
39 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
0 commit comments