Redis memory elimination means that some keys stored by users can be actively deleted from the instance by Redis, resulting in read misses. So why does Redis have this function? This is the original design intention that we need to explore.
The two most common application scenarios of Redis are caching and persistent storage. The first question to be clarified is which scenario is the memory elimination strategy more suitable for? Is it persistent storage or cache?
The original intention of the memory elimination mechanism is to make better use of memory and use certain cache misses in exchange for memory usage efficiency.
As a Redis user, how do I use this feature provided by Redis? Take a look at the following configuration
# maxmemory <bytes>
We can enable the memory elimination function by configuring the maxmemory value in redis.conf. As for the meaning of this value, we can understand its meaning by understanding the process of memory elimination:
1. The client initiated a command that needs to apply for more memory (such as set).
2. Redis checks the memory usage. If the used memory is greater than maxmemory, it will start to eliminate the memory (key) according to different elimination strategies configured by the user in exchange for a certain amount of memory.
3. If there is no problem with the above, then this command is executed successfully.
When maxmemory is 0, it means that we have no limit on the memory usage of Redis.
Redis provides the following elimination strategies for users to choose from, the default strategy is the noeviction strategy:
noeviction: When the memory usage reaches the threshold, all causes The command to apply for memory will report an error.
allkeys-lru: In the primary key space, remove recently unused keys first.
volatile-lru: In the key space with an expiration time set, remove recently unused keys first.
allkeys-random: Randomly remove a key in the primary key space.
volatile-random: Randomly remove a key in the key space with expiration time set.
volatile-ttl: In the key space with expiration time set, keys with earlier expiration time will be removed first.
Here we add the primary key space and the key space with expiration time set. For example, assuming we have a batch of keys stored in Redis, there is a hash table for storage. This batch of keys and their values. If some of the keys in this batch have an expiration time set, then this batch of keys will also be stored in another hash table. The value in this hash table corresponds to the expiration date of the key. time. The key space with expiration time set is a subset of the primary key space.
We understand that Redis provides roughly several elimination strategies, so how to choose? The selection of the elimination strategy can be specified through the following configuration:
# maxmemory-policy noeviction
But what should be filled in with this value? To solve this problem, we need to understand how our application requests access to the data set stored in Redis and what our demands are. At the same time, Redis also supports Runtime to modify the elimination strategy, which allows us to adjust the memory elimination strategy in real time without restarting the Redis instance.
Let’s take a look at the applicable scenarios of several strategies:
allkeys-lru: If our application’s access to the cache conforms to a power law distribution (that is, there are relative hot spots data), or we don’t know much about the cache access distribution of our application, we can choose the allkeys-lru strategy.
allkeys-random: If our application has equal access probability to cache keys, we can use this strategy.
volatile-ttl: This strategy allows us to prompt Redis which keys are more suitable for eviction.
In addition, the volatile-lru strategy and volatile-random strategy are suitable for when we apply a Redis instance to both cache and persistent storage, but we can also By using two Redis instances to achieve the same effect, it is worth mentioning that setting the key expiration time will actually consume more memory, so we recommend using the allkeys-lru strategy to use memory more efficiently.
For more redis knowledge, please pay attention to the redis introductory tutorial column.
The above is the detailed content of Introduction to redis memory elimination mechanism. For more information, please follow other related articles on the PHP Chinese website!

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor