Files
buzz/crates/buzz-db
Luke Tornquist d44031f418 perf(relay): cut writer-pool round trips past the 48k relay ceiling (#8846)
## Summary

Five changes that take load off the writer pool and the relay's
connection bookkeeping. On a single-relay load rig, the clean ceiling
moves from 40k to about 48k WebSocket clients. In the A/B, connect p99
at 48k fell from 4.1 s (abort) to 1.65 s.

1. **Ban/mute restriction cache with a 1 s max age.** HTTP API requests,
publishes and ephemeral writes read `(community, pubkey)` restriction
state through one cache. An entry is served for 1 s from when its read
began, matching the open-channel cache's bound. Socket admission,
moderation-command actor checks and the `/buzz/v1` ban recheck still
read fresh. An admitted socket outlives the 1 s window, so a banned
client that reconnects inside it would otherwise keep a socket open
indefinitely. A ban drops the target's entry, and each owned agent's
entry, on every pod as part of the live-disconnect fan-out, best-effort.
Timeouts, untimeouts and unbans drop it on the pod that handles them.
2. **Fewer round trips per event read.** Replica reads skip the per-read
`statement_timeout` tighten, because the reader pool installs the
deadline at connect. They keep the savepoint, so a failed or cancelled
read leaves the session usable and releases its locks. That takes them
from 4 round trips to 3. Writer reads send `BEGIN` and the tighten as
one message, which also takes them from 4 to 3.
3. **Concurrent open-channel cache misses share one read.** The first
miss in a community takes a per-community gate and reads; the others
wait for it and use its result. A waiter whose leader failed reads once
on its own and does not queue again. A waiter also stops waiting after
the cache's 1 s max age and reads on its own, because the leader's set
would be stale by then anyway. So a leader whose read hangs stalls only
its own request.
4. **`has_admitted_connection` is one map lookup.** A `(community,
pubkey) → admitted count` index is maintained under each socket's pubkey
lock in `mark_admitted`, `set_authenticated_identity` and `deregister`.
It replaces a scan of every connection on each disconnect.
5. **Host → community cache with a 1 s max age.** Only successful
lookups are cached; unmapped hosts and errors go to the database every
time.

## What got simpler

- The ban handler's separate pod-local cache drop is gone; the
disconnect every ban already runs covers it on every pod.
- One write-restriction gate instead of two.
`enforce_cached_write_restriction`, `enforce_relay_membership_ephemeral`
and the `cached_ban` parameter chain through `enforce_membership` /
`admit_member` / `deny_banned` are deleted.
- The ephemeral path's separate 30 s ban cache is gone, so the relay has
one staleness rule for restriction state instead of two.

## Notes

- Bans reach the restriction cache on every pod. Every ban path (signed
command, report action, relay admin) closes the target's and its agents'
sockets clusterwide, and each pod drops those entries in the same step.
A dropped Redis publish, or a restriction read already in flight when
the drop lands (it can refill the old row), falls back to the 1 s bound.
Timeouts on other pods also rely on the 1 s bound, and so do unbans and
untimeouts, which fail closed.
- On the load rig the restriction-cache hit rate was 28%, because HTTP
agents poll every 5 s. A longer TTL with Redis invalidation would raise
it, but that is a separate change.

## Load test

Single relay pinned to 8 cores, native Postgres 16, a seeded fixture
with 2,000 open channels, and a 6-shard generator. Steady ramp with 120
s holds; a step aborts at p99 > 3 s. Each step adds 10% HTTP agents.

| clients | base connect p99 | this PR connect p99 | base relay cores |
this PR relay cores |
|---|---|---|---|---|
| 32k | 34 ms | 31 ms | 1.71 | 1.66 |
| 40k | 45 ms | 26 ms | 2.33 | 2.15 |
| 48k | 4,107 ms (abort) | 1,652 ms | 4.7 | 3.0 |
| 56k | n/a | 4,535 ms (abort) | n/a | 4.0 |

Open-channel re-reads over the run fell from 72k to 1.6k. The tenant
cache hit rate was 94%.

48k is this branch's knee. A later run of the same build aborted at 48k:
connect p99 2.2 s, but 29% subscribe errors from the concurrent-handler
cap. With a 10 s restriction max age, it passed 48k, at connect p99 1.56
s, 60% hit rate and 10% fewer relay cores, and both arms stopped at 56k.

## Testing

- `cargo fmt --check`, `cargo clippy -p buzz-relay -p buzz-db
--all-targets -D warnings`
- `cargo test -p buzz-db --lib` and `-p buzz-relay --lib`, run in
parallel. The only failures were 8 DB-seeding tests that need a migrated
default database; the Postgres lane covers them. Commits after that run
changed the replica savepoint, the `/buzz/v1` recheck revert and some
test edits.
- Postgres lane at head: `scripts/postgres-test-run.sh -p buzz-relay -p
buzz-db --lib --bins --tests`. All 1,183 tests passed.
- Mutants. Each one must turn its new test red:
  - waiters skipping the gate
  - a no-op index removal
  - the tenant cache ignoring age
  - the restriction cache ignoring age
  - a waiter waiting on the gate with no limit
  - a disconnect that keeps the cached restriction row
  - replica reads dropping the savepoint

buzz-review-completed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 11:15:49 -05:00
..