Skip to content

perf(ratelimit): Skip org lookup RPC for sentry app tokens - #126661

Merged
scttcper merged 4 commits into
masterfrom
scttcper/ratelimit-replica-org
Oct 8, 2026
Merged

scttcper merged 4 commits into
masterfrom
scttcper/ratelimit-replica-org

Conversation

@scttcper

@scttcper scttcper commented Oct 6, 2026

Copy link
Copy Markdown
Member

Rate limiting sentry app requests makes an RPC to control (get_installation_org_id_by_token_id) just to get the installation's org. The token on the request already carries it: the cell's ApiTokenReplica.organization_id (#58490) is re-replicated when installations change. This uses that and only falls back to the RPC when it's missing.

Follows #76068 and #76258, which narrowed this RPC to cut latency. #65403 assumed the org couldn't be read off the token in every silo, but AuthenticatedToken copies it from both the replica and ApiToken.

That RPC runs ~935k times an hour, averaging 47ms (~150ms from DE). Example trace

Rate limiting sentry app requests made an RPC to control just to get the
installation's org. The token on the request already carries it, the
cell's ApiTokenReplica org is re-replicated when installations change.
Only fall back to the RPC when it's missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 6, 2026
@scttcper
scttcper marked this pull request as ready for review October 6, 2026 21:51
@scttcper
scttcper requested a review from a team as a code owner October 6, 2026 21:51
ApiToken.organization_id is a property that queries control silo tables,
so reading it in a cell blew up and the middleware failed open. Only read
the org off AuthenticatedToken and ApiTokenReplica, raw ApiTokens keep
using the RPC.

Adds back a test that asserts sentry app tokens skip the RPC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/sentry/ratelimits/utils.py Outdated
Comment on lines +97 to +98
# Sentry app tokens carry their installation's organization. ApiToken computes it
# with control silo queries, so it's left to the RPC fallback below.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this comment? The logic flow below explains the purpose well enough.

Comment thread src/sentry/ratelimits/utils.py Resolved
@scttcper
scttcper enabled auto-merge (squash) October 8, 2026 18:52
@scttcper
scttcper merged commit 8a5fff1 into master Oct 8, 2026
73 checks passed
@scttcper
scttcper deleted the scttcper/ratelimit-replica-org branch October 8, 2026 19:03

This branch was successfully deployed

1 active deployment
Preview — 2b7f9063 Deployed Oct 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants