Bird
Raised Fist0
Redisquery~10 mins

Maxmemory setting in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Maxmemory setting
Start Redis Server
↓
Check maxmemory config
↓
Is maxmemory set?
No→Use unlimited memory
Yes↓
Limit memory usage to maxmemory
↓
When memory usage > maxmemory
↓
Apply eviction policy
↓
Free memory
↓
Continue running
Redis starts and checks if maxmemory is set. If yes, it limits memory usage and applies eviction when needed.
Execution Sample
Redis
CONFIG SET maxmemory 100mb
INFO memory
-- simulate memory usage --
-- memory > 100mb triggers eviction --
Set maxmemory to 100mb and observe memory usage and eviction when limit exceeded.
Execution Table
StepActionMemory Usagemaxmemory SettingEviction TriggeredResult
1Start Redis server0mbNot setNoServer runs with unlimited memory
2Set maxmemory to 100mb0mb100mbNoMemory limit applied
3Memory usage grows to 80mb80mb100mbNoNo eviction needed
4Memory usage grows to 105mb105mb100mbYesEviction policy activated
5Eviction frees 20mb85mb100mbNoMemory usage below limit
6Memory usage stable at 85mb85mb100mbNoServer continues running
💡 Memory usage is controlled by maxmemory; eviction triggers when usage exceeds limit.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Memory Usage0mb0mb80mb105mb85mb85mb
maxmemoryNot set100mb100mb100mb100mb100mb
Eviction TriggeredNoNoNoYesNoNo
Key Moments - 3 Insights
Why does Redis start without any memory limit if maxmemory is not set?
As shown in execution_table row 1, if maxmemory is not set, Redis uses unlimited memory by default.
When exactly does Redis trigger eviction?
Eviction triggers when memory usage exceeds maxmemory, as seen in execution_table row 4 where usage is 105mb > 100mb.
Does eviction reduce memory usage below maxmemory immediately?
Yes, eviction frees memory to bring usage below maxmemory, shown in row 5 where usage drops from 105mb to 85mb.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the maxmemory setting after step 2?
ANot set
B100mb
C0mb
DUnlimited
💡 Hint
Check the 'maxmemory Setting' column at step 2 in the execution_table.
At which step does eviction get triggered according to the execution_table?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Eviction Triggered' column to find when it changes to 'Yes'.
If maxmemory was set to 200mb instead of 100mb, what would happen at step 4?
AMemory usage would be below maxmemory, no eviction
BEviction would trigger
CRedis would crash
DMemory usage would reset to 0
💡 Hint
Compare memory usage at step 4 (105mb) with a hypothetical maxmemory of 200mb.
Concept Snapshot
Maxmemory setting limits Redis memory use.
If memory usage exceeds maxmemory, eviction frees memory.
Without maxmemory, Redis uses unlimited memory.
Set maxmemory with CONFIG SET maxmemory <bytes>.
Eviction policy controls which keys to remove.
Helps prevent Redis from using too much RAM.
Full Transcript
Redis starts and checks if maxmemory is set. If not set, Redis uses unlimited memory. When maxmemory is set, Redis limits memory usage to that value. If memory usage grows beyond maxmemory, Redis triggers eviction to free memory. Eviction reduces memory usage below the limit so Redis can continue running smoothly. This process repeats as memory usage changes. Setting maxmemory helps control Redis memory consumption and avoid crashes.

Practice

(1/5)
1. What does the maxmemory setting in Redis control?
easy
A. The maximum amount of memory Redis can use
B. The maximum number of clients connected
C. The maximum size of a single key
D. The maximum number of commands per second

Solution

  1. Step 1: Understand the purpose of maxmemory

    The maxmemory setting limits how much memory Redis can use to avoid overloading the server.
  2. 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.
  3. Final Answer:

    The maximum amount of memory Redis can use -> Option A
  4. Quick Check:

    maxmemory limits Redis memory use [OK]
Hint: maxmemory always limits Redis memory size [OK]
Common Mistakes:
  • Confusing maxmemory with client limits
  • Thinking maxmemory limits key size
  • Assuming maxmemory limits command rate
