~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

Spaces' issuer keys renewed in the background; PLC reads reuse connections

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:perf/issuer-keys

In production, 7.7% of authorizeSpaceSync calls waited about 350 ms for the PLC directory. Together they made up 254 of its 300 s of server time. This keeps spaces' issuer keys for PDS_DID_CACHE_SECS and renews them in the background, with one read per issuer at a time. Reads from the directory now reuse an open connection.

Claude Opus 5.5 · high

What changes

Spec. SPACES-CREDENTIALS.md, "Issuers' keys": an issuer's key is kept for PDS_DID_CACHE_SECS, not a fixed 5 minutes. Once a key is half that old, a token it verifies is answered at once and the issuer's document is read again afterwards. A key unused for the whole of PDS_DID_CACHE_SECS is read again before it is used. Each issuer's document is read once at a time, and calls that need it meanwhile wait for that read. Reading again when a signature fails against a kept key is unchanged. LIMITS.md: the PDS_DID_CACHE_SECS row names issuer keys, and "The PLC directory" gains Connections. A read goes over a connection an earlier read left open, kept up to 90 s. A read that fails on its way is sent once more over a connection of its own. A post always takes a connection of its own.

Harness. harness/spaces/issuers.mjs runs in the spaces run after the guard, against the server alone. It starts the server with PDS_DID_CACHE_SECS=6 and makes did:web issuers whose documents a harness listener serves, recording each read:

  • 8 revocations at once from an issuer not read before take exactly one read;
  • a revocation 3.5 s after the read, with the document now 1.5 s slow, is answered within 1 s, and the document is read once more after it;
  • a revocation 7 s after that read waits for a read of its own.

In the limits run, plcConnections makes 6 accounts one after another through the PLC proxy, which now records the requests each connection carried. Each post must be the first request on a connection of its own, and the reads must take fewer connections than there are reads.

pds-server. space_credentials::jwt::Keys keeps each key with the time its read began, and gives each issuer a turn (a per-issuer async mutex, as blobstore's turns are). A stale key that verifies spawns renew, which reads unless another read is under way or one has ended since. The credential paths take &Shared so that renew can be spawned. plc::document reads over a pooled client (90 s idle) and retries over the caller's no-idle client. Posts keep plc::client().

Why

From monolith.chadtmiller.com, over 4.4 h of its log and 52 h of /metrics:

  • pds_outbound_request_duration_seconds{target="plc"} averaged 362 ms over 8,427 reads, and none took under 250 ms.
  • From the box, plc.directory takes 360 ms on a new connection (connect 116 ms, TLS done at 245 ms) and 116 ms on a reused one.
  • Slow authorizeSpaceSync calls came in bursts every 5 minutes, at KEY_KEPT = 300. Up to 8 of them arrived in the same millisecond and made 8 separate reads.

Considered: verifying the server's own accounts' tokens with the key the server holds, skipping the read altogether. Once keys are renewed in the background, that saves no call any wait, only background reads, and it adds a second path with its own rules for accounts that rotate keys or move away. So it is left out.

Kept: a rotated-to key is still taken at once, because a failed signature forces a read. A key rotated away from is now taken for up to PDS_DID_CACHE_SECS (an hour by default) rather than 5 minutes. That matches how long the server already keeps account and service-token documents.

How it was checked

  • The server as on main (cc1f0b6). issuers.mjs: 8 reads, not 1; no renewal; no read once the key was past its time. plcConnections: 6 reads over 6 connections.
  • Bugs planted in this change. With renewal, the issuer turn and the pooled client each disabled, each check failed the same way. With the change in place, both checks pass.
  • Local runs. issuers.mjs was run on its own against a lone server through a throwaway driver (removed). The limits run was run with --only plcDocuments,plcAtOnce,plcConnections.
  • Cargo tests. cargo test -p pds-server --lib -- space_credentials plc:: space_notify identity:: passes. a_rotated_key_is_read_again now sets did_cache = 3600, since the test config's 0 keeps no key.
  • Gates. just quick passes here (all 7 runs). The full gates are the gates workflow on this pull.

Shortcomings

  • The renewal timing (3.5 s and 7 s against a 6 s window) is checked only by the spaces run with a short PDS_DID_CACHE_SECS. The default hour is not run.
  • The pooled reads client is a process-wide static. Across tokio runtimes (the crate's tests), a connection left by a runtime that has ended fails and is retried over a connection of its own.
  • The other uncached lookups seen in production are not in this pull. getBlob waits about 100 ms on R2, and the blobstore and outbound clients keep connections idle only 2 s.

Review

Start at verify_issuer and renew in crates/pds-server/src/space_credentials/jwt.rs, then the "Issuers' keys" paragraph.

Approved

This keeps spaces' issuer keys for PDS_DID_CACHE_SECS. Once a key is half that old, it renews the key in the background. It reads each issuer's document once at a time. It also reads PLC documents over a pooled client that retries once on a connection of its own. I read the whole diff and found no defects.

  • Behaviour in code: Turn handling is sound. The key is stamped with the time its read began, so a late write can't make a key look younger than it is. A failed renewal leaves the old key in place. PDS_DID_CACHE_SECS=0 keeps nothing and reads every time, as the spec says.
  • Spec: The change is in SPACES-CREDENTIALS.md and LIMITS.md, with the "Connections" bullet and the harness descriptions. The changeset is written for operators.
  • Harness: The new checks only add strictness. issuers.mjs runs against the server alone, and it states what it compares in each case. The proxy change only adds recording.
  • Docs: docs/ has no mention of PDS_DID_CACHE_SECS, so there is no guide to update.

Minor, no change needed: while the directory is down, every call that finds a half-old key spawns another renewal. Each runs one at a time and the PLC turn limits them, so this is bounded.

All checks have passed

4 passed, 1 skipped

Merged as 7425de9