Giter Site home page Giter Site logo

Conditional/Delayed Eviction about moka HOT 1 OPEN

moka-rs avatar moka-rs commented on June 12, 2024
Conditional/Delayed Eviction

from moka.

Comments (1)

tatsuya6502 avatar tatsuya6502 commented on June 12, 2024 1

Hi. Sorry for the late reply, and thank you for the request. Your idea seems feasible to me.

Before we talk about your idea, let me explain how Moka cache works today.

When cache's capacity is exceeded, there are two timings when entry can be evicted:

  1. Right after the insertion: An entry can be evicted by the historic LFU (Least Frequently Used) filter.
    • This filter prevents unpopular key to get into the cache.
  2. Some time after the insertion: An entry can be evicted after dropping off from the LRU (Least Recently Used) queue.

Please see the TinyLFU diagram in this section of the wiki page.

TinyLFU policy may not work very well for certain access patterns. I wonder if you are hitting this problem. The access pattern is like the followings:

  • Inserting an entry whose key has never been accessed before.
  • But once inserted, the key will be accessed often for a while.

While the cache has enough capacity, everything will be okay because new entries will be admitted anyway. After the admission, they will be accessed so they will build up popularity. However, once the cache becomes full, this will become a problem. New entries will not be admitted because their keys were never accessed before; they are less popular than the old ones. They will be evicted immediately after the insertion.

In the future, we will upgrade TinyLFU to W-TinyLFU, which has an admission LRU window in front of the LFU filter. W-TinyLFU will work better for the access pattern above because the LRU window will help the new entries to build up the popularity.

If you are hitting this problem, W-TinyLFU will mitigate it. W-TinyLFU will work automatically for all users; no need to set the callback. So I wonder which one we should implement first: W-TinyLFU? or your conditional eviction idea?

One could argue that W-TinyLFU still does not guarantee that an entry is not evicted while it is alive. (e.g. the entry has very long life)

If we implement your idea, we could add two pending eviction queues to the cache: one for 1., and another for 2. If the callback returned false on an entry, that entry will be added to one of these queues and kept in the cache.

Also, there are other cases when entry will be removed from a cache, so we need to decide what to do for such cases:

  • An entry can be expired when TTL (time-to-live) or TTI (time-to-idle) timer exceeded:
    • With the current spec, get operation never returns expired entry.
    • Should the callback prevent the entry from expiring?
      • We could add a config flag to the cache builder to customize this behavior.
  • An entry can be replaced by another insert on the same key:
    • With the current spec, insert always succeeds and replaces the old entry.
      • insert has no return value; no way to tell when insert is failed.
    • So, should we keep the current insert behavior by ignoring the callback?
    • Note that one can use get_with to avoid replacing an existing entry.
  • An entry can be invalidated by invalidate, invalidate_all, etc.:
    • With the current spec, invalidate etc. always succeeds and removes the entry.
      • invalidate has no return value; no way to tell when invalidate is failed.
    • So, should we keep the current invalidate behavior by ignoring the callback?

from moka.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.