search
HomeBackend DevelopmentC#.Net TutorialRedis 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)!


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
C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

Demystifying C# .NET: An Overview for BeginnersDemystifying C# .NET: An Overview for BeginnersApr 20, 2025 am 12:11 AM

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software