Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultCache<T> should use a keyed lock instead of a global lock for each T #1857

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

maartenba
Copy link
Member

Debugging what seemed like a race condition resulted in discovering DefaultCache<T> locks globally instead of per cache key. Especially in the client stores this is an issue, as retrieving and trying to cache clientA results in requests for clientB having to wait for the lock to become available.

In addition ,we found that some dependencies were registered as transient where a longer lifetime makes more sense.

This PR serves as a discussion:

  • Is locking per cache key better than locking the cache altogether?
  • Do we want to expand lifetimes where we changed them in this PR?

@maartenba
Copy link
Member Author

Added test for the new implementation. It succeeds (yay!), and if you run it against the previous implementation it hangs for a while and then crashes with the cache lock exception.

@maartenba maartenba added area/products/identity-server Related to Identity Server impact/non-breaking The fix or change is not a breaking one labels Mar 12, 2025
@damianh damianh removed their request for review March 13, 2025 10:10
Copy link
Member

@josephdecock josephdecock left a comment

Choose a reason for hiding this comment

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

Thanks Maarten! This is an interesting idea, and warrants further investigation. There's some other thoughts going around about experimenting with HybridCache as well.

If we take this approach, it would be a breaking change for customizations that derived from DefaultCache, so it would be something we do in a major release. Merging now would put it into the 7.3 release, so I'm marking this as "request changes", just in case.

@josephdecock
Copy link
Member

* Is locking per cache key better than locking the cache altogether?

Probably. We do already have some granularity in the locks from the type parameter. More granularity is probably helpful especially if there are many clients. We should measure.

* Do we want to expand lifetimes where we changed them in this PR?

This looks safe to me. In general, lifetime changes can cause subtle breaking changes, so good to be careful with that.

@maartenba
Copy link
Member Author

Thanks! Lets revisit during 7.3

@josephdecock josephdecock added impact/breaking The fix or change will be a breaking one and removed impact/non-breaking The fix or change is not a breaking one labels Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/products/identity-server Related to Identity Server impact/breaking The fix or change will be a breaking one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants