Redis Study Notes-Virtual Memory

Source: Internet
Author: User
First, we will explain that redis's virtual memory and OS's virtual memory are not the same thing, but their ideas and purposes are the same. It is to temporarily swap infrequently accessed data from the memory to the disk, so as to free up valuable memory space for other data to be accessed. Especially for memory databases such as redis, the memory is always insufficient. In addition to splitting data into multiple redis servers. Another way to increase database capacity is to use VM to swap infrequently accessed data disks. If a small portion of the stored data is frequently accessed and most of the data is rarely accessed, the website is always active only a small number of users. When a small amount of data is frequently accessed, using a VM not only increases the capacity of a single redis Server database, but also does not have too much impact on performance.

Redis does not use the virtual memory mechanism provided by OS, but implements its own virtual memory mechanism in user mode. The author explains the reason in his blog. Http://antirez.com/post/redis-virtual-memory-story.html
There are two main reasons:

1. The virtual memory of the OS is switched in the smallest unit of 4 K pages. Most redis objects are much smaller than 4 K, so there may be multiple redis objects on an OS page. In addition, the set object type of redis, such as list, may exist on multiple OS pages. In the end, only 10% keys may be frequently accessed, but all OS pages will be regarded as active by the OS, so that the OS will switch pages only when the memory is actually exhausted.

2. Compared with OS. Redis can compress the objects exchanged to the disk. Objects saved to the disk can remove pointer and object metadata information. Generally, the compressed object is 10 times smaller than the object in the memory. In this way, redis VMS can perform much less Io operations than OS VMS.

The following are VM-related configurations
VM-enabled yes # enable the VM function
VM-Swap-file/tmp/redis. swap # the file path for storing the exchanged value/tmp/redis. swap
VM-max-memory 1000000 # maximum memory used by redis. After the upper limit is exceeded, redis starts to exchange value to the disk file.
VM-page-size 32 #32 bytes per page
VM-pages 134217728 # maximum number of pages used in files, the size of the swap file = VM-page-size * VM-pages
VM-max-threads 4 # Number of worker threads used to execute value object switching. 0 indicates no working thread is used (described later)

Redis VM is designed to ensure the key search speed, only the value is exchanged to the swap file. Therefore, if the memory problem is caused by too many keys with small values, the VM cannot solve the problem. Like OS, redis also exchanges objects by page. Redis requires that only one object can be saved on the same page. However, an object can be saved on multiple pages. No value is exchanged before the memory used by redis exceeds the VM-max-memory. When the maximum memory limit is exceeded, redis selects older objects. If the two objects are the same, the larger objects will be exchanged first. The precise formula is swappability = age * log (size_in_memory ). For VM-page-size settings, set the page size to the size that can accommodate most objects according to your application. If the disk space is too large, the disk space will be wasted. If the disk space is too small, the swap file will be broken. For each page in the swap file, redis will correspond to a 1-bit value in the memory to record the idle status of the page. As shown in the preceding configuration, the number of pages (Vm-pages 134217728) occupies 16 MB of memory to record the idle page status. VM-max-threads indicates the number of threads used for switching tasks. If it is greater than 0, we recommend that you set the number of CPU cores to the server. If the value is 0, the switching process is performed in the main thread.

After the parameter configuration is discussed, let's briefly introduce how the VM works,
When VM-max-threads is set to 0 (blocking VM)
Swap out
When the main thread regularly checks and finds that the maximum memory is exceeded, the selected object is saved to the swap file and the memory occupied by the object is released, this process repeats until the following conditions are met
1. The maximum memory usage is as follows:
2. The swap file is full.
3. Almost all objects are switched to the disk.

Inbound
When a client request value is exchanged for a key. The main thread will load the corresponding value object from the file in blocking mode, and the client will be blocked during loading. Then process the client request

When VM-max-threads is greater than 0 (threaded VM)
Swap out
When the main thread detects that the memory usage exceeds the maximum limit, it puts the selected object information to be exchanged in a queue for processing by the working thread background, and the main thread continues to process client requests.

Inbound
If the key requested by a client is swapped out, the main thread first blocks the client that issues the command, and then places the information of the loaded object in a queue for the worker thread to load. The worker thread notifies the main thread after loading. The main thread then executes the client command. This method only blocks the client whose request value is exchanged with the key.

In general, the blocking VM method has better performance, because thread synchronization is not required, and thread creation and recovery are congested. However, responsiveness is also sacrificed. Threaded VM mode the main thread does not block the disk Io, so it is more responsive. We recommend using blocking VM if our applications do not frequently change in and out, and do not care much about latency. For details about redis Vm, refer to the following link.
Http://antirez.com/post/redis-virtual-memory-story.html
Http://redis.io/topics/internals-vm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.