Repository navigation
Conversation
…session When a user was already loaded in the current session by another lookup, for example by email on a login, getUserByUsername and getUserByEmail returned the managed user before the lookup query was stored. The query for the other key was therefore never cached and every following request paid one lookup in the user store, e.g. the username lookup of the user profile validation on each login with VERIFY_PROFILE enabled. The early return dates back to the user cache policy change, which moved the query caching below the adapter creation to skip uncached users. Cache the query on the managed path as well, under the same conditions as before: only for cached UserAdapters, with the same revision and lifespan, and only if the managed user still matches the lookup key. Closes keycloak#54645 Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Niko Köbler <niko@n-k.de>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change to avoid repeated username and email lookups during warm logins.
No open issues 🎉
What changed in this PR
Fixes #54645 by caching username and email lookup queries when the user is already managed in the session, avoiding repeated user-store lookups during warm logins.
Changes:
- Adds shared query-caching logic with lookup-key matching and cache-policy safeguards.
- Adds regression tests for both lookup orders, uncached users, invalidation, and local users.
- Introduces and registers a counting user storage provider for the tests.
| File | Description |
|---|---|
tests/custom-providers/src/main/resources/META-INF/services/org.keycloak.storage.UserStorageProviderFactory |
Registers the counting provider factory. |
tests/custom-providers/src/main/java/org/keycloak/tests/providers/federation/CountingUserStorageProviderFactory.java |
Creates counting provider instances. |
tests/custom-providers/src/main/java/org/keycloak/tests/providers/federation/CountingUserStorageProvider.java |
Supplies in-memory users and tracks lookup calls. |
tests/base/src/test/java/org/keycloak/tests/model/UserCacheQueryTest.java |
Tests query caching and exclusion behavior across sessions. |
model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/UserCacheSession.java |
Caches matching username/email queries on the managed-user path. |
Review effort: Balanced
sguilhen
approved these changes
Nov 16, 2026
sguilhen
left a comment
Contributor
There was a problem hiding this comment.
The proposed fix looks correct: cacheUserQuery reuses the same UserAdapter/lifespan gating as the existing non-managed path, and the added realm-id + username/email match guards prevent caching a stale query if the managed user no longer matches the lookup key (e.g. after a rename).
Looks good to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #54645
UserCacheSession.getUserByUsername()andgetUserByEmail()returned the already managed user before storing the lookup query, so a user loaded first by the other lookup never got the second query cached. On a warm email login withVERIFY_PROFILEenabled (default),DuplicateUsernameValidatorthen caused onegetUserByUsernamecall to the user store on every login.This restores the behaviour from before the 2016 user cache policy change: the query is cached on the managed path too, under the same conditions as the normal path (only for cached
UserAdapters, same revision and lifespan), and only if the managed user still matches the lookup key. Invalidated users,NO_CACHE/uncached users and realm invalidations keep their current behaviour.getUserByFederatedIdentityandgetServiceAccounthave the same early return. I left them out to keep this PR small.Tests: new
UserCacheQueryTestwith a counting user storage provider. It fails without the fix and passes with it. The existing model, user storage, LDAP, organization-cache and admin-user tests intests/basestill pass, as doUserStorageTest,UserStorageFailureTestandLDAPExternalChangesTestagainst Quarkus.Docs: no documentation change; this is an internal caching fix with no user-visible configuration or behaviour change.
Generative AI disclosure
Claude Code helped with the history analysis, the implementation, the tests and this description. I reviewed and understand the change and can explain and revise it.