Maxmemory setting in Redis - Time & Space Complexity
When Redis reaches its memory limit, it uses the maxmemory setting to decide what to do next.
We want to understand the time complexity of memory management as data size increases.
Analyze the time complexity of Redis handling maxmemory eviction with the allkeys-lru policy.
CONFIG SET maxmemory 100mb
CONFIG SET maxmemory-policy allkeys-lru
SET key1 value1
SET key2 value2
...
SET keyN valueN
This code sets a memory limit and an eviction policy that removes least recently used keys when memory is full.
Redis samples a small, fixed number of keys to approximate the least recently used key for eviction.
- Primary operation: Sampling and evaluating a fixed number of keys for LRU approximation.
- How many times: A constant number of samples (default 5), independent of total keys.
As the number of keys grows, Redis still samples the same fixed number of keys, keeping eviction time constant.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Sample fixed # (e.g., 5) keys |
| 100 | Sample fixed # (e.g., 5) keys |
| 1000 | Sample fixed # (e.g., 5) keys |
Pattern observation: The time to find a key to evict is constant regardless of the number of keys.
Time Complexity: O(1)
This means the time to find a key to evict is constant, independent of the number of keys stored.
[X] Wrong: "Redis scans all keys linearly to find the LRU key."
[OK] Correct: Redis uses sampling of a fixed small number of keys for approximation, keeping it O(1).
Understanding how Redis manages memory helps you explain real-world system behavior and performance under load.
"What if we changed the eviction policy from allkeys-lru to noeviction? How would the time complexity change?"
Practice
maxmemory setting in Redis control?Solution
Step 1: Understand the purpose of maxmemory
Themaxmemorysetting limits how much memory Redis can use to avoid overloading the server.Step 2: Compare options with this purpose
Only the maximum amount of memory Redis can use matches this description, others relate to clients, keys, or commands, which are unrelated.Final Answer:
The maximum amount of memory Redis can use -> Option AQuick Check:
maxmemory limits Redis memory use [OK]
- Confusing maxmemory with client limits
- Thinking maxmemory limits key size
- Assuming maxmemory limits command rate
CONFIG SET in Redis?Solution
Step 1: Recall Redis size suffix format
Redis accepts size suffixes like KB, MB, GB in uppercase for memory settings.Step 2: Check each option's suffix
CONFIG SET maxmemory 512MB uses 'MB' uppercase which is correct; CONFIG SET maxmemory 512mb uses lowercase 'mb' which is invalid; CONFIG SET maxmemory 512m uses 'm' which is not recognized; CONFIG SET maxmemory 512 has no suffix, so it's bytes, not megabytes.Final Answer:
CONFIG SET maxmemory 512MB -> Option BQuick Check:
Use uppercase suffixes like MB for megabytes [OK]
- Using lowercase suffixes like mb
- Using incomplete suffix like m
- Omitting suffix and expecting megabytes
CONFIG SET maxmemory 1GB executed on a Redis server, what will be the effect?Solution
Step 1: Interpret the command meaning
The command sets maxmemory to 1GB, meaning Redis should limit memory use to 1 gigabyte.Step 2: Validate the command syntax and effect
The syntax is correct and Redis supports 'GB' suffix, so it will enforce the memory limit.Final Answer:
Redis will limit its memory usage to 1 gigabyte -> Option AQuick Check:
CONFIG SET maxmemory 1GB limits memory to 1GB [OK]
- Thinking it sets unlimited memory
- Confusing bytes with gigabytes
- Expecting syntax error from correct command
CONFIG SET maxmemory 100mb on Redis but it returns an error. What is the likely cause?Solution
Step 1: Check Redis suffix case sensitivity
Redis requires uppercase suffixes like MB, GB for memory sizes.Step 2: Analyze the error cause
Using lowercase 'mb' causes syntax error; changing to 'MB' fixes it.Final Answer:
The suffix 'mb' is lowercase and invalid -> Option DQuick Check:
Use uppercase suffixes for memory sizes [OK]
- Assuming maxmemory can't be changed at runtime
- Using lowercase suffix causing errors
- Thinking maxmemory only accepts bytes
Solution
Step 1: Set maxmemory to 2 gigabytes correctly
UseCONFIG SET maxmemory 2GBwith uppercase suffix for 2 gigabytes.Step 2: Choose eviction policy that evicts keys automatically
allkeys-lruevicts least recently used keys from all keys, enabling automatic eviction.Step 3: Verify other options
Lowercase '2gb' with allkeys-random uses invalid suffix and random eviction; 2GB with noeviction disables eviction; 2048 (bytes, not GB) with volatile-lru limits to tiny memory and only evicts expiring keys.Final Answer:
CONFIG SET maxmemory 2GB and CONFIG SET maxmemory-policy allkeys-lru -> Option CQuick Check:
Set maxmemory + allkeys-lru for eviction [OK]
- Using lowercase suffix like '2gb'
- Choosing noeviction policy
- Setting maxmemory without suffix
- Using volatile-lru when all keys need eviction
Ready to prove it?
Pass the test and this pattern moves unknown
