What is the default redis memory size and how do you configure it
1. On a 64-bit machine, if the redis memory size is not configured, the default is the maximum memory size of the system, and if a 32-bit system, the maximum memory size is 3G
2. To manually configure the redis memory, open the redis. Conf configuration file and set the maxMemory parameter.
3. Redis is generally recommended to set the memory to three-quarters of the maximum physical memory.
4. Run the config get maxmemory command to query the redis memory, and run the config get maxmemory 1024 command to set the redis memory
What happens if Redis memory usage exceeds the maximum set?
1. OOM memory overflow is reported
Redis has three different deletion strategies
1. Delete periodically
-
This ensures maximum freshness of data in memory because it guarantees that the expiration key will be deleted immediately after expiration, but it is not CPU-friendly because deletion takes up CPU time and puts extra stress on the CUP if the CPU is busy
-
Cons: CPU unfriendly, trading processor power for storage (time for space)
2. Lazy deletion
-
When the data reaches the expiration time, it is not processed. When the data is accessed next time, if the data is not expired, it is returned and the data is deleted. In extreme cases, if a key is expired and has not been accessed, it will occupy memory and will not be released, wasting memory resources
-
Cons: Memory unfriendly, trading storage for processor performance (space for time)
Periodically delete
- Periodic deletion policies Perform the deletion expiration key at intervals and limit the duration and frequency of the deletion operation to reduce the impact on the CPU time.
- For example, periodical polling of time-sensitive data of Redis library is adopted, random sampling strategy is adopted, and the deletion frequency is controlled by the proportion of expired data
- Features: CPU usage is set to peak value, detection frequency can be automatically set, memory pressure is not very high, and cold data that occupies memory for a long time is continuously cleared
- The difficulty of the periodic deletion strategy is to determine the duration and frequency of the deletion operation: If a delete operation frequently, or execution time is too long, regularly delete strategy will degenerate regularly delete, so that the consumption of too much CPU time on delete expired keys, if delete operation execution is too little, or execution time is too short, time deletion policy would like inert strategy, a waste of memory. Therefore, the server must set the execution duration and frequency of the deletion operation based on the actual situation
- Summary: Periodically check storage space (randomly and critically)
For example, redis checks expired keys every 100ms by default and deletes expired keys. Note: Instead of checking all keys every 100ms, Redis checks them randomly. Therefore, if the periodic deletion policy is adopted, many keys will not be deleted.
Memory flushing strategy
- The second, and least recently used, is a common page replacement algorithm. Select the most recent data that has not been used for the longest time to eliminate.
LRU elimination strategy algorithm implementation see another blog: detailed explanation of Redis elimination strategy LRU algorithm implementation