Memory used by Redis Memory used by Redis Obsolete LRU algorithm Implementation of LRU in Redis Memory used by LFU algorithm Redis

We know that Redis is a key-value database based on memory. Because the memory size of the system is limited, we can configure the maximum memory size that Redis can use when using Redis.

We know that Redis is a key-value database based on memory. Because the memory size of the system is limited, we can set the maximum memory size that Redis can use when using Redis.

1. Set the memory size by adding the following configuration to the redis.conf configuration file under the Redis installation directory

// Set the maximum memory used by Redis to 100M
maxmemory 100mb
Copy the code

The redis configuration file does not necessarily use the redis. Conf file under the installation directory. When starting the Redis service, you can pass a parameter to specify the redis configuration file

Redis supports dynamic memory size change at runtime through commands

// Set the maximum memory used by Redis to 100M
127.0.0.1:6379> config set maxmemory 100mb
// Get the maximum memory size Redis can use
127.0.0.1:6379> config get maxmemory
Copy 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 memory obsolescence

Since you can set the maximum memory usage of Redis, the configured memory will run out. Redis will run out of memory if it continues to add data to Redis. Redis defines several policies to handle this situation: noeviction: write requests will no longer be served, errors will be returned (except for DEL requests and some special requests) allkex-lru: volatile lru algorithm will be disabled from allkeys. 11. volatile: randomly eliminate volatile- TTL from keys with expiration: A key whose expiration time is set is eliminated according to its expiration time. The earlier the key expires, the earlier the key is eliminated

Like Noeviction, error returns when no key can be eliminated when using volatile- LRU, volatile- Random, volatile- TTL policies.

How do I obtain and set a memory flushing policy

Get the current memory elimination policy:

127.0.0.1:6379> config get maxmemory-policy
Copy the code

Set the elimination policy through the configuration file (modify the redis.conf file) :

maxmemory-policy allkeys-lru
Copy the code

Modify the elimination policy by running the following command:

127.0.0.1:6379> config set maxmemory-policy allkeys-lru
Copy the code

LRU algorithm

What is the LRU?

The LRU algorithm can be used to flush out memory when the maximum memory available is used.

LRU(Least Recently Used) is a cache replacement algorithm. When using memory as a cache, the size of the cache is usually fixed. When the cache is full and data is added to the cache, some of the old data needs to be eliminated to free up memory for new data. At this point you can use the LRU algorithm. The idea is that if a piece of data has not been used in the recent past, it is unlikely to be used in the future, so it can be eliminated.

Implementation of LRU in Redis

Approximate LRU algorithm

Redis uses an approximate LRU algorithm, which is not quite the same as the regular LRU algorithm. The approximate LRU algorithm weeded out data by random sampling, randomly producing 5 (default) keys at a time, and weeding out the least recently used keys from the data.

The number of samples can be modified with the maxmemory-samples parameter: example: maxmemory-samples 10 The larger the maxmenory-samples configuration, the closer the result of the elimination is to the strict LRU algorithm

In order to realize the approximate LRU algorithm, Redis adds an extra 24bit field to each key, which is used to store the last access time of the key.

The above content hopes to help you, more PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be searched on wechat: PHP open source community

2021 Jinsanyin four big factory interview real questions collection, must see!

Four years of PHP technical articles collation collection – PHP framework

A collection of four years’ worth of PHP technical articles – Microservices Architecture

Distributed Architecture is a four-year collection of PHP technical articles

Four years of PHP technical essays – High Concurrency scenarios

Four years of elite PHP technical article collation collection – database