lfu cache optimization - redis labs · 2020. 1. 2. · whether redis will increment the counter...
TRANSCRIPT
![Page 1: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/1.jpg)
presented by
LFU Cache Optimization
Sripathi KrishnanCTO, RDBTools & HashedIn
![Page 2: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/2.jpg)
About RDBTools - Administration GUI for Redis
![Page 3: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/3.jpg)
LFU Cache is a 3 Step Process
1. Increment a key specific counter every time it is accessed2. Decrement the counter over time3. Evict keys that have a small counter value
![Page 4: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/4.jpg)
and each step corresponds to a setting ...
1. lfu-log-factor - for incrementing the counter2. lfu-decay-time - for decrementing the counter over time3. maxmemory-samples - for finding keys to evict
![Page 5: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/5.jpg)
This talk is about tuning these 3 parameters.
![Page 6: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/6.jpg)
Step1: Incrementing the Counter
![Page 7: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/7.jpg)
Redis only has an 8 bit Counter
… which means it can only count upto 255.
![Page 8: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/8.jpg)
So Redis Doesn’t Increment on Every Access
Instead, it uses probability. ● For lower counter values, probability to increment is higher● For higher counter values, probability to increment is very low
![Page 9: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/9.jpg)
If LFU Counter = 10, what is the frequency?
The real frequency could be anywhere between 75 and 125
![Page 10: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/10.jpg)
Whether redis will increment the counter depends on:
1. Current Counter Value2. lfu-log-factor
Higher the log factor, lower is the probability, therefore redis can track a higher frequency but at a much lower accuracy.
lfu-log-factor controls the probability
![Page 11: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/11.jpg)
● log factor = 0 => Range is 0-255, Accurate● log factor = 1 => Range is 0-30K, less accuracy● log factor = 5 => count upto 16K, lesser accuracy● log factor = 10 => count upto 1M, even lesser accuracy
Tradeoff between accuracy and range
![Page 12: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/12.jpg)
Step2: Decrementing the Counter
![Page 13: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/13.jpg)
lfu-decay-time
In how many minutes should redis decrement the counter? Default value is 1 minute.
This means that 4h 15m is the max time a key will retain it’s frequency.
![Page 14: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/14.jpg)
Initial Value for New Keys
Newly created keys start with a counter of 5…
which means that if it isn’t accessed again in 5 minutes, it could be evicted.
![Page 15: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/15.jpg)
Step3: Finding Keys to Evict
![Page 16: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/16.jpg)
maxmemory-samples = 5
Elena covered this in her talk. This is the same setting for LFU & LRU.
![Page 17: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/17.jpg)
Tuning LFU Parameters
![Page 18: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/18.jpg)
● Let’s assume you use the default log factor = 10● Assume your most frequently used key is accessed 10K times● The counter for your key will be ~50
Which means that in 50 minutes, the frequency of the key will become 0, and it becomes eligible for eviction.
What if you choose a wrong log factor?
![Page 19: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/19.jpg)
You want your most frequently used key to reach to a value around 255 - so that you get the best possible accuracy.
redis-server --maxmemory-policy volatile-lfu --lfu-log-factor 10
redis-cli del mfukey && \redis-benchmark -n 10000 incr mfukey && \redis-cli object freq mfukey
So what’s the right value?
![Page 20: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/20.jpg)
What if my workload varies a lot?
![Page 21: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/21.jpg)
If a subset of keys is used very frequently, and another less frequently … you have a problem.
Separate out your data into two separate redis instances.
Use Separate Redis Instances!
![Page 22: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/22.jpg)
redis-cli --hotkeys
-------- summary -------
Sampled 113958 keys in the keyspace!hot key found with counter: 162 keyname: mykeyhot key found with counter: 157 keyname: mykey2hot key found with counter: 11 keyname: mykey3hot key found with counter: 10 keyname: key100
Finding Hot Keys
![Page 23: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/23.jpg)
Temporarily set lfu-decay-time to a high number in redis.conf
This tells redis ‘Don’t decrement the counter’ - so you get a better idea of how frequently keys are being used.
Tip to Find Hot Keys
![Page 24: LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter depends on: 1.Current Counter Value 2.lfu-log-factor Higher the log factor, lower is](https://reader034.vdocument.in/reader034/viewer/2022052105/6040b9c38ec6836cb50e8f74/html5/thumbnails/24.jpg)
presented by
Thank you!