This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.


😉 offer yourself

Self-introduction, to everyone recommend their column 😁, welcome small partners to collect attention 😊

MybatisPlus column

App crawler Column

PC side crawler column

Big factory interview question column


🔥 a map to see what Redis cache elimination policies are!!

✨ Premise summary

As we all know, Redis is based on memory database, data are in memory, with its efficient data structure, multi-way IO mechanism, single thread and so on, making Redis performance is very high, single instance QPS can reach about 10W.

However, memory is also limited, you can not let unlimited data storage

So, when the Redis memory is full, if the write request will be an error, of course, the read request is still normal execution!

So, Redis memory full after the solution, the following one for everyone to explain!! 😁


🔥 Dynamically change the memory size by running commands

127.0.0.1:6379> config set maxmemory 100mb // obtain the maximum memory size for Redis 127.0.0.1:6379> config get maxmemoryCopy the code

If the maximum memory size is not set or the maximum memory size is set to 0, the memory size is not limited in a 64-bit operating system and the maximum memory size is 3GB in a 32-bit operating system

🔥Redis expiration deletion policy

Common deletion strategies for expired data are two (important! Things to consider when building your own cache wheel) :

Lazy delete: Data is expired only when the key is removed. This is cpu-friendly, but may result in too many expired keys not being deleted. Periodically delete: By default, Redis randomly selects some keys with expiration time every 100ms, checks whether they are expired, and deletes them if they are expired.

Periodic deletes are more memory friendly, and lazy deletes are CPU friendly. Both have their strengths,

So Redis uses a mix of periodic deletion and lazy deletion.

However, simply setting an expiration time for a key is problematic. It is still possible for periodic deletes and lazy deletes to miss a lot of expired keys. This will cause a large number of expired keys to accumulate in memory, and then OOM.

Solution: Redis memory flushing mechanism.


🔥Redis cache elimination policy

  • Noeviction: When adding data, Redis will return error if it judges that the operation will cause memory usage to exceed its memory limit and do nothing

  • Allkees-lru: when adding data, if redis determines that the operation will cause memory usage to exceed the memory limit, it will scan allkeys and eliminate some recently unused keys

  • Allkeys-lfu: when adding data, if redis determines that the memory usage exceeds the memory limit, it scans allkeys and removes the least recently used keys

  • Allkeys-random: when adding data, if redis determines that the memory usage will exceed the memory limit, it scans allkeys and randomly removes some keys

  • Volatile – lRU: When adding data, if Redis determines that the operation will cause memory usage to exceed the memory limit, it scans for keys that have expired and eliminates some that have not been used recently

  • Volatile -random: When adding data, if Redis determines that the operation will cause memory usage to exceed the memory limit, it scans for keys with expiration dates and randomly eliminates some keys

  • Volatile – lFU: When adding data, Redis will eliminate the least recently used keys with expiration dates if it determines that the operation will exceed the memory limit

  • Volatile – TTL: When adding data, if Redis determines that the operation will cause memory usage to exceed the memory limit, it scans the set of expired keys and eliminates those that are about to expire


❤ finally

I am aCode pipi shrimp, a prawns lover who loves to share knowledge, will update useful blog posts in the future, looking forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == one key three even oh! Thank you for your support. See you next time