Skip to content

Commit c2ef202

Browse files
Added explanation of maxmemory for replication. (#194)
Added how to monitor eviction The change closes [#166](#166) --------- Signed-off-by: Anastasia Alexadrova <[email protected]> Signed-off-by: Viktor Söderqvist <[email protected]> Co-authored-by: Viktor Söderqvist <[email protected]>
1 parent 07968b7 commit c2ef202

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

topics/lru-cache.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,29 @@ following directive inside the `valkey.conf` file:
2626
Setting `maxmemory` to zero results into no memory limits. This is the
2727
default behavior for 64 bit systems, while 32 bit systems use an implicit
2828
memory limit of 3GB.
29-
3029
When the specified amount of memory is reached, how **eviction policies** are configured determines the default behavior.
3130
Valkey can return errors for commands that could result in more memory
3231
being used, or it can evict some old data to return back to the
3332
specified limit every time new data is added.
3433

34+
### Considerations for `maxmemory` when using replication
35+
36+
If you have set up replication, Valkey needs some RAM as a buffer to send data to the replicas and AOF files. This memory is not included in the used memory count that is compared against the `maxmemory` to trigger eviction.
37+
38+
The reason for that is that the key eviction process itself generates some changes that need to be added to the replication and AOF buffers. If these buffers were counted for the key eviction, this would result in a loop where a freed memory would immediately be used up by these updates causing more keys to be deleted repeatedly until the database is empty.
39+
40+
For Valkey with replication configured, it's recommended to set the `maxmemory` value lower than for a single instance without replication. This way you ensure there's enough memory for AOF and replication buffers, and other processes.
41+
42+
You can estimate how much memory is used by the replication and AOF buffers using the `mem_not_counted_for_evict` value of the INFO memory command output.
43+
3544
## Eviction policies
3645

3746
The exact behavior Valkey follows when the `maxmemory` limit is reached is
3847
configured using the `maxmemory-policy` configuration directive.
3948

4049
The following policies are available:
4150

42-
* **noeviction**: New values arent saved when memory limit is reached. When a database uses replication, this applies to the primary database
51+
* **noeviction**: New values aren't saved when memory limit is reached. When a database uses replication, this applies to the primary database
4352
* **allkeys-lru**: Keeps most recently used keys; removes least recently used (LRU) keys
4453
* **allkeys-lfu**: Keeps frequently used keys; removes least frequently used (LFU) keys
4554
* **volatile-lru**: Removes least recently used keys with a time-to-live (TTL) set.
@@ -81,6 +90,49 @@ So we continuously cross the boundaries of the memory limit, by going over it, a
8190

8291
If a command results in a lot of memory being used (like a big set intersection stored into a new key) for some time, the memory limit can be surpassed by a noticeable amount.
8392

93+
## Monitor eviction
94+
95+
To monitor the point when Valkey starts to evict data, use the `INFO MEMORY` command. The following fields provide the information about the memory usage and the condition to trigger key eviction:
96+
97+
* `used_memory`: The total number of bytes that the server allocated for storing data. It is the sum of the `used_memory_overhead` and the `used_memory_dataset` outputs.
98+
* `mem_not_counted_for_evict`: The amount of memory not counted for eviction. This includes the replication buffer and AOF buffer.
99+
100+
Thus, the memory usage to trigger eviction is calculated as follows:
101+
102+
```
103+
used_memory - mem_not_counted_for_evict > maxmemory
104+
```
105+
106+
Let's see how this works in practice.
107+
108+
Consider the following INFO memory output:
109+
110+
```
111+
# Memory
112+
used_memory:12498952
113+
...
114+
maxmemory:10737418240
115+
...
116+
mem_not_counted_for_evict:12336
117+
...
118+
```
119+
120+
In this example, Valkey will not start data eviction because the actual memory usage is `12498952 - 12336 = 12486616` which is considerably less than `maxmemory`.
121+
122+
The following example shows that we're nearing eviction:
123+
124+
```
125+
# Memory
126+
used_memory:12498952
127+
...
128+
maxmemory:12500000
129+
...
130+
mem_not_counted_for_evict:12336
131+
```
132+
133+
Once eviction happens, additional information is available through the `INFO STATS` metrics. The `total_eviction_exceeded_time` metric shows the total time in milliseconds that `used_memory` exceeded `maxmemory`.
134+
135+
84136
## Approximated LRU algorithm
85137

86138
Valkey LRU algorithm is not an exact implementation. This means that Valkey is

0 commit comments

Comments
 (0)