Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

RedisDay London 2018 - What happens when Redis runs out of memory

159 views

Published on

RedisDay London 2018

Published in: Technology
  • Be the first to comment

RedisDay London 2018 - What happens when Redis runs out of memory

  1. 1. What happens when Redis runs out of memory Elena Kolevska
  2. 2. What is cache?
  3. 3. In computing, a cache is a hardware or software component that stores data so that future requests for that data can be served faster The Architecture of Computer Hardware and Systems Software, An Information Technology Approach ~ Irv Englander
  4. 4. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. The more requests that can be served from the cache, the faster the system performs.
  5. 5. 5 2009 2018 March 29th First commit
  6. 6. Pre Redis 2.2 New data Expired data
  7. 7. 7 2009 2018 March 29th First commit May 2009 maxmemory implemented
  8. 8. Pre Redis 2.2 maxmemory + volatile-ttl
  9. 9. Pre Redis 2.2 maxmemory + volatile-ttl •sample 3 random volatile keys, expire the one with shortest TTL •If no volatile keys - return an error
  10. 10. "Why delete objects if there is no need to? And why use more memory than needed?" ~ Salvatore Sanfilippo
  11. 11. Why use extra bits for storing the TTL if there is no need to? ~ Salvatore Sanfilippo
  12. 12. 12 LRU = Least Recently Used
  13. 13. 13 LRU = Least Recently Used •If you’ve used an item from your cache recently - you’re more likely to use it again. •The longer you haven’t used an item, the smaller the chance you’ll need it again
  14. 14. 14 Since LRU is itself an approximation of what we want to achieve, how about approximating LRU itself? Salvatore Sanfilipo, Blog post
  15. 15. 15 Sample 3 random keys and evict the one with the highest idle time
  16. 16. 16 Sample X random keys and evict the one with the highest idle time maxmemory-samples
  17. 17. Commit: 165346ca29972817b1245e689315edeba1fe369b [165346ca] Author: antirez <antirez@gmail.com> Date: October 14, 2010 at 20:22:21 GMT+1 # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached? You can select among five behaviors: # # volatile-lru -> remove the key with an expire set using an LRU algorithm # allkeys-lru -> remove any key accordingly to the LRU algorithm # volatile-random -> remove a random key with an expire set # allkeys->random -> remove a random key, any key # volatile-ttl -> remove the key with the nearest expire time (minor TTL) # # maxmemory-policy volatile-lru
  18. 18. 18 2009 2018 March 29th First commit May 2009 maxmemory implemented October 2010 LRU implemented v 2.2 alpha 3
  19. 19. About a month later the noeviction policy was added
  20. 20. 20 2009 2018 March 29th First commit May 2009 maxmemory implemented October 2010 LRU implemented November 2010 noeviction policy added v 2.2 alpha 3 v 2.2 alpha 5
  21. 21. ...if you look at this algorithm *across* its executions, you can see how we are trashing a lot of interesting data Salvatore Sanfilipo, Blog post
  22. 22. First rule of Fight Club is: observe your algorithms with naked eyes Salvatore Sanfilipo, Blog post
  23. 23. 24 LRU v2 • Use a pool of best candidates for eviction (maxmemory-samples) • All sampled keys are compared against the keys in the pool and if they're more adapt to eviction compared to a key in the pool - they switch places •After all N keys have benn compared, the best candidate is evicted from the pool
  24. 24. https://www.dropbox.com/s/md9a8me9goksny5/Screenshot%202018-11-12%2021.33.25.png?dl=0
  25. 25. 26 2009 2018 March 29th First commit May 2009 maxmemory implemented October 2010 LRU implemented November 2010 noeviction policy added v 2.2 alpha 3 v 2.2 alpha 5 March 2014 - LRU eviction pool - maxmemory-samples = 5 - LRU field changed to 24 bits (v 2.8.8) v 3.0 Default policy noeviction v 2.8.8
  26. 26. 28 2009 2018 March 29th First commit May 2009 maxmemory implemented October 2010 LRU implemented November 2010 noeviction policy added July 2016 Cross-database eviction March 2014 - LRU eviction pool - maxmemory-samples = 5 - LRU field changed to 24 bits (v 2.8.8) v 3.0 Default policy noeviction v 2.8.8 v 2.2 alpha 3 v 2.2 alpha 5
  27. 27. r this subsystem of Redis was stimulated again at that point. I wan Salvatore Sanfilipo, Blog post
  28. 28. probability of being accessed in the future, that are the keys *mos Salvatore Sanfilipo, Blog post
  29. 29. 31 LFU = Least Frequently Used
  30. 30. 32 LFU = Least Frequently Used • Track access (implement access counter) • Track time (so you can know the frequency)
  31. 31. 33 LRU counter space <-> LFU counters space
  32. 32. 34 24 bits 16 bits Last decrease time 8 bits LOG C
  33. 33. 35 24 bits reduced-precision unix time less significant bits of the UNIX time converted to minutes Todo: show the "math" 16 bits Last decrease time 8 bits LOG C
  34. 34. 36 24 bits 16 bits Last decrease time 8 bits LOG C Morris (logarithmic) counter+--------+------------+------------+------------+------------+------------+ | factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits | +--------+------------+------------+------------+------------+------------+ | 0 | 104 | 255 | 255 | 255 | 255 | +--------+------------+------------+------------+------------+------------+ | 1 | 18 | 49 | 255 | 255 | 255 | +--------+------------+------------+------------+------------+------------+ | 10 | 10 | 18 | 142 | 255 | 255 | +--------+------------+------------+------------+------------+------------+ | 100 | 8 | 11 | 49 | 143 | 255 | +--------+------------+------------+------------+------------+------------+
  35. 35. 37 2009 2018 March 29th First commit May 2009 maxmemory implemented October 2010 LRU implemented November 2010 noeviction policy added July 2016 Cross-database eviction March 2014 - LRU eviction pool - maxmemory-samples = 5 - LRU field changed to 24 bits (v 2.8.8) v 3.0 Default policy noeviction v 2.8.8 July 2014 - LFU Implementation - Volatile-ttl uses the pool v 4.0 v 2.2 alpha 3 v 2.2 alpha 5
  36. 36. maxmemory-policy ★ noeviction ★ allkeys-random ★ volatile-random ★ volatile-ttl ★ allkeys-lru ★ volatile-lru ★ allkeys-lfu ★ volatile-lfu
  37. 37. Thank you!

×