Bird
Raised Fist0
Redisquery~20 mins

PERSIST to remove expiry in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Persist Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
What is the output of the PERSIST command?
Given a Redis key session_token with an expiry set, what will be the output of the command PERSIST session_token if the key exists and has an expiry?
Redis
SET session_token abc123
EXPIRE session_token 300
PERSIST session_token
A0
BError: wrong number of arguments
Cnil
D1
Attempts:
2 left
💡 Hint
PERSIST removes the expiry from a key if it exists and has a timeout.
❓ query_result
intermediate
1:30remaining
What happens when PERSIST is called on a key without expiry?
If a Redis key user:100 exists but has no expiry set, what will PERSIST user:100 return?
Redis
SET user:100 "John Doe"
PERSIST user:100
AError: key does not exist
B1
C0
Dnil
Attempts:
2 left
💡 Hint
PERSIST only removes expiry if one exists.
📝 Syntax
advanced
1:30remaining
Which PERSIST command syntax is correct?
Identify the correct syntax to remove the expiry from a Redis key named cache_data.
APERSIST cache_data
BPERSIST(cache_data)
CPERSIST 'cache_data'
DPERSIST cache_data EXPIRE
Attempts:
2 left
💡 Hint
Redis commands do not use parentheses or extra keywords for PERSIST.
🧠 Conceptual
advanced
2:00remaining
Why use PERSIST instead of deleting and recreating a key?
Which of the following is the best reason to use PERSIST to remove expiry from a key instead of deleting and recreating it?
ADeleting and recreating is faster and safer.
BPERSIST preserves the key's value and metadata without interruption.
CPERSIST automatically backs up the key to disk.
DDeleting a key does not remove its expiry.
Attempts:
2 left
💡 Hint
Think about what happens to the key's data and connections.
🔧 Debug
expert
2:00remaining
Why does PERSIST return 0 unexpectedly?
You run PERSIST mykey but it returns 0 even though you expect the key to have an expiry. Which is the most likely cause?
AThe key <code>mykey</code> exists but has no expiry set.
BThe Redis server is down.
CThe key <code>mykey</code> does not exist in Redis.
DPERSIST command is deprecated and no longer works.
Attempts:
2 left
💡 Hint
PERSIST returns 0 if no expiry was removed.

Practice

(1/5)
1. What does the Redis command PERSIST key do?
easy
A. Renames the key to 'persisted_key'
B. Sets a new expiration time on the key
C. Deletes the key immediately
D. Removes the expiration time from the key, making it permanent

Solution

  1. Step 1: Understand the purpose of PERSIST

    The PERSIST command removes any expiration time set on a Redis key, so the key no longer expires.
  2. Step 2: Compare with other commands

    Unlike EXPIRE which sets a timeout, PERSIST cancels it. It does not delete or rename the key.
  3. Final Answer:

    Removes the expiration time from the key, making it permanent -> Option D
  4. Quick Check:

    PERSIST removes expiry = Removes the expiration time from the key, making it permanent [OK]
Hint: PERSIST cancels expiry, keeps key forever [OK]
Common Mistakes:
  • Confusing PERSIST with EXPIRE
  • Thinking PERSIST deletes the key
  • Assuming PERSIST renames the key
2. Which of the following is the correct syntax to remove expiry from a Redis key named session123?
easy
A. PERSIST session123
B. REMOVEEXPIRY session123
C. EXPIRE session123 0
D. TTL session123

Solution

  1. Step 1: Recall the correct command for removing expiry

    The Redis command to remove expiry is PERSIST key. So for key 'session123', it is PERSIST session123.
  2. Step 2: Check other options

    REMOVEEXPIRY is not a Redis command. EXPIRE with 0 does not remove expiry but sets it to expire immediately. TTL only checks expiry time.
  3. Final Answer:

    PERSIST session123 -> Option A
  4. Quick Check:

    Correct syntax for removing expiry = PERSIST session123 [OK]
Hint: Use PERSIST key to remove expiry [OK]
Common Mistakes:
  • Using EXPIRE with 0 to remove expiry
  • Trying non-existent commands like REMOVEEXPIRY
  • Confusing TTL with expiry removal
3. Given the following Redis commands executed in order:
SET mykey "hello"
EXPIRE mykey 10
TTL mykey
PERSIST mykey
TTL mykey

What will be the output of the two TTL commands?
medium
A. 10 and -1
B. -1 and 10
C. 10 and 10
D. -1 and -1

Solution

  1. Step 1: Analyze the first TTL after EXPIRE

    After setting key 'mykey' and applying EXPIRE 10, TTL returns the remaining seconds, which is 10.
  2. Step 2: Analyze the second TTL after PERSIST

    PERSIST removes expiry, so TTL returns -1 indicating no expiration.
  3. Final Answer:

    10 and -1 -> Option A
  4. Quick Check:

    TTL before persist = 10, after persist = -1 [OK]
Hint: TTL shows seconds left; PERSIST makes TTL -1 [OK]
Common Mistakes:
  • Expecting TTL to stay 10 after PERSIST
  • Confusing TTL -1 meaning
  • Thinking PERSIST sets TTL to 0
4. You run the command PERSIST mykey but it returns 0. What is the most likely reason?
medium
A. The key does not exist
B. The key has no expiry set
C. The key is already persistent
D. The command syntax is incorrect

Solution

  1. Step 1: Understand PERSIST return values

    PERSIST returns 1 if expiry was removed, 0 if key had no expiry or key does not exist.
  2. Step 2: Differentiate between no expiry and missing key

    If key does not exist, PERSIST returns 0, but usually you check key existence first. If key exists but no expiry, PERSIST returns 0 because nothing to remove.
  3. Final Answer:

    The key has no expiry set -> Option B
  4. Quick Check:

    PERSIST returns 0 if no expiry to remove [OK]
Hint: PERSIST returns 0 if no expiry on key [OK]
Common Mistakes:
  • Assuming 0 means syntax error
  • Confusing no expiry with key missing
  • Thinking 0 means key deleted
5. You have a Redis key cache:item with an expiry of 30 seconds. You want to keep it permanently only if its current TTL is more than 10 seconds. Which sequence of commands achieves this safely?
hard
A. Run EXPIRE cache:item 0 to remove expiry
B. Run PERSIST cache:item directly without checking TTL
C. Use TTL cache:item, then if result > 10, run PERSIST cache:item
D. Rename the key to a new key without expiry

Solution

  1. Step 1: Check current TTL of the key

    Use TTL cache:item to get remaining expiry time in seconds.
  2. Step 2: Conditionally remove expiry

    If TTL is more than 10, run PERSIST cache:item to remove expiry and keep key permanently.
  3. Step 3: Avoid wrong commands

    Running PERSIST without checking TTL removes expiry regardless. EXPIRE with 0 does not remove expiry but sets immediate expiry. Renaming does not affect expiry.
  4. Final Answer:

    Use TTL, then if > 10, run PERSIST -> Option C
  5. Quick Check:

    Check TTL before PERSIST to keep key only if TTL > 10 [OK]
Hint: Check TTL first, then PERSIST if TTL > 10 [OK]
Common Mistakes:
  • Running PERSIST blindly without TTL check
  • Using EXPIRE 0 to remove expiry
  • Thinking renaming removes expiry
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