The EXPIRE command sets a time-to-live (TTL) in seconds on a key. After this time, the key will be automatically deleted.
Click to reveal answer
beginner
How can you check the remaining time before a Redis key expires?
Use the TTL command to see how many seconds remain before the key expires. It returns -1 if the key has no expiration and -2 if the key does not exist.
Click to reveal answer
intermediate
What happens if you set EXPIRE on a key that does not exist?
The EXPIRE command will return 0, meaning it did not set the expiration because the key does not exist.
Click to reveal answer
intermediate
Can you remove the expiration from a Redis key? If yes, how?
Yes, use the PERSIST command to remove the expiration and make the key permanent.
Click to reveal answer
advanced
What is the difference between EXPIRE and PEXPIRE in Redis?
EXPIRE sets the TTL in seconds, while PEXPIRE sets the TTL in milliseconds, allowing more precise expiration times.
Click to reveal answer
What does the TTL command return if a key does not have an expiration?
A-2
B0
C1
D-1
✗ Incorrect
TTL returns -1 when the key exists but has no expiration set.
Which command sets a key to expire after a certain number of seconds?
APERSIST
BTTL
CEXPIRE
DGET
✗ Incorrect
EXPIRE sets the time-to-live in seconds for a key.
If you want to remove the expiration from a key, which command should you use?
APERSIST
BEXPIRE
CTTL
DDEL
✗ Incorrect
PERSIST removes the expiration, making the key permanent.
What does the EXPIRE command return if the key does not exist?
A1
B0
C-1
D-2
✗ Incorrect
EXPIRE returns 0 if the key does not exist.
Which command sets the expiration time in milliseconds?
APEXPIRE
BPTTL
CTTL
DEXPIRE
✗ Incorrect
PEXPIRE sets the expiration time in milliseconds.
Explain how to set and check the expiration time of a key in Redis.
Think about how you tell Redis to delete a key after some time and how to see how much time is left.
You got /4 concepts.
Describe how to remove the expiration from a Redis key and what effect it has.
Consider what happens if you want a key to stay forever after setting an expiration.
You got /3 concepts.
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
Step 1: Understand the EXPIRE command
The EXPIRE command sets a countdown timer on a key in seconds.
Step 2: Apply the command to the example
Here, EXPIRE key 60 sets the key to expire after 60 seconds.
Final Answer:
Sets the key to expire and be deleted after 60 seconds. -> Option C
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
Step 1: Identify the command to check TTL
The Redis command to check remaining time is TTL key.
Step 2: Match the syntax with the key name
For key session, the correct command is TTL session.
Final Answer:
TTL session -> Option A
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
Step 1: Set key and expire time
The key temp is set with value "data" and an expire time of 10 seconds.
Step 2: Check TTL immediately after EXPIRE
Immediately after setting expire, TTL returns the remaining seconds, which is 10.
Final Answer:
10 -> Option D
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
Step 1: Understand EXPIRE with negative time
Setting EXPIRE with a negative value deletes the key immediately.
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
Step 1: Understand the requirement
We want to set the key and only add expiration if none exists (TTL = -1).
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.
Final Answer:
SET cache "value"
TTL cache
EXPIRE cache 30 if TTL is -1 -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.