8000 Shorter lifespan for offline session cache entries in memory by martin-kanis · Pull Request #26811 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Shorter lifespan for offline session cache entries in memory - #26811

Merged
ahus1 merged 1 commit into
keycloak:mainfrom
martin-kanis:24302
Feb 9, 2024
Merged

ahus1 merged 1 commit into
keycloak:mainfrom
martin-kanis:24302

Conversation

@martin-kanis
@martin-kanis martin-kanis commented Feb 6, 2024 •
Copy link
Copy Markdown
Member

Closes #26810

This is a new PR instead of #24302 where I added a model test and rebased the code. I also added documentation.

@pedroigor
Copy link
Copy Markdown
Contributor

@martin-kanis Is it basically about providing a server-wide setting to avoid setting the lifespan on per realm-basis?

@sschu
sschu commented Feb 6, 2024
Copy link
Copy Markdown
Contributor

@pedroigor I would assume this is to have some way to evict data from the cache, especially for offline sessions that have no max session length. Since we have lazy loading now, it is definitely an improvement.
@martin-kanis I am wondering why we do not offer to limit the size of caches instead of adding an expiration? This would give much more predictability when it comes to Keycloak's memory consumption (I understand this is the main goal of this issue, to reduce memory consumption).

@pedroigor
Copy link
Copy Markdown
Contributor

@pedroigor I would assume this is to have some way to evict data from the cache, especially for offline sessions that have no max session length. Since we have lazy loading now, it is definitely an improvement.

Yeah, but does it make sense to include yet another server-level setting instead of just setting a lifespan to offline sessions in your realm? The ultimate goal is the same, no?

Also, as you mentioned, why not just set a limit to the corresponding caches in the cache configuration?

@sschu
sschu commented Feb 7, 2024
Copy link
Copy Markdown
Contributor

@pedroigor There is a difference from the offline session settings int he realm: offline sessions that are evicted from the cache might still be valid and will be lazy-loaded from the database into the cache.

@martin-kanis
Copy link
Copy Markdown
Member Author

@sschu We picked the lifespan override as safer variant that will still safe some memory but removes a risk that you set fixed size for a cache, then you have many parallel offline token refreshes and a session got evicted from the cache in the middle of a request, just after it got imported from the DB. Infinispan uses tiny LFU algorithm (modification of least frequently used) for eviction and we are not fully sure how it would behave.

We still might add and evaluate the cache size approach for future. We hope users will start experimenting with this lifespan override option and provide a feedback how it works or doesn't work for them.

@martin-kanis
martin-kanis requested review from ahus1 and mhajas February 7, 2024 14:56
@sschu
sschu commented Feb 7, 2024
Copy link
Copy Markdown
Contributor

@martin-kanis OK, I think I get the point. It is probably the safer approach. But I would say the problem you described isn't very likely to happen as long as you have reasonable cache sizes > 10.000? You need to have really high load to have problems with parallel requests evicting each others' entries. Couldn't we also just document that we recommend a certain minimum size depending on the expected load?

@thomasdarimont
thomasdarimont commented Feb 7, 2024 •
Copy link
Copy Markdown
Contributor

I'm really happy to see this eventually landing in main :)

As far as I understand the implementation the new lifespan overrides for in memory items only affect cache items that are created (loaded-into memory) by Keycloak instances with that setting. So perhaps it would make sense to add a small note that a cluster restart might be needed to apply the new lifespans to all (offline)) cache entries.

Another option would be to adjust the timestamps on the fly when an offline session is accessed and an "larger" lifespan is detected.

@sschu
sschu commented Feb 8, 2024
Copy link
Copy Markdown
Contributor

@thomasdarimont This is a KC24 feature. When you roll out KC24, won't you restart all nodes anyways?

@thomasdarimont
Copy link
Copy Markdown
Contributor

@sschu it depends on whether an external infinispan is used and whether cached sessions from (<KC24) are compatible with KC24. Some folks might cycle their Keycloak cluster but perhaps not the infinispan cluster.

@sschu
sschu commented Feb 8, 2024
Copy link
Copy Markdown
Contributor

You are right. I didn't check the implementation. I thought the expiration was added on a cache level on the embedded cache, but it looks like it is added when adding individual session entries...

@thomasdarimont
Copy link
Copy Markdown
Contributor

Would it be possible to backport this to 23.0.x / 22.0.x?

@ahus1
ahus1 commented 8000 Feb 9, 2024
Copy link
Copy Markdown
Member

You need to have really high load to have problems with parallel requests evicting each others' entries. Couldn't we also just document that we recommend a certain minimum size depending on the expected load?

@sschu - Infinispan uses "least frequently used" to expire entries, which I find hard to reason about in this scenario. Therefore, we'll stick with the expiry option.

@ahus1
ahus1 commented Feb 9, 2024
Copy link
Copy Markdown
Member

Would it be possible to backport this to 23.0.x / 22.0.x?

We're considering a backport. We had backporting in mind when we planned for this to keep the changes as minimal as possible.

@sschu
sschu commented Feb 9, 2024
Copy link
Copy Markdown
Contributor

You need to have really high load to have problems with parallel requests evicting each others' entries. Couldn't we also just document that we recommend a certain minimum size depending on the expected load?

@sschu - Infinispan uses "least frequently used" to expire entries, which I find hard to reason about in this scenario. Therefore, we'll stick with the expiry option.

Oh, OK. I quite naturally assumed this to be least-recently-used.

@ahus1
ahus1 commented Feb 9, 2024 •
Copy link
Copy Markdown
Member

Another note on people using remote caches: There is RemoteCacheSessionsLoader which will load for all distributed caches the entries from the remote cache into the local cache. When doing this, it will not honor the lifespan set in the remote cache, and it will (as I understand it) use the default lifespan configured in the Infinispan configuration.

Therefore, for those who are using this in an environment with a remote store, I recommend to set the lifespan for all distributed caches in their Infinispan XML file to a value that matches their environment. I'll open a follow-up issue for this to document this in the HA guide. We'll still need to analyze where that loading is really necessary (we know it is necessary for online user sessions). #26941

Closes keycloak#26810

Co-authored-by: Thomas Darimont <thomas.darimont@googlemail.com>
Co-authored-by: Martin Kanis <mkanis@redhat.com>

Signed-off-by: Thomas Darimont <thomas.darimont@googlemail.com>
Signed-off-by: Martin Kanis <mkanis@redhat.com>
@ahus1 ahus1 left a comment
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.

Thank you for this change, @thomasdarimont, and @martin-kanis for documenting and testing it!

@cypress
cypress Bot commented Feb 10, 2024 •
Copy link
Copy Markdown

1 flaky test on run #10984 ↗︎

0 1112 106 0 Flakiness 1

Details:

Shorter lifespan for offline session cache entries in memory
Project: Keycloak Admin UI Commit: 93fc6a6c54
Status: Passed Duration: 08:25 💡
Started: Feb 12, 2024 12:21 AM Ended: Feb 12, 2024 12:30 AM
Flakiness  cypress/e2e/partial_export_test.spec.ts • 1 flaky test • firefox

View Output

Test Artifacts
Partial realm export > Exports the realm Screenshots

Review all test suite changes for PR #26811 ↗︎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shorter lifespan for offline session cache entries in memory

5 participants

0