Repository navigation
Use shorter lifetimes for offline session cache entries in memory #24274
thomasdarimont
started this conversation in
Archive
Replies: 2 comments 3 replies
|
A PoC implementation of this can be found here: #24302 The current implementation uses server wie configuration, but this could also be supported on realm level, with the following precedence:
The offline session cache entry lifetimes can be configured via the following properties: Test Script: Example session: |
3 replies
|
Note that some of this recommendations have been implemented in the meantime via #26998 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Ideas for Optimizing Offline Session handling
Memory consumption
Offline Sessions are held in memory for too long
Problem
Description of the problem
When using many offline sessions, a customer of mine had enormous problems with high memory requirements (sometimes several 10GB). The machines regularly ran out of memory, and GC efforts consumed a lot of CPU resources.
A few resourceful software developers had an IMHO simple and clever idea: keep offline session cache entries shorter in memory than in the database.
Scenario
Example scenario in which the problem arises
Keycloak cluster has about 25 million users with about 10 million offline sessions for a mobile app.
Idea/Solution
Description of the proposed solution / idea
Since Keycloak can lazy load offline sessions from the database, Keycloak does not have to keep all loaded offline sessions permanently in memory but can limit the TTL of the cached entries in memory to a value of ~12h - 24 h.
The reasoning is that power users who use the app regularly perform a token refresh (with the offline refresh token) often enough so that the offline session remains in memory. Offline sessions of users who use the app only occasionally do not have to be permanently in memory and can be automatically removed from memory after 12h-24h. If such an occasional user returns, his offline session is reloaded "lazily" from the database at the cost of increased latency for database access.
In the database, the offline session entries are still stored according to their "Offline Session IDLE" or "Offline Session Max" for longer.
Approach for implementation
How could the proposed solution be implemented?
When OfflineSession / OfflineClientSessions entries are stored in Infinispan, the TTL is currently set to -1 with a max idle value. Instead, one could introduce a new server configuration property like "offlineSessionCacheLifespanOverride" (int in millis) and thus (if specified) determine the (shorter) TTL time, which could be configured on the InfinispanUserSessionProviderFactory.
The configuration property could then be used to override the lifespan of the cache entries.
This will then keep the offline session Infinispan cache entry in memory for a (much) shorter time and remove it automatically after the TTL has expired. Of course, the offline session cache entry remains stored longer in the database.
Effort / Complexity
_How much effort must be put into this to make it work? _
Effort: Low
Complexity: Low
Migration
How to migrate existing offline sessions, if necessary
Existing offline session cache entries that still have the long TTLs, could be adjusted on next access and configured with the shorter TTL such that the system "heals" itself and releases more memory over-time.
Expected Results
Expected Results from this optimization, potentially based on PoCs and load-tests)
In the project, a customer tried this and experienced significantly reduced memory consumption (during a longer-running load test) and improved system stability.
All reactions