Redis Tutorial (11): Introduction to Virtual Memory
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)!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.
