Home  >  Article  >  Backend Development  >  Redis Tutorial (11): Introduction to Virtual Memory

Redis Tutorial (11): Introduction to Virtual Memory

黄舟
黄舟Original
2016-12-28 15:04:451234browse

1. Introduction:

Like most NoSQL databases, Redis also follows the Key/Value data storage model. In some cases, Redis will save Keys/Values ​​in memory to improve the efficiency of data query and data modification. However, this approach is not always a good choice. In view of this, we can further optimize it, that is, try to keep only the Keys data in the memory, which can ensure the efficiency of data retrieval, and the Values ​​data can be swapped out to disk when it is rarely used.
In actual applications, only about 10% of Keys are relatively commonly used keys, so Redis can swap out the remaining less commonly used Keys and Values ​​to the disk through virtual storage, and once these are swapped out When Keys or Values ​​need to be read, Redis reads them back into main memory again.

2. Application scenarios:

For most databases, the most ideal operation method is to load all data into memory, and subsequent query operations can be completely based on memory. Data is complete. However, in reality, this scenario is not common. In more cases, only part of the data can be loaded into memory.
In Redis, there is a very important concept, that is, keys are generally not exchanged, so if there are a large number of keys in your database, and each key is only associated with a small value, then this scenario is not Great for using virtual memory. If on the contrary, the database only contains a small number of keys, but the value associated with each key is very large, then this scenario is perfect for using virtual storage.
In actual applications, in order to make the virtual memory play a fuller role and help us improve the operating efficiency of the system, we can merge Keys with many smaller values ​​into Keys with a small number of larger values. . The most important method is to change the original Key/Value mode to a Hash-based mode, which allows many original Keys to become attributes in Hash.

3. Configuration:

1). Add the following configuration items in the configuration file to enable the current Redis server to turn on the virtual memory function when it starts.
vm-enabled yes

2). Set the maximum number of virtual memory bytes available for Redis in the configuration file. If the data in the memory is greater than this value, some objects will be swapped out to the disk, and the memory occupied by the swapped out objects will be released. Swapping out will not stop until the used memory is less than this value.

     vm-max-memory (bytes)

The exchange rule of Redis is to try to consider the "oldest" data, that is, the data that has not been used for the longest time will be swapped out. If the ages of the two objects are the same, the data with the larger Value will be swapped out first. It should be noted that Redis will not exchange Keys to disk, so if the data of keys alone has filled the entire virtual memory, then this data model will not be suitable for using the virtual memory mechanism, or the value must be set higher. Large to accommodate the entire Keys data. In actual applications, if we consider using Redis virtual memory, we should allocate as much memory as possible to Redis to avoid frequent swapping in and out.

3). Set the number of pages and the number of bytes occupied by each page in the configuration file. In order to transfer data from memory to disk, we need to use a swap file. These files have nothing to do with data persistence and Redis will delete them all before exiting. Since most access to the swap file is random access, it is recommended to store the swap file on a solid-state disk, which can greatly improve the operating efficiency of the system

vm-pages 134217728
    vm-page-size 32

In the above configuration, Redis divides the swap file into vm-pages pages, the bytes occupied by each page are vm-page-size, then the final swap file size available for Redis is: vm-pages * vm-page-size. Since a value can be stored on one or more pages, but one page cannot hold multiple values, in view of this, we need to fully consider this feature of Redis when setting vm-page-size.

4). There is a very important configuration parameter in the Redis configuration file, namely:

    vm-max-threads 4

CPU cores。如果将该值设置为0,那么Redis在与交换文件进行IO交互时,将以同步的方式执行此操作。
    对于Redis而言,如果操作交换文件是以同步的方式进行,那么当某一客户端正在访问交换文件中的数据时,其它客户端如果再试图访问交换文件中的数据,该客户端的请求就将被挂起,直到之前的操作结束为止。特别是在相对较慢或较忙的磁盘上读取较大的数据值时,这种阻塞所带来的影响就更为突兀了。然而同步操作也并非一无是处,事实上,从全局执行效率视角来看,同步方式要好于异步方式,毕竟同步方式节省了线程切换、线程间同步,以及线程拉起等操作产生的额外开销。特别是当大部分频繁使用的数据都可以直接从主内存中读取时,同步方式的表现将更为优异。
    如果你的现实应用恰恰相反,即有大量的换入换出操作,同时你的系统又有很多的cores,有鉴于此,你又不希望客户端在访问交换文件之前不得不阻塞一小段时间,如果确实是这样,我想异步方式可能更适合于你的系统。
    至于最终选用哪种配置方式,最好的答案将来自于不断的实验和调优。

以上就是Redis教程(十一):虚拟内存介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn