-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
Can the eviction strategy be configured? #247
Comments
Hi. Moka does not support other eviction strategies like SampledLFU. I do not have a plan to support it/them as the current strategy seems to give good overall performance. But nothing stops someone from implementing other strategies. To implement it, you will start to look at the moka/src/sync_base/base_cache.rs Lines 1448 to 1525 in 83ce745
Currently, it gets the LRU entry at line 1480 and 1488. You will need to change it to sample some entries and then get the LFU of them. To sample entries and find the LFU, you will do the followings:
I hope this helps. |
Really appreciate it, it definitely helps, I will try it. Also, the background here is I have a go service using ristretto to cache, when I rewrote it with rust using Moka with the same configuration, the cache hit rate dropped from about 90% to 70%. |
I think there is another difference between Ristretto and Moka/Caffeine. Ristretto has larger frequency sketch than Moka. https://github.com/dgraph-io/ristretto#features
Moka's frequency sketch (aka CountMin sketch) is direct port from Caffeine, and it has 4 bits per counter. moka/src/common/frequency_sketch.rs Lines 14 to 18 in 83ce745
A 12-bit counter can count up to 4095, while a 4-bit counter can count up to 15. You might want to change Moka to use 12-bit counters as well to get a similar cache hit performance to Ristretto. |
Thanks, I'd like to try it. Is there any cache hit bench for Moka that I can reuse to compare with different bit counters, now I implemented 16-bit counters. |
Yes, there are two. Mokabench will be easier to run. It is written entirely in Rust. Follow the README and download the trace datasets called ## Run with the S3 dataset.
$ cargo run --release -- --num-clients 1
## Run with the DS1 dataset.
$ cargo run --release -- --num-clients 1 --trace-file ds1 (Do not use Another benchmarking tool is Moka Cache Driver for the Caffeine Simulator. It is written in Rust and Java, so it will need more work to run. But a nice thing with it is that you can compare results with other caching strategies. I used it for creating the benchmark results on our Wiki. |
I tried it, but still the same as before, the cache hit rate first goes up to 90% and stays there for a while, then goes down. |
Nice catch! dgraph-io/ristretto#335 It seems Ristretto uses 4-bit counter too. The max value is 15 and // Counter value.
v := (r[i] >> s) & 0x0f
// Only increment if not max value (overflow wrap is bad for LFU).
if v < 15 {
r[i] += 1 << s
} |
@Millione you might also consider recording a trace (e.g. log a 64-bit hash of the key). Then you can run it against the different simulators that the projects provide and assert behavior, e.g. if any are not honoring the maximum size or misreporting the hit rate, and see what other policies report (like optimal). A trace of a few million events is typically pretty good; you want something many multiples of the cache size to ensure the run captures long-term the workfload characteristics. |
Hi @Millione,
FYI, you might be affected by the issue fixed by |
Is this issue solved?
|
Hi @Ten0 As for the SampledLFU eviction strategy, no, it is not implemented. Moka's default cache policy remains Caffeine style TinyLFU, which uses an access-order (LRU) queue for selecting a cache victim. (Cache victim is a candidate to be removed from the cache). And this issue is about adding Ristretto style TinyLFU, which uses random sampling for selecting a cache victim (thus SampledLFU). I opened #389. I will evaluate cache hit performance of Ristretto style TinyLFU to see if it is worth to be implemented. As for the original issue that the user who created this GH Issue was having with
We fixed a bug in
|
Right now the eviction strategy is LRU, but I want to use SampledLFU strategy to suit my usage scenario, how could I get there?
The text was updated successfully, but these errors were encountered: