当前位置:网站首页>What happens when redis runs out of memory
What happens when redis runs out of memory
2022-06-25 05:55:00 【HelloWorld forever】
1. Memory recovery
Use redis We need to set the validity period for some key value pairs ,redis Through 4 A separate command to set the expiration time for a key value :
(1)expire key ttl : take key The expiration time for is set to ttl second
(2)pexpire key ttl : take key The expiration time of the value is set to ttl millisecond
(3)expireat key timestamp : take key The expiration time of is set to the specified timestamp Number of seconds
(4)pexpireat key timestamp : take key The expiration time of the value is set to the specified timestamp Number of milliseconds
Whatever command you use ,redis The bottom floor is all through pexpireat To set the expiration time
View remaining expiration time : Can pass ttl and pttl Two commands to view
ttl key return key Seconds left to expire
pttl key return key The number of milliseconds remaining to expire
If the expiration time is not set, this 2 All commands return -1 , If an illegal expiration time is set , All return to -2.
2. Expiration strategy
If you delete an expired key , We have three strategies
(1) Delete regularly : Set a timer for each key , Once the expiration time is up , Delete the key . This strategy is memory friendly , But yes. CPU unfriendly , Because each timer will occupy a certain amount of CPU resources .
(2) Lazy deletion : No matter whether the key has expired or not, it will not be deleted automatically , Wait until each time you get the key to determine whether it is expired , Delete the key if it is out of date , Otherwise, it returns the value of the key . This strategy is not memory friendly , It can waste a lot of memory .
(3) Scan regularly : The system scans regularly at regular intervals , Delete the expired key when it is found . This strategy is relatively a compromise between the above two strategies , It should be noted that the regular frequency should be well controlled according to the actual situation , One drawback of using this scheme is that keys that have expired may also be returned .
stay Redis in , The choice is strategy (2) And strategy (3) Comprehensive use of , however redis The regular scan of will only scan the key with expiration time set , Because the key to set the expiration time Redis Will be stored separately , So you don't have to scan all the keys :
typedef struct redisDb{
dict *dict ; // All key value pairs
dict * expires; // Set the key value pair of expiration time
dict * blocking_keys ;// blocked key
dict * watched_keys; //WATCHED keys
}
Redis Of 8 Kind of elimination strategy
First redis A parameter is provided maxmemory To configure the redis Maximum memory used :maxmemory <bytes> Or by order config set maxmemory 1GB To dynamically modify . If the parameter is not set , So in 32 Bit in the operating system Redis Use... At most 3GB Memory , And in the 64 There are no restrictions in the bit operating system .
Redis Provided in 8 Kind of elimination strategy , You can use the parameter maxmemory-policy To configure :

ttl: Set the expiration time of key in , Those with less time left will be eliminated first .
lru: Least recently used key, Priority to be eliminated .
lfu: The least visited recently key, Priority to be eliminated .
random: Randomly eliminate the contents of memory .
noeviction: Can no longer write Redis, Does not process the contents of memory , Is the default elimination strategy .
边栏推荐
- Depth of binary tree
- Design of IM login server and message server
- CSDN cerebral palsy bug has wasted nearly two hours of hard work
- Trouble of setting table property to null
- Vscode voice notes to enrich information (medium)
- JS to realize the encapsulation of the function of obtaining the mouse click position
- Duplicate symbols for architecture i386 clang
- Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
- Use of MySQL variables
- JS implementation mouse can achieve the effect of left and right scrolling
猜你喜欢

Day21 performance test process
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications

Stack and Queue

Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation

MySQL operation JSON

Deep learning non local neural networks
SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control
How SAP ui5 device type detection device API works

Instant messaging project (I)

Use of MySQL variables
随机推荐
Interface learning
C language -- Sanzi chess
Some common errors and solutions of using SAP ui5 to consume OData services
SQL get current time
[golang] leetcode intermediate - Search rotation sort array & search two-dimensional matrix II
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
[untitled]
Summary of 6 common methods of visual deep learning model architecture
JS implementation mouse can achieve the effect of left and right scrolling
Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications
SAP ui5 beginner tutorial No. 27 - unit test tool quNit introduction trial version for SAP ui5 application
JSON Library Tutorial from scratch (I): starting to learn and organize notes
Introduction to MySQL test run test framework
Jz-066- motion range of robot
An example of recursion, the full permutation problem of 1~n
PAT (Advanced Level) Practice 1025
Do you know what a three-tier architecture is?
Pat 1045 quick sort
Design of IM login server and message server