Understanding Redis Virtual Memory (VM): Mechanism, Config & Best Practices
This article explains Redis's virtual memory (VM) feature, how it swaps cold data to disk while keeping keys in memory, details configuration parameters, describes the two threading models for data swap-in/out, and highlights why Redis's custom VM implementation boosts performance.
Redis's source code contains many excellent practices worth studying. As a popular open‑source project, it attracts many developers and interviewers, driving deeper industry discussion.
Redis VM Mechanism
Redis's virtual memory (VM) temporarily moves infrequently accessed (cold) data from RAM to disk, freeing memory for hot data. VM enables hot data to stay in memory while cold data resides on disk, preventing performance degradation due to memory shortage. Redis increases capacity either by sharding data across multiple servers or by swapping cold data to disk.
Note: Redis implements its own swap instead of using the OS swap.
Redis swaps only the value, keeping all keys in memory, making it ideal for scenarios with small keys and large values. If keys are large and values small, VM may not be suitable.
VM Configuration
VM behavior is controlled via redis.conf parameters:
# Enable VM
vm-enabled yes
# Path for swapped values
vm-swap-file /tmp/redis.swap
# Memory threshold to start swapping
vm-max-memory 1000000
# Page size in bytes
vm-page-size 32
# Maximum number of pages to swap
vm-pages 13417728
# Number of worker threads (0 = main thread, otherwise CPU cores)
vm-max-threads 4Each data page stores a single object; an object may span multiple pages. When memory usage stays below vm-max-memory, no swapping occurs. Once the limit is exceeded, Redis evicts the oldest objects (preferring larger ones if ages match) to disk, conserving memory.
The vm-page-size should match the typical value size: too small causes a value to occupy multiple pages, too large wastes space.
VM Working Mechanism
Redis supports two threading models for VM operations:
1. vm-max-threads = 0 (single‑threaded)
Data swap‑out: The main thread periodically checks memory usage; if it exceeds the limit, it blocks, selects objects, writes them to the swap file, and frees memory. This repeats until memory falls below the limit, the swap file reaches its page limit, or almost all objects are swapped.
Memory usage drops below the limit.
Swap file reaches its page limit.
Nearly all objects are swapped.
Data swap‑in: When a client requests a key whose value is on disk, the main thread blocks all clients, loads the value from the swap file, then processes the request. This blocks all clients.
2. vm-max-threads > 0 (multi‑threaded)
Data swap‑out: Upon memory overflow, the main thread queues selected objects for background worker threads, allowing it to continue serving other clients.
Data swap‑in: When a client needs a swapped‑out value, the main thread blocks that client, queues the load task to a worker thread, and continues handling other clients. After loading, the worker notifies the main thread, which then resumes the blocked client. This blocks only the requesting client.
Conclusion
Redis implements its own VM mechanism instead of relying on the operating system's swap, eliminating extra data movement overhead and contributing to Redis's high performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
