Bird
Raised Fist0
Redisquery~10 mins

Why memory optimization matters in Redis - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a key with an expiration time in Redis.

Redis
SET mykey myvalue EX [1]
Drag options to blanks, or click blank then click option'
ADEL
Bmyvalue
C60
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Using a command like GET instead of a number for expiration.
Forgetting to specify the expiration time after EX.
2fill in blank
medium

Complete the command to check the memory usage of a key in Redis.

Redis
MEMORY [1] mykey
Drag options to blanks, or click blank then click option'
AUSAGE
BGET
CDEL
DSET
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET or DEL instead of USAGE.
Not specifying the key after the command.
3fill in blank
hard

Fix the error in the command to delete all keys matching a pattern.

Redis
KEYS [1] | xargs [2]
Drag options to blanks, or click blank then click option'
A*
BDEL
CGET
DSET
Attempts:
3 left
💡 Hint
Common Mistakes
Using DEL, GET, or SET as the pattern instead of '*'.
Using xargs with GET or SET instead of DEL.
Not using a wildcard pattern to match keys.
4fill in blank
hard

Fill both blanks to create a Redis command that sets a key only if it does not exist and expires after 120 seconds.

Redis
SET mykey myvalue [1] [2] 120
Drag options to blanks, or click blank then click option'
ANX
BXX
CEX
DPX
Attempts:
3 left
💡 Hint
Common Mistakes
Using XX instead of NX, which sets only if the key exists.
Using PX instead of EX, which sets expiration in milliseconds.
5fill in blank
hard

Fill all three blanks to create a Redis command that sets a counter key with expiration in milliseconds.

Redis
SET [1] 1 [2] [3]
Drag options to blanks, or click blank then click option'
Amycounter
BPX
C60000
DEX
Attempts:
3 left
💡 Hint
Common Mistakes
Using EX instead of PX for milliseconds.
Not specifying the key name.
Wrong expiration time value.

Practice

(1/5)
1. Why is memory optimization important in Redis?
easy
A. It makes Redis use more CPU power.
B. It increases network traffic.
C. It helps Redis run faster and store more data efficiently.
D. It disables data expiration features.

Solution

  1. Step 1: Understand Redis memory usage

    Redis stores data in memory, so efficient use means faster access and more data storage.
  2. Step 2: Identify benefits of optimization

    Optimizing memory reduces waste and improves performance, unlike increasing CPU or network load.
  3. Final Answer:

    It helps Redis run faster and store more data efficiently. -> Option C
  4. Quick Check:

    Memory optimization = better speed and capacity [OK]
Hint: Memory optimization means faster and bigger data storage [OK]
Common Mistakes:
  • Confusing memory optimization with CPU usage
  • Thinking it increases network traffic
  • Believing it disables expiration
2. Which Redis command helps limit memory usage by setting a maximum memory size?
easy
A. GET key
B. MEMORY USAGE key
C. EXPIRE key seconds
D. CONFIG SET maxmemory <bytes>

Solution

  1. Step 1: Identify commands related to memory limits

    CONFIG SET maxmemory sets the max memory Redis can use.
  2. Step 2: Differentiate from other commands

    MEMORY USAGE checks usage, EXPIRE sets expiration, GET retrieves data, none limit memory size.
  3. Final Answer:

    CONFIG SET maxmemory <bytes> -> Option D
  4. Quick Check:

    Set max memory with CONFIG SET maxmemory [OK]
Hint: Use CONFIG SET maxmemory to limit Redis memory [OK]
Common Mistakes:
  • Confusing MEMORY USAGE with setting limits
  • Using EXPIRE to limit memory size
  • Choosing GET which only reads data
3. Given this Redis memory info snippet:
used_memory:1048576
maxmemory:2097152

What happens if Redis tries to add 2MB more data?
medium
A. Redis will reject the new data because it exceeds maxmemory.
B. Redis will delete expired keys to free space before adding data.
C. Redis will automatically increase maxmemory to fit data.
D. Redis will accept the data without any issue.

Solution

  1. Step 1: Compare used_memory and maxmemory values

    Used memory is 1MB, maxmemory is 2MB, adding 2MB exceeds limit.
  2. Step 2: Understand Redis behavior on exceeding maxmemory

    Redis rejects new data if it exceeds maxmemory unless eviction policies apply, but none mentioned here.
  3. Final Answer:

    Redis will reject the new data because it exceeds maxmemory. -> Option A
  4. Quick Check:

    Adding data > maxmemory causes rejection [OK]
Hint: Adding data beyond maxmemory causes rejection [OK]
Common Mistakes:
  • Assuming Redis auto-increases maxmemory
  • Thinking expired keys are deleted automatically here
  • Believing Redis accepts all data regardless of limits
4. You wrote this Redis command to set a key expiration:
EXPIRE mykey 3600
But the key never expires. What is the likely problem?
medium
A. The expiration time must be set in milliseconds, not seconds.
B. The key 'mykey' does not exist in Redis.
C. Redis does not support key expiration.
D. You must restart Redis for expiration to work.

Solution

  1. Step 1: Check if key exists before expiration

    EXPIRE only works if the key exists; if 'mykey' is missing, expiration fails silently.
  2. Step 2: Verify expiration syntax and Redis features

    EXPIRE uses seconds correctly; Redis supports expiration; no restart needed.
  3. Final Answer:

    The key 'mykey' does not exist in Redis. -> Option B
  4. Quick Check:

    Expiration requires existing key [OK]
Hint: Check key exists before setting expiration [OK]
Common Mistakes:
  • Using milliseconds instead of seconds in EXPIRE
  • Thinking Redis lacks expiration support
  • Believing restart is needed for expiration
5. You want to optimize Redis memory by choosing the best data type for storing user sessions that expire after 30 minutes. Which approach is best?
hard
A. Use strings with EXPIRE set to 1800 seconds for each session.
B. Use hashes without expiration and delete sessions manually.
C. Store sessions as large lists without expiration.
D. Use sets with no expiration and rely on periodic cleanup.

Solution

  1. Step 1: Identify data type suitable for session storage

    Strings are simple and efficient for session data; hashes or lists add overhead.
  2. Step 2: Apply expiration for automatic memory cleanup

    Setting EXPIRE to 1800 seconds (30 minutes) ensures sessions auto-remove, saving memory.
  3. Final Answer:

    Use strings with EXPIRE set to 1800 seconds for each session. -> Option A
  4. Quick Check:

    Strings + EXPIRE = best memory optimization for sessions [OK]
Hint: Use strings with EXPIRE for session memory efficiency [OK]
Common Mistakes:
  • Using data types without expiration
  • Relying on manual or periodic cleanup
  • Choosing complex types that waste memory
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