+ All Categories
Home > Documents > LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter...

LFU Cache Optimization - Redis Labs · 2020. 1. 2. · Whether redis will increment the counter...

Date post: 10-Oct-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
24
presented by LFU Cache Optimization Sripathi Krishnan CTO, RDBTools & HashedIn
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

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

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

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

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

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

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

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

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

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

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

● 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

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

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

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

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

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

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

● 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

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

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

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

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

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

presented by

Thank you!


Recommended