8000 Avoid having N+1 database calls when fetching composite roles by ahus1 · Pull Request #46622 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Avoid having N+1 database calls when fetching composite roles - #46622

Merged
ahus1 merged 3 commits into
keycloak:mainfrom
ahus1:is-46605-fix-n-plus-one-fetch-and-repeated-fetch
Feb 26, 2026
Merged

ahus1 merged 3 commits into
keycloak:mainfrom
ahus1:is-46605-fix-n-plus-one-fetch-and-repeated-fetch

Conversation

@ahus1
@ahus1 ahus1 commented Feb 26, 2026
Copy link
Copy Markdown
Member

Closes #46605

Closes keycloak#46605

Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
if (clientModel != null) {
RoleModel adminRole = adminRealm.getRole(AdminRoles.ADMIN);
if (adminRole.getCompositesStream().noneMatch(r -> (r.isClientRole() && r.getContainerId().equals(clientModel.getId())))) {
if (containers == null) {
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.

This should limit the potential reloading of admin composite roles due to newly cached realms to just once per session.

Newly cached realms are caused either by a realm being added, or because an existing realm was evicted from cache. If I'm understanding correctly all resources in the realm cache are being treated equally - that is loading a lot of entries, such as composite roles, will cause the eviction of realms and the admin role. Other than adjusting the realm cache size, do we have some additional controls over cache tiers or weighting for eviction by type (it seems realms are substancially more heavy-weight than most entries)?

Without adjustments to what is being evicted, the other approach of simply invalidating the admin role when a newly cached realm is created may be better than this in practice (at least for non-admin heavy workloads) because if realms are routinely being evicted, then so is the master admin role.

Alternatively is there any way to create a cache entry for the admin role that cannot be evicted? That would address the performance issue here and reduce the overhead with frequent admin requests once the cache is thrashing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Infinispan will cache evict the "least frequently used" item, so this should help a bit.

Still, I'll rewrite the code to always invalidate the admin role.

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.

Infinispan will cache evict the "least frequently used" item, so this should help a bit.

Is it worth a follow-up about tuning this further?

Another possible followup - even with light-weight tokens it seems like creating the server side identity for someone with the master admin role is expensive (at least in terms of cache entires) when there are a lot of realms. Should we consider partitioning the load of composites by realm, rather than materializing all of them for light-weight tokens? Or do we already caution against using someone with the master admin role to perform admin operations in this scenario?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it worth a follow-up about tuning this further?

IMHO no.

For light-weight tokens it seems like creating the server side identity for someone with the master admin role is expensive

It is very expensive as it will evaluate all roles by iterating through them recursively. There were some ideas to maybe implement a recursive query in the past, but we did never implement that.

See https://vladmihalcea.com/hibernate-with-recursive-query/ for more information, and #46653 for a follow-up issue.

Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
shawkins
shawkins previously approved these changes Feb 26, 2026
@shawkins shawkins 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.

Looks good, just one nit and some questions about follow-ups.

if (clientModel != null) {
RoleModel adminRole = adminRealm.getRole(AdminRoles.ADMIN);
if (adminRole.getCompositesStream().noneMatch(r -> (r.isClientRole() && r.getContainerId().equals(clientModel.getId())))) {
if (containers == null) {
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.

Infinispan will cache evict the "least frequently used" item, so this should help a bit.

Is it worth a follow-up about tuning this further?

Another possible followup - even with light-weight tokens it seems like creating the server side identity for someone with the master admin role is expensive (at least in terms of cache entires) when there are a lot of realms. Should we consider partitioning the load of composites by realm, rather than materializing all of them for light-weight tokens? Or do we already caution against using someone with the master admin role to perform admin operations in this scenario?

…ispan/RealmCacheSession.java

Co-authored-by: Steven Hawkins <shawkins@redhat.com>
Signed-off-by: Alexander Schwartz <alexander.schwartz@gmx.net>
@ahus1
ahus1 enabled auto-merge (squash) February 26, 2026 14:19
@ahus1
ahus1 requested a review from shawkins February 26, 2026 14:19
@ahus1
ahus1 merged commit f2cfe15 into keycloak:main Feb 26, 2026
88 of 91 checks passed
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.

26.5.4 startup regression with many realms: RealmCacheSession.prepareCachedRealm() scans master admin role composites per realm (O(N²))

2 participants

0