2. Which of the following is the correct syntax to set maxmemory to 512 megabytes using CONFIG SET in Redis?
easy
A. CONFIG SET maxmemory 512mb
B. CONFIG SET maxmemory 512MB
C. CONFIG SET maxmemory 512m
D. CONFIG SET maxmemory 512

Solution

  1. Step 1: Recall Redis size suffix format

    Redis accepts size suffixes like KB, MB, GB in uppercase for memory settings.
  2. 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.
  3. Final Answer:

    CONFIG SET maxmemory 512MB -> Option B
  4. Quick Check:

    Use uppercase suffixes like MB for megabytes [OK]
Hint: Use uppercase suffixes like MB, GB for sizes [OK]
Common Mistakes:
  • Using lowercase suffixes like mb
  • Using incomplete suffix like m
  • Omitting suffix and expecting megabytes
3. Given the command CONFIG SET maxmemory 1GB executed on a Redis server, what will be the effect?
medium
A. Redis will limit its memory usage to 1 gigabyte
B. Redis will allow unlimited memory usage
C. Redis will limit memory to 1 byte
D. Redis will throw a syntax error

Solution

  1. Step 1: Interpret the command meaning

    The command sets maxmemory to 1GB, meaning Redis should limit memory use to 1 gigabyte.
  2. Step 2: Validate the command syntax and effect

    The syntax is correct and Redis supports 'GB' suffix, so it will enforce the memory limit.
  3. Final Answer:

    Redis will limit its memory usage to 1 gigabyte -> Option A
  4. Quick Check:

    CONFIG SET maxmemory 1GB limits memory to 1GB [OK]
Hint: CONFIG SET maxmemory with GB limits memory to gigabytes [OK]
Common Mistakes:
  • Thinking it sets unlimited memory
  • Confusing bytes with gigabytes
  • Expecting syntax error from correct command
4. You run CONFIG SET maxmemory 100mb on Redis but it returns an error. What is the likely cause?
medium
A. Redis does not support changing maxmemory at runtime
B. maxmemory must be set in bytes only, no suffix allowed
C. The value 100mb is too large for Redis
D. The suffix 'mb' is lowercase and invalid

Solution

  1. Step 1: Check Redis suffix case sensitivity

    Redis requires uppercase suffixes like MB, GB for memory sizes.
  2. Step 2: Analyze the error cause

    Using lowercase 'mb' causes syntax error; changing to 'MB' fixes it.
  3. Final Answer:

    The suffix 'mb' is lowercase and invalid -> Option D
  4. Quick Check:

    Use uppercase suffixes for memory sizes [OK]
Hint: Always use uppercase suffixes like MB, GB [OK]
Common Mistakes:
  • Assuming maxmemory can't be changed at runtime
  • Using lowercase suffix causing errors
  • Thinking maxmemory only accepts bytes
5. You want Redis to use at most 2 gigabytes of memory and automatically evict keys when the limit is reached. Which two commands should you run?
hard
A. CONFIG SET maxmemory 2gb and CONFIG SET maxmemory-policy allkeys-random
B. CONFIG SET maxmemory 2GB and CONFIG SET maxmemory-policy noeviction
C. CONFIG SET maxmemory 2GB and CONFIG SET maxmemory-policy allkeys-lru
D. CONFIG SET maxmemory 2048 and CONFIG SET maxmemory-policy volatile-lru

Solution

  1. Step 1: Set maxmemory to 2 gigabytes correctly

    Use CONFIG SET maxmemory 2GB with uppercase suffix for 2 gigabytes.
  2. Step 2: Choose eviction policy that evicts keys automatically

    allkeys-lru evicts least recently used keys from all keys, enabling automatic eviction.
  3. 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.
  4. Final Answer:

    CONFIG SET maxmemory 2GB and CONFIG SET maxmemory-policy allkeys-lru -> Option C
  5. Quick Check:

    Set maxmemory + allkeys-lru for eviction [OK]
Hint: Use uppercase GB and allkeys-lru for eviction [OK]
Common Mistakes:
  • Using lowercase suffix like '2gb'
  • Choosing noeviction policy
  • Setting maxmemory without suffix
  • Using volatile-lru when all keys need eviction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard