Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure redis virtual memory

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to configure redis virtual memory". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First of all, it is explained that the virtual memory of redis is not the same as that of os, but the idea and purpose are the same. It is to temporarily swap infrequently accessed data from memory to disk, thus freeing up valuable memory space for other data that needs to be accessed. Especially for in-memory databases such as redis, memory is always insufficient. Except that data can be split into multiple redis server. Another way to increase database capacity is to use vm to exchange data on disks that are not accessed frequently. If a small part of our stored data is always accessed frequently, and most of the data is rarely accessed, it is true that only a small number of users are always active for the site. When a small amount of data is accessed frequently, using vm not only increases the capacity of a single redis server database, but also does not have much impact on performance.

Instead of using the virtual memory mechanism provided by os, redis implements its own virtual memory mechanism in user mode.

There are two main reasons.

The virtual memory of 1.os is swapped for the smallest unit of 4k pages. Most objects in redis are much smaller than 4k, so there may be multiple redis objects on an os page. In addition, the aggregate object types of redis, such as list,set, may exist on multiple os pages. In the end, only 10%key will be visited frequently, but all os pages will be considered active by os, so that os will exchange pages only when memory is really exhausted.

two。 Compared to the exchange mode of os. Redis can compress objects swapped to disk, and objects saved to disk can remove pointer and object metadata information. Generally, the compressed object is 10 times smaller than the object in memory. In this way, redis's vm can do a lot less io operations than os vm.

Here are the configurations related to vm

Vm-enabled yes # enable vm function

Vm-swap-file / tmp/redis.swap # swapped value saved file path / tmp/redis.swap

The maximum memory limit used by vm-max-memory 1000000 # redis. After the upper limit is exceeded, redis begins to swap value to disk files.

Vm-page-size 32 # 32 bytes per page size

Vm-pages 134217728 # the maximum number of pages used in the file, the size of the swap file = vm-page-size * vm-pages

The number of worker threads used by vm-max-threads 4 # to perform value object swapping in and out. 0 indicates that worker threads are not used (described later)

Redis's vm is designed to ensure the speed of key lookup, only to swap value into swap files. So if the memory problem is caused by too many key with very small value, then vm can't solve it. Like os, redis exchanges objects by page. Redis stipulates that only one object can be saved on the same page. But an object can be saved on multiple pages. No value will be swapped until redis uses more memory than vm-max-memory. When the maximum memory limit is exceeded, redis selects older objects. If two objects are the same, the larger objects will be exchanged first, the exact formula swappability = age*log (size_in_memory). For vm-page-size settings, you should set the size of the page to accommodate most objects according to your own application. Too large will waste disk space, and too small will cause fragments in the swap file. For each page in the swap file, redis corresponds to a 1-bit value in memory to record the idle status of the page. So the number of pages in the configuration above (vm-pages 134217728) takes up 16m of memory to record the idle state of the page. Vm-max-threads represents the number of threads used for swapping tasks. If greater than 0, it is recommended to set the number of cpu core as the server. If it is 0, the exchange process takes place on the main thread.

This is the end of the content of "how to configure redis virtual memory". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report