Bird
Raised Fist0
Redisquery~10 mins

EXPIRE and TTL for time-to-live in Redis - Interactive Code Practice

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

Complete the code to set a key to expire after 60 seconds.

Redis
EXPIRE mykey [1]
Drag options to blanks, or click blank then click option'
A6
B600
C60
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will delete the key immediately.
Using 600 sets expiration to 10 minutes, not 1 minute.
2fill in blank
medium

Complete the code to check the remaining time to live of a key.

Redis
TTL [1]
Drag options to blanks, or click blank then click option'
Amykey
Bsession
Cexpire
Dtimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Using a command name instead of a key name.
Using a key that does not exist returns -2.
3fill in blank
hard

Fix the error in the command to set expiration to 120 seconds.

Redis
EXPIRE mykey [1]
Drag options to blanks, or click blank then click option'
A1m
B120
Ctwo_minutes
D-120
Attempts:
3 left
💡 Hint
Common Mistakes
Using text instead of a number.
Using negative numbers.
4fill in blank
hard

Fill both blanks to set a key to expire and then check its TTL.

Redis
EXPIRE [1] 30
TTL [2]
Drag options to blanks, or click blank then click option'
Acache
Bsession
Attempts:
3 left
💡 Hint
Common Mistakes
Using different key names in EXPIRE and TTL commands.
Using a key that does not exist.
5fill in blank
hard

Fill all three blanks to set a key expiration, check TTL, and remove expiration.

Redis
EXPIRE [1] 45
TTL [2]
PERSIST [3]
Drag options to blanks, or click blank then click option'
Atempkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using different key names in commands.
Confusing PERSIST with EXPIRE.

Practice

(1/5)
1. What does the Redis command EXPIRE key 60 do?
easy
A. Returns the remaining time to live for the key.
B. Immediately deletes the key named 'key'.
C. Sets the key to expire and be deleted after 60 seconds.
D. Renames the key to '60'.

Solution

  1. Step 1: Understand the EXPIRE command

    The EXPIRE command sets a countdown timer on a key in seconds.
  2. Step 2: Apply the command to the example

    Here, EXPIRE key 60 sets the key to expire after 60 seconds.
  3. Final Answer:

    Sets the key to expire and be deleted after 60 seconds. -> Option C
  4. Quick Check:

    EXPIRE sets countdown = A [OK]
Hint: EXPIRE sets time in seconds before deletion [OK]
Common Mistakes:
  • Thinking EXPIRE deletes immediately
  • Confusing EXPIRE with TTL command
  • Assuming EXPIRE renames keys
2. Which of the following is the correct syntax to check the remaining time to live of a key named session in Redis?
easy
A. TTL session
B. EXPIRE session
C. GETTTL session
D. TIMELEFT session

Solution

  1. Step 1: Identify the command to check TTL

    The Redis command to check remaining time is TTL key.
  2. Step 2: Match the syntax with the key name

    For key session, the correct command is TTL session.
  3. Final Answer:

    TTL session -> Option A
  4. Quick Check:

    TTL command syntax = D [OK]
Hint: TTL shows remaining seconds before expiry [OK]
Common Mistakes:
  • Using EXPIRE to check TTL
  • Assuming GETTTL is a Redis command
  • Using TIMELEFT which does not exist
3. Consider the following Redis commands executed in order:
SET temp "data"
EXPIRE temp 10
TTL temp

What will the TTL temp command return immediately after setting the expire?
medium
A. Error
B. -1
C. 0
D. 10

Solution

  1. Step 1: Set key and expire time

    The key temp is set with value "data" and an expire time of 10 seconds.
  2. Step 2: Check TTL immediately after EXPIRE

    Immediately after setting expire, TTL returns the remaining seconds, which is 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    TTL after EXPIRE = 10 [OK]
Hint: TTL returns seconds left right after EXPIRE [OK]
Common Mistakes:
  • Expecting TTL to return -1 (no expire)
  • Thinking TTL returns 0 immediately
  • Assuming TTL causes an error if key exists
4. You run the following commands:
SET user "Alice"
EXPIRE user -5
TTL user

What is the result of TTL user and why?
medium
A. -2 because key is deleted immediately
B. Error due to invalid expire time
C. -1 because negative expire removes expiration
D. 5 because negative is treated as positive

Solution

  1. Step 1: Understand EXPIRE with negative time

    Setting EXPIRE with a negative value deletes the key immediately.
  2. Step 2: Check TTL after immediate deletion

    TTL returns -2 when the key does not exist.
  3. Final Answer:

    -2 because key is deleted immediately -> Option A
  4. Quick Check:

    Negative EXPIRE deletes key, TTL = -2 [OK]
Hint: Negative EXPIRE deletes key, TTL returns -2 [OK]
Common Mistakes:
  • Thinking negative expire removes expiration but keeps key
  • Expecting TTL to return -1 for no expire
  • Assuming negative expire causes error
5. You want to set a key cache with a value and ensure it expires in 30 seconds only if it does not already have an expiration. Which Redis command sequence achieves this safely?
hard
A. SET cache "value" NX EX 30
B. SET cache "value" TTL cache EXPIRE cache 30 if TTL is -1
C. SET cache "value" EX 30 EXPIRE cache 30
D. SET cache "value" EXPIRE cache 30

Solution

  1. Step 1: Understand the requirement

    We want to set the key and only add expiration if none exists (TTL = -1).
  2. Step 2: Analyze options

    SET cache "value" TTL cache EXPIRE cache 30 if TTL is -1 checks TTL first, then sets EXPIRE only if TTL is -1, ensuring no overwrite of existing expiration.
  3. Final Answer:

    SET cache "value" TTL cache EXPIRE cache 30 if TTL is -1 -> Option B
  4. Quick Check:

    Check TTL before EXPIRE to avoid overwriting [OK]
Hint: Check TTL before EXPIRE to avoid overwriting [OK]
Common Mistakes:
  • Using NX option which only affects key existence
  • Setting EXPIRE twice unnecessarily
  • Not checking TTL before setting EXPIRE
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