Uploaded image for project: 'Lucene - Core'
  1. Lucene - Core
  2. LUCENE-9038

Improve LruQueryCache's concurrency



    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
    • Lucene Fields:


      LRUQueryCache appears to play a central role in Lucene's performance. There are many issues discussing its performance, such as LUCENE-7235, LUCENE-7237, LUCENE-8027, LUCENE-8213, and LUCENE-9002. It appears that the cache's overhead can be just as much of a benefit as a liability, causing various workarounds and complexity.

      When reviewing the discussions and code, the following issues are concerning:

      1. The cache is guarded by a single lock for all reads and writes.
      2. All computations are performed outside of the any locking to avoid penalizing other callers. This doesn't handle the cache stampedes meaning that multiple threads may cache miss, compute the value, and try to store it. That redundant work becomes expensive under load and can be mitigated with ~ per-key locks.
      3. The cache queries the entry to see if it's even worth caching. At first glance one assumes that is so that inexpensive entries don't bang on the lock or thrash the LRU. However, this is also used to indicate data dependencies for uncachable items (per JIRA), which perhaps shouldn't be invoking the cache.
      4. The cache lookup is skipped if the global lock is held and the value is computed, but not stored. This means a busy lock reduces performance across all usages and the cache's effectiveness degrades. This is not counted in the miss rate, giving a false impression.
      5. An attempt was made to perform computations asynchronously, due to their heavy cost on tail latencies. That work was reverted due to test failures and is being worked on.
      6. An in-progress change tries to avoid LRU thrashing due to large, infrequently used items being cached.
      7. The cache is tightly intertwined with business logic, making it hard to tease apart core algorithms and data structures from the usage scenarios.

      It seems that more and more items skip being cached because of concurrency and hit rate performance, causing special case fixes based on knowledge of the external code flows. Since the developers are experts on search, not caching, it seems justified to evaluate if an off-the-shelf library would be more helpful in terms of developer time, code complexity, and performance. Solr has already introduced Caffeine in SOLR-8241 and SOLR-13817.

      The proposal is to replace the internals LruQueryCache so that external usages are not affected in terms of the API. However, like in SolrCache, a difference is that Caffeine only bounds by either the number of entries or an accumulated size (e.g. bytes), but not both constraints. This likely is an acceptable divergence in how the configuration is honored.

      cc Andrzej Bialecki, David Smiley


        1. cache.patch
          15 kB
          Ben Manes
        2. cache.patch
          23 kB
          Ben Manes
        3. CaffeineQueryCache.java
          5 kB
          Ben Manes



            • Assignee:
              ben.manes Ben Manes
            • Votes:
              0 Vote for this issue
              3 Start watching this issue


              • Created: