What if your data could clean itself up automatically, saving you hours of work?
Why TTL-based expiry in Redis? - Purpose & Use Cases
Imagine you have a huge list of temporary coupons stored in a notebook. You have to check each coupon every day to see if it expired and then erase it manually.
This manual checking is slow, tiring, and easy to forget. You might accidentally use expired coupons or waste time deleting them one by one.
TTL-based expiry lets the system automatically remove data after a set time. You just set a timer, and the system cleans up expired items for you without any extra work.
Check each item daily and delete if expired
SET coupon123 '10%OFF' EX 86400 # expires in 1 day automatically
You can trust your data to self-manage its lifetime, freeing you from tedious cleanup tasks.
Online stores use TTL expiry to automatically remove flash sale prices after the sale ends, ensuring customers always see current offers.
Manual expiry is slow and error-prone.
TTL expiry automates data removal after a set time.
This keeps data fresh and reduces manual work.
Practice
EXPIRE key 60 do?Solution
Step 1: Understand the EXPIRE command
The EXPIRE command sets a time-to-live (TTL) in seconds for a key in Redis.Step 2: Apply the command to the example
Using EXPIRE key 60 means the key will be deleted automatically after 60 seconds.Final Answer:
Sets the key to expire and be deleted after 60 seconds -> Option CQuick Check:
EXPIRE sets TTL = expire after time [OK]
- Thinking EXPIRE deletes key immediately
- Confusing EXPIRE with TTL command
- Assuming EXPIRE renames the key
session?Solution
Step 1: Identify the command to check TTL
The Redis command to check remaining time-to-live is TTL followed by the key name.Step 2: Match the syntax
TTL session correctly checks the remaining TTL of the key 'session'. Other options are either invalid or perform different actions.Final Answer:
TTL session -> Option AQuick Check:
TTL command returns remaining expiry time [OK]
- Using EXPIRE to check TTL instead of setting it
- Using PERSIST which removes expiry, not checks it
- Assuming GETTTL is a valid Redis command
SET temp 100EXPIRE temp 10TTL tempWhat is the expected output of the last command?
Solution
Step 1: Set key and expiry
The key 'temp' is set with value '100' and then given a TTL of 10 seconds.Step 2: Check TTL immediately
Immediately after setting expiry, TTL temp returns the remaining time, which should be close to 10 seconds.Final Answer:
10 -> Option DQuick Check:
TTL after EXPIRE shows seconds left [OK]
- Expecting TTL to return -1 (means no expiry)
- Thinking TTL returns 0 immediately after EXPIRE
- Assuming TTL command causes error
SET user123 "data"EXPIRE user123 30PERSIST user123TTL user123But the TTL command returns -1. What is the issue?
Solution
Step 1: Understand PERSIST effect
PERSIST removes the expiry from a key, making it permanent.Step 2: Check TTL after PERSIST
After PERSIST, TTL returns -1 indicating no expiry is set on the key.Final Answer:
PERSIST removed the expiry, so TTL returns -1 -> Option BQuick Check:
PERSIST removes TTL, TTL returns -1 [OK]
- Thinking TTL returns error if expiry removed
- Assuming EXPIRE failed without checking
- Confusing TTL -1 with key missing
cache_item with a value and ensure it expires exactly after 5 minutes. Which sequence of commands correctly achieves this and verifies the expiry?Solution
Step 1: Set the key with value
Use SET to create the key 'cache_item' with the desired value.Step 2: Set expiry to 300 seconds (5 minutes)
Use EXPIRE cache_item 300 to set TTL to 5 minutes.Step 3: Verify TTL
Use TTL cache_item to check remaining time, which should be close to 300 seconds.Final Answer:
SET cache_item "value"\nEXPIRE cache_item 300\nTTL cache_item -> Option AQuick Check:
SET then EXPIRE then TTL to verify expiry [OK]
- Using TTL to set expiry (wrong command)
- Setting EXPIRE before SET (key must exist first)
- Using PERSIST which removes expiry
Ready to prove it?
Pass the test and this pattern moves unknown
