Skip to content

[MemoryCache] Add possibility to disable linked cache entries #45592

Closed

Description

@adamsitnik

Background and Motivation

MemoryCache by default tracks linked cache entries to allow for propagating options.

In the following example, the expirationToken added for child is also applied to parent:

using (var parent = cache.CreateEntry(key))
{
    parent.SetValue(obj);

    using (var child = cache.CreateEntry(key1))
    {
        child.SetValue(obj);
        child.AddExpirationToken(expirationToken);
    }
}

Tracking internally uses AsyncLocal<T>, which is expensive and adds non-trivial overhead. The cost of using it has popped out in the performance reports that we have received from our customers (#45436).

Currently, this feature can not be disabled.

Proposed API

As suggested by @Tratcher in #45436 (comment), we can extend the existing MemoryCacheOptions with a flag that disables this behaviour.

Naming is hard and the best idea I currently have is TrackLinkedCacheEntries:

namespace Microsoft.Extensions.Caching.Memory
{
    public class MemoryCacheOptions
    {
+        public bool TrackLinkedCacheEntries { get ; init; } = true;
    }
}

I am open to suggestions.

Usage Examples

var options = new MemoryCacheOptions
{
    TrackLinkedCacheEntries = false
};

var cache = new MemoryCache(options);

Alternative Designs

As an alternative, we could add a new method to MemoryCache that would allow for creating cache entries without tracking:

namespace Microsoft.Extensions.Caching.Memory
{
    public class MemoryCache
    {
        public ICacheEntry CreateEntry(object key); // existing method
+        public ICacheEntry CreateUnlinkedEntry(object key);
    }

But there is a lot of existing extension methods that allow for adding new cache entries to the cache and we would most probably need to add new overloads for them...

Risks

Introducing this API has no risks as long as we don't change the default settings (enabled by default).

cc @eerhardt @maryamariyan @davidfowl

Activity

  1. added
    api-suggestion
    tenet-performance
    on Dec 4, 2020
  2. added this to the 6.0.0 milestone on Dec 4, 2020
  3. self-assigned this
    on Dec 4, 2020
  4. added
    untriaged
    on Dec 4, 2020
  5. Tratcher commented on Dec 7, 2020

    @Tratcher
    Member

    Note @davidfowl asked for this to be disabled by default and allow the few people that actually need the feature to opt-in with the new setting. Not sure if that meets the compat bar.

  6. NickCraver commented on Dec 9, 2020

    @NickCraver
    Member

    For what it's worth, at scale we found a lot of the weight of our cache (somewhere in the 30-40% range) was overhead of the CacheEntry object itself. The per-entry storage on CacheEntry currently is:

            // static
            private static readonly Action<object> ExpirationCallback = ExpirationTokensExpired;
    
            // per entry
            private readonly object _lock = new object();
            private readonly MemoryCache _cache;
            private IList<IDisposable> _expirationTokenRegistrations;
            private IList<PostEvictionCallbackRegistration> _postEvictionCallbacks;
            private IList<IChangeToken> _expirationTokens;
            private TimeSpan? _absoluteExpirationRelativeToNow;
            private TimeSpan? _slidingExpiration;
            private long? _size;
            private CacheEntry _previous;
            private object _value;
            private int _state;
            public DateTimeOffset? AbsoluteExpiration { get; set; }

    In our slimmed down version removing a lot of the callback weight we weren't using anywhere (in the vein of this issue), we narrowed down to:

            // static
            private static long s_currentDateIshTicks = DateTime.UtcNow.Ticks;
            private static readonly Timer ExpirationTimeUpdater = new Timer(state => s_currentDateIshTicks = DateTime.UtcNow.Ticks, null, 1000, 1000);
    
            // per entry
            private long _absoluteExpirationTicks;
            private int _accessCount;
            private readonly uint _slidingSeconds;
            public object Value { get; }

    Note: we actually added _accessCount here for evaluation of unused cache, which was a valuable feature for seeing where lot of unused waste was. Happy to share code here on all those bits (cc @mgravell) - it's something we found very valuable to have a dashboard for, in the running application, to see where we can easily slim down further.

    When you're storing a lot of things like integers or bools or any small data type, the weight of the reference overhead is quite large. What is the thinking on how we can reduce that? Note that most of it is inherent to the ICacheEntry interface and so a break means a much larger change...which is the route we ended up taking. Happy to help however we can here, and appreciate the kick on this issue!

  7. adamsitnik commented on Dec 9, 2020

    @adamsitnik
    MemberAuthor

    @NickCraver thanks for providing very valuable feedback! I've already removed 4 fields from CacheEntry (#45410) but it looks like we still have work to do here. I'll try to come up with more ideas

  8. adamsitnik commented on Dec 11, 2020

    @adamsitnik
    MemberAuthor

    For what it's worth, at scale we found a lot of the weight of our cache (somewhere in the 30-40% range) was overhead of the CacheEntry object itsel

    @NickCraver I've sent #45962 to minimize the overhead (280 -> 224 bytes). I'll try to wrap my head if it would be possible to replace the nullable date-related fields with a single struct. It would be much easier if I could introduce breaking changes ;)

  9. removed this from the 6.0.0 milestone on Feb 3, 2021
  10. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Mar 4, 2021
  11. added this to the Future milestone on Mar 4, 2021
  12. modified the milestones: Future, 7.0.0 on Jul 22, 2021
  13. bartonjs commented on Aug 10, 2021

    @bartonjs
    Member

    Since this is proposing a breaking change (making the default be non-tracked), going with one centralized option seems the most sensible.
    Adding an overload of MemoryCache.CreateEntry(object key, bool linked) (or something like that) can be done in the future if entry-level support feels warranted.

    namespace Microsoft.Extensions.Caching.Memory
    {
        partial class MemoryCacheOptions
        {
            public bool TrackLinkedCacheEntries { get ; set; } = false;
        }
    }
  14. added
    api-approved
    and removed
    api-ready-for-review
    on Aug 10, 2021
  15. ghost added
    in-pr
    on Aug 18, 2021
  16. ghost removed
    in-pr
    on Sep 14, 2021
  17. ghost locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-Extensions-Cachingtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions