Skip to content

fix: remove redundant removeTransientAdminRoles calls in addRolesAsAttributes to fix O(n) performance regression with many realms - #51569

Open
waterWang wants to merge 1 commit into
keycloak:mainfrom
waterWang:fix/keycloak-51554-admin-api-performance-regression
Open

fix: remove redundant removeTransientAdminRoles calls in addRolesAsAttributes to fix O(n) performance regression with many realms#51569
waterWang wants to merge 1 commit into
keycloak:mainfrom
waterWang:fix/keycloak-51554-admin-api-performance-regression

Conversation

@waterWang

Copy link
Copy Markdown

Description

Closes #51554

Root Cause

The addRolesAsAttributes method in KeycloakIdentity.java calls removeTransientAdminRoles for every role in the access token, on every request. When lightweight admin tokens are expanded to include roles across all realms, this results in O(n) database lookups per request where n = number of realms.

With 400 realms, each admin API request takes 9–19 seconds (vs. 5–24ms without the redundant calls), as measured by the reporter.

Fix

Remove the two removeTransientAdminRoles calls from addRolesAsAttributes. The admin role validation is already performed at token creation time by AdminRoleTokenPostProcessor.process() — the roles are stripped of transient entries before the token reaches the client. Re-validating at request time is redundant and causes the O(n) performance regression.

Performance Impact

Realms Before After Ratio
10 7 ms 5 ms 1x
50 40 ms 6 ms 7x
100 141 ms 6 ms 24x
200 551 ms 11 ms 50x
250 3628 ms 15 ms 242x
300 11367 ms 18 ms 632x
350 19157 ms 24 ms 798x

Security

Safe to remove because AdminRoleTokenPostProcessor.process() already validates transient admin roles during token creation. The addRolesAsAttributes method is only populating attributes for the authorization context from an already-validated token.

Related

…tributes to fix O(n) performance regression with many realms (keycloak#51554)
Copilot AI balanced review requested due to automatic review settings August 10, 2026 06:23
@waterWang
waterWang requested a review from a team as a code owner August 10, 2026 06:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Removes redundant per-request admin-role validation to eliminate realm-count-dependent database lookups.

Changes:

  • Removes transient admin-role filtering from authorization attribute population.
  • Removes the unused static import.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 324 to 325
if (realmAccess != null) {
removeTransientAdminRoles(realm, null, user, realmAccess);
attributes.put("kc.realm.roles", realmAccess.getRoles());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin API per-request cost grows super-linearly with realm count since 26.7.1

2 participants