Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Improve performance of MemoryCache by using ConcurrentDictionary rather than ReaderWriterLockSlim #242

Closed

Description

@mikeharder

It may be possible to significantly improve the performance of MemoryCache by using ConcurrentDictionary rather than Dictionary protected by a ReaderWriterLockSlim. Under high load, there is contention acquiring the read lock from ReaderWriterLockSlim.

I created a quick prototype which shows significantly improved performance, though it may not be functionally correct in all scenarios.

Scenario MemoryCache Implementation RPS CPU
Plaintext N/A 1,167,369 100%
MemoryCachePlaintext ReaderWriterLockSlim 342,129 60%
MemoryCachePlaintext ConcurrentDictionary 802,076 100%

The remaining work is to improve and test the ConcurrentDictionary implementation to ensure it's correct under high load, has no race conditions, etc.

Prototype: https://github.com/aspnet/Caching/tree/mikeharder/concurrent-dictionary

Activity

  1. self-assigned this
    on Oct 27, 2016
  2. added this to the 1.1.0 milestone on Nov 3, 2016
  3. modified the milestones: , 1.1.0 on Nov 3, 2016
  4. Dissimilis commented on Nov 16, 2016

    @Dissimilis

    Please thoroughly test ConcurrentDictionary vs lock'ed Dictionary performance and don't make this change blindly. Performance may depend a lot on specific workload (amount of get vs set, etc.), even simple Monitor may be faster than ReaderWriterLockSlim if most operations are writes.

  5. JunTaoLuo commented on Nov 17, 2016

    @JunTaoLuo
    Contributor

    @Dissimilis thanks for the feedback but is there any particular workload you are concerned about? A few scenarios we have benchmarked include 1) concurrent gets with a few set to update the entry and 2) concurrent sets and removes and have seen major improvements in both. See the referenced PR for benchmark results. The move toward ConcurrentDictionary also takes advantage of the more fine-grained locking and will significantly improve performance when concurrently accessing different entries in the cache compared to the previous implementation which uses a single ReaderWriterLockSlim across the entire dictionary.

  6. sebastienros commented on Nov 17, 2016

    @sebastienros
    Member

    Can we update the table with the new numbers?

  7. JunTaoLuo commented on Nov 17, 2016

    @JunTaoLuo
    Contributor
    Scenario previous CPU previous RPS CPU RPS
    benchmarks: memorycacheplaintext 60 342,129 100 886,544
    benchmarks: memorycacheplaintextsetremove - - 100 689,634
  8. modified the milestones: , 2.0.0 on Jan 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions