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

Spaces' issuer keys at scale: local keys, a day's window, a bound, kept

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

On a PDS with hundreds of thousands of users, keeping spaces' issuer keys has three problems: the cache cleared itself at 10,000 keys, renewals every 30 minutes could fill the PLC directory's 8 slots, and every restart read every issuer again. This verifies the server's own accounts with the key it already holds, renews other issuers' keys hourly and keeps them up to a day, drops the key used longest ago past a bound, and keeps the keys across a shutdown.

Claude Opus 5.5 · high

What changes

Spec. SPACES-CREDENTIALS.md, "Issuers' keys":

  • An issuer that is an account here is verified first against the key the server holds for it, with nothing read. A token that fails against it goes on as any other issuer's.
  • Other issuers' keys are fresh for PDS_DID_CACHE_SECS. From then until the new PDS_DID_CACHE_MAX_SECS (default a day) they are still used, and read again after the answer. Past that, they are read first.
  • At most one key per 1024 bytes of PDS_CACHE_BYTES (262,144 by default). Past that, the key used longest ago is forgotten.
  • Keys are written to PDS_DATA_DIR/issuer-keys at shutdown and at most once a minute, never synced, and read back in the background after a start.

LIMITS.md adds the variable. STORE.md counts the keys in the Memory and Settled bounds.

Harness.

  • harness/spaces/issuers.mjs, against the server alone, with PDS_CACHE_BYTES=65536 so that 64 keys are kept:
    • one read for 8 calls at once;
    • after 65 more issuers, the key used longest ago is read again, and one used since is not;
    • after a SIGTERM restart, a kept key is not read again.
  • Then with PDS_DID_CACHE_SECS=3 and PDS_DID_CACHE_MAX_SECS=8: a call at 3.5 s is answered within 1 s and the key is renewed after it; a call at 9 s waits for a read.
  • restartWith in the spaces run can shut the server down gently.
  • Limits: plcLocalIssuer. Delegation tokens of accounts here, traded at authorizeSpaceSync, must read nothing from the directory.

pds-server. Built by a fresh agent given AGENTS.md's step 3 rules and the spec alone. Neither it nor its instructions saw the reference's source. The code is in space_credentials/issuers.rs (moved out of jwt.rs):

  • Kept holds the keys in a list ordered by last use, the key used last first, each packed into 34 bytes.
  • held gets the key of an account here.
  • The file is a magic line, then the keys, then the SHA-256 of all before. It is written to a temporary file and renamed into place. Read-back is checked whole.
  • ops::shutdown writes the keys; run() starts the read-back and the writer each minute once the server serves.

Why

At 100k active issuers:

  • The cache emptied itself. KEYS_KEPT = 10_000 cleared every key once full, so the cache emptied over and over.
  • Renewals could fill the directory's slots. Renewing at half of PDS_DID_CACHE_SECS is about 55 reads/s, near the about 69/s that 8 directory slots allow at about 116 ms each. Hourly renewal halves that.
  • A restart read every issuer again. It started with no keys, so every active issuer was read again, calls queued behind the 8 slots, and some waited out the 15 s limit.

Accounts here need no read at all: the server signs their tokens.

How it was checked

  • Against main (77b758e): the 64-key bound is never applied, the key is read again after a restart, a key past PDS_DID_CACHE_SECS waits 1.5 s, and a local issuer's document is read. Each new check fails.
  • Bugs planted in this change: with the local-key path disabled, plcLocalIssuer fails. With the shutdown write disabled, the restart check fails.
  • With the change: the whole spaces run passes (spaces/compare.mjs --seed 1), and so do the limits checks plcLocalIssuer, plcDocuments, plcConnections, spacesPayFirst and didWebApart.
  • Cargo: cargo test -p pds-server --lib -- space_credentials (21 tests) and space_notify (22 tests) pass, as reported by the server agent. Clippy and fmt are clean.

Shortcomings

  • A key rotated away from is accepted for up to PDS_DID_CACHE_MAX_SECS (a day) for an issuer not in use, where it was an hour before. A rotation to a key is still taken at once.
  • Where the server agent read the spec (the spec does not say):
    • a deleted account is no "account here", while a deactivated or taken-down one still is;
    • the keys are also written on SIGINT;
    • when the file names a DID twice, the first, newer key wins.
  • Rules: the server agent cut the old key code out of jwt.rs with a script rather than the edit tool. The result is a plain deletion.

Review

Start at verify_issuer and held in crates/pds-server/src/space_credentials/issuers.rs, then Kept.

Approved

This pull verifies the server's own accounts' spaces tokens against the key the server already holds. It keeps other issuers' keys fresh for PDS_DID_CACHE_SECS and usable up to the new PDS_DID_CACHE_MAX_SECS, with renewal in the background. It bounds the cache at one key per 1 KiB of PDS_CACHE_BYTES, dropping the key used longest ago, and persists the keys to issuer-keys. I read issuers.rs in full, plus the changes in lib.rs, ops.rs, cache.rs, space_credentials.rs, the changeset, and LIMITS.md and STORE.md. I did not read these files: jwt.rs, tests.rs, limits.mjs, compare.mjs, issuers.mjs, SPACES-CREDENTIALS.md. I did not run any checks; the verdict rests on the code and the pull's account of its own checks.

  • Logic. The LRU list, the failed-key handling (a stale key that no longer verifies falls through to a fresh read), the per-issuer turns, and the checksummed temp-file-then-rename write all look correct.
  • Repository requirements. The spec and LIMITS.md are updated, STORE.md counts the keys in its bounds, a changeset is present, and no atproto or MCP crates were added. I found no mention of PDS_DID_CACHE_SECS in docs/guide, so I see no guide to update.
  • Remaining risk. A key rotated away from is now accepted for up to a day for an idle issuer. The pull's body lists this as a shortcoming, and the spec states it, so I accept it.
All checks have passed

4 passed, 1 skipped

Merged as cfbc1d2