Skip to content

perf(auth): the latest audit checkpoint's signatures are checked once per new checkpoint, not on every request - #430

Merged
PiquelChips merged 3 commits into
mainfrom
perf/audit-latest-cache
Nov 8, 2026
Merged

PiquelChips merged 3 commits into
mainfrom
perf/audit-latest-cache

Conversation

@PiquelChips

Copy link
Copy Markdown
Member

/audit/checkpoints/latest checked Ed25519 signatures on every request. It looks for the newest checkpoint the keys verify, and may pass over up to 1,000 forged ones that someone with auth's database role inserted. A public endpoint shouldn't repeat that work for every visitor. Now each replica remembers what it found, and searches again only when another checkpoint has been stored where it could change the answer. Every request still sees the current answer, at the cost of one small query.

What changes

  • The cache: AuditApplication keeps the last note it found, with two numbers:
    • floor: the lowest position the search read. That's the trusted checkpoint's, the oldest untrusted one if it stopped at its limit of 1,000, or 0 if it read every checkpoint.
    • how many checkpoints were stored at or after floor.
  • Why a count: stored checkpoints can't be changed or deleted, so only a new one at or above the floor can change the answer. It's a count, not the newest position, because someone with auth's database role can hold the top position with a forged checkpoint while a genuine one below it is still being signed. Keyed on the top position, the cache would never see the genuine one.
  • A request: it counts checkpoints at or after the floor. That's a few index entries, and it holds no lock. When the count changed, one request searches again, under an async lock, and requests waiting on it reuse its answer. The search and its count read one snapshot, so a checkpoint stored during the search can't make the cache stale.
  • Supporting code: AuditRepository::count_checkpoints(from) and TrustedCheckpoint::floor. audit-history.md says when signatures are checked again.

Deploying and rolling back

No migration. Each replica starts with an empty cache. Rolling back restores the search on every request.

Proof

  • the_latest_checkpoint_is_verified_once_per_checkpoint, on one replica:
    • A forged checkpoint at 8 is passed over.
    • A genuine checkpoint stored at 7, below it, is then served. The first version of this branch, keyed on the top position, fails here.
    • Tampering 7's note, with no checkpoint stored since, doesn't change what the replica serves, which proves it doesn't search again. A fresh replica finds 6.
  • Audit and database tests: 98 of 98, with the query cache check, clippy and fmt.

Reviewed by GPT 6.1 Sol, which found the top-position keying stale (blocking) and repeated searches under concurrent misses; both are fixed. Then re-checked by Claude Opus 5.5 while Sol was rate-limited. It confirmed both fixes, and measured an unbounded count(*) at 7–8 ms after a year of checkpoints, held under the lock. That led to the bounded count and the lock-free hit path.

Note

Written by Claude Opus 5.5 in Claude Code and reviewed by GPT 6.1 Sol and Claude Opus 5.5 (via T3 Code)

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 890789b7-4a45-494f-bcc6-4fc8087087eb
Commits

Reviewing files that changed from the base of the PR and between e730934 and d028e11.

Files selected for processing: 6

  • apps/auth-service/src/audit/routes.rs
  • apps/auth-service/src/audit/tests.rs
  • apps/auth-service/src/audit/verify.rs
  • crates/database/.sqlx/query-f698134e33daba0560c63590843ab964b14d779d33dd1b16e626cd6bb9b7c113.json
  • crates/database/src/repositories/audit/mod.rs
  • docs/dev/security/audit-history.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@PiquelChips
PiquelChips force-pushed the perf/audit-latest-cache branch from 4fd06fb to 7e0a664 Compare November 7, 2026 12:28
@PiquelChips PiquelChips self-assigned this Nov 8, 2026
@PiquelChips PiquelChips added the performance Performance Improvements label Nov 8, 2026
@PiquelChips PiquelChips added this to the v0.1 milestone Nov 8, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in sourceorigin.dev Nov 8, 2026
@PiquelChips
PiquelChips force-pushed the perf/audit-latest-cache branch from 7e0a664 to d028e11 Compare November 8, 2026 10:40
@PiquelChips
PiquelChips added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit 53475b9 Nov 8, 2026
10 checks passed
@PiquelChips
PiquelChips deleted the perf/audit-latest-cache branch November 8, 2026 11:08
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in sourceorigin.dev Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance Improvements

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant