Redis’s VM(virtual memory) mechanism temporarily swaps infrequently accessed data (cold data) from memory to disk, freeing up valuable memory space for other data that needs to be accessed (hot data).

Through the VM function can realize the separation of hot and cold data, so that hot data is still in memory and cold data is saved to disk. This avoids the problem of slow access due to insufficient memory.

There are two ways Redis can increase database capacity:

1. One method can split data to multiple Redis servers;

2. The other is to use virtual memory to swap infrequently accessed data to disk.

It is important to note that Redis does not use the OS provided Swap, but implements it itself.

In order to keep the lookup speed, Redis will only swap out the value and keep all the keys in memory. So it’s a good fit for a storage structure where keys are small and values are large. If the Key is large and the value is small, then the VM may not be able to meet the requirements.

1. VM-related configuration

Data is swapped in and out of memory and disk by setting VM parameters in Redis. conf file. The relevant configuration is as follows:

# Vm-enabled yes # swap-file/TMP /redis.swap # Set the value to be swapped out of vm-max-memory 1000000 when memory consumption is reached # Sets the maximum number of pages that can be swapped to disk vm-pages 13417728 # Sets the number of worker threads that can be swapped. Set to 0 means that no worker threads are used and the main thread is used, which will run as a block. It is recommended to set the number of CPU cores as vm-max-threads 4

Redis states that only one object can be stored on the same data page, but an object can be stored on multiple data pages. No value will be swapped to disk until Redis uses more memory than VM-Max-Memory. When the maximum memory limit is exceeded, Redis will select the older object (if the two objects are equally old, the larger object will be swapped in preference) and remove it from memory, thus saving more memory.

For Redis, a data page will only hold one object, which is a Value, so VM-page-size should be set so that most values can be stored. If the setting is too small, a single Value object will occupy several pages of data, and if the setting is too large, the free space on the page will be wasted.

2. The working mechanism of VM

The Redis VM works in two ways: one is VM-max-threads =0, and the other is VM-max-threads >0.

First: vm-max-threads = 0

Data exchange:

The main thread periodically checks the size of memory used. If it finds that the maximum memory is exceeded, it blocks the selected object to disk (saved to a file) and frees the memory used by the object. The process repeats until either of the following conditions is met:

1. Memory usage dropped below maximum limit.

2. The maximum number of swapped files has been set.

3. Almost all the objects have been swapped to disk.

Data swap in:

When a client requests that the corresponding value of a key has been swapped out to disk, the main thread will load the corresponding value object from the swapped out file in a blocking manner. At this time, all clients will be blocked and the client’s request will be processed. This will block all clients.

Vm-max-threads > 0

Data exchange:

When the main thread detects that the maximum memory usage is exceeded, the selected data to be exchanged is placed in a queue for background processing by the worker thread, and the main thread continues to process the client request.

Data swap in:

When the value of a key requested by a client has been swappened out to disk, the main program blocks the current client and puts the information of the loaded object in a queue for the worker thread to load. At this time, the main program continues to process other client requests. When the load completes, the worker notifies the main thread, which then executes the blocked client’s commands. This only blocks a single client.

Conclusion: Redis directly builds its own VM mechanism, not like the general system will call system function processing, will waste a certain amount of time to move and request, but Redis does not exist. That’s one reason Redis is so fast.

The original link: https://blog.csdn.net/Seky_fe…

Copyright Notice: This article is the original work of Mr. Youyan, a CSDN blogger. It is under the CC 4.0 BY-SA copyright agreement. Please attach the link of the original source and this notice.

Recent hot article recommended:

1.1,000+ Java interview questions and answers (latest version of 2021)

2. I finally got the IntelliJ IDEA activation code through open source project. How nice!

3. Ali Mock tool officially open source, kill all the Mock tools in the market!

4.Spring Cloud 2020.0.0 is officially released, a new and disruptive version!

5. “Java development manual (Songshan edition)” the latest release, fast download!

Feel good, don’t forget with thumb up + forward oh!