Key expiry helps automatically remove old or unused data from memory. This keeps the database fast and saves space.
Key expiry for memory management in Redis
Introduction
Syntax
Redis
EXPIRE key seconds TTL key PERSIST key
EXPIRE sets a time (in seconds) after which the key is deleted.
TTL shows how many seconds remain before the key expires.
PERSIST removes the expiration from the key, making it persistent.
Examples
Redis
EXPIRE session123 300Redis
TTL session123
Redis
PERSIST session123
Sample Program
This sets a key 'tempKey' with value 'hello', sets it to expire in 10 seconds, then checks how many seconds remain.
Redis
SET tempKey "hello" EXPIRE tempKey 10 TTL tempKey
Important Notes
Expiry time counts down only when the key exists.
If you set EXPIRE on a key that does not exist, it returns 0 (failure).
Use PERSIST to cancel expiry if needed.
Summary
Key expiry automatically deletes keys after a set time.
Use EXPIRE to set expiry, TTL to check time left, and PERSIST to cancel expiry.
This helps manage memory by removing old data.
Practice
1. What does the
EXPIRE command do in Redis?easy
Solution
Step 1: Understand the purpose of EXPIRE
The EXPIRE command sets a countdown timer on a key, after which Redis deletes it automatically.Step 2: Differentiate from other commands
Unlike DEL which deletes immediately, EXPIRE schedules deletion after a set time.Final Answer:
Sets a time limit after which a key will be automatically deleted -> Option AQuick Check:
EXPIRE = set auto-delete timer [OK]
Hint: EXPIRE sets timer; key deletes after time [OK]
Common Mistakes:
- Confusing EXPIRE with DEL command
- Thinking EXPIRE shows time left instead of setting it
- Assuming EXPIRE removes expiration instead of adding it
2. Which of the following is the correct syntax to set a key
session to expire in 120 seconds?easy
Solution
Step 1: Recall EXPIRE command syntax
The correct syntax isEXPIRE key seconds, soEXPIRE session 120sets expiry to 120 seconds.Step 2: Check other options
EXPIRE 120 session reverses arguments, A is incomplete syntax for SETEX, D is invalid usage of TTL.Final Answer:
EXPIRE session 120 -> Option DQuick Check:
EXPIRE key seconds = EXPIRE session 120 [OK]
Hint: EXPIRE syntax: EXPIRE key seconds [OK]
Common Mistakes:
- Swapping key and seconds in EXPIRE
- Using TTL to set expiry instead of check
- Confusing SETEX syntax
3. Given the commands:
What will the
SET user:1 "Alice" EXPIRE user:1 10 TTL user:1
What will the
TTL user:1 command return immediately after setting expiry?medium
Solution
Step 1: Understand TTL after EXPIRE
After setting EXPIRE to 10 seconds, TTL returns the remaining time in seconds, which is 10 immediately.Step 2: Interpret other options
-1 means no expiry, 0 means expired, Error is invalid here.Final Answer:
10 -> Option CQuick Check:
TTL after EXPIRE = expiry seconds [OK]
Hint: TTL shows seconds left after EXPIRE set [OK]
Common Mistakes:
- Expecting TTL to return -1 immediately
- Thinking TTL returns 0 right after EXPIRE
- Confusing TTL with DEL command
4. You run the command
EXPIRE mykey 30 but TTL mykey returns -2. What is the likely problem?medium
Solution
Step 1: Understand TTL return values
TTL returns -2 if the key does not exist in Redis.Step 2: Analyze the scenario
Since TTL returns -2, it meansmykeyis missing, so EXPIRE had no effect.Final Answer:
The keymykeydoes not exist -> Option BQuick Check:
TTL = -2 means key missing [OK]
Hint: TTL -2 means key missing, not syntax error [OK]
Common Mistakes:
- Assuming -2 means no expiry set
- Thinking EXPIRE syntax is wrong
- Ignoring that key might be deleted
5. You want to keep a Redis key
cache:data but remove its expiry to prevent automatic deletion. Which command should you use?hard
Solution
Step 1: Identify command to remove expiry
The PERSIST command removes the expiry time from a key, making it permanent.Step 2: Check other options
EXPIRE with 0 deletes immediately, DEL deletes key, TTL only checks expiry time.Final Answer:
PERSIST cache:data -> Option AQuick Check:
PERSIST removes expiry timer [OK]
Hint: Use PERSIST to cancel expiry, keep key [OK]
Common Mistakes:
- Using EXPIRE 0 which deletes key immediately
- Confusing DEL with removing expiry
- Using TTL expecting it to remove expiry
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
