TTL-&-EXPIRE
Set automatic expiration on keys to manage cache, sessions, and temporary data. TTL shows remaining time, EXPIRE sets it.
You'll Learn
- TTL and PTTL for checking expiration
- EXPIRE and PEXPIRE for setting timeout
- PERSIST to remove expiration
- How Redimo shows live countdown
See Your Data, Not Terminal Text
Redimo visualizes every Redis data type beautifully. Edit inline, undo mistakes, stay safe in production.
1. What Is TTL?
TTL (Time To Live) returns the remaining seconds before a key expires and is automatically deleted. PTTL returns milliseconds for precision timing. This is fundamental to Redis's role as a cache.
Redis handles expiration efficiently using a combination of passive expiration (checked on access) and active expiration (periodic sampling). You don't need to clean up expired keys - Redis does it automatically.
Syntax
TTL key # Remaining seconds (-1 = no expiry, -2 = key doesn't exist)
PTTL key # Remaining milliseconds
EXPIRE key seconds # Set expiration in seconds from now
PEXPIRE key milliseconds # Set expiration in milliseconds
EXPIREAT key timestamp # Set expiration at Unix timestamp (seconds)
PEXPIREAT key timestamp # Set expiration at Unix timestamp (milliseconds)
PERSIST key # Remove expiration (make key permanent)
GETEX key EX seconds # Get value and set/update expiration (Redis 6.2+)TTL Return Values
-2- Key does not exist-1- Key exists, no expiration0+- Seconds remaining
Common Use Cases
- • Session tokens (1 hour)
- • Cache entries (5 minutes)
- • Rate limit windows (1 minute)
- • Temporary locks (30 seconds)
2. The CLI Way (Hard Mode)
In CLI, you're constantly typing TTL to check remaining time. No visual feedback when a key is about to expire:
Managing Session Expiration
127.0.0.1:6379> SET session:abc123 "{\"userId\":1001}" EX 3600
OK
127.0.0.1:6379> TTL session:abc123
(integer) 3597
127.0.0.1:6379> TTL session:abc123
(integer) 3594
127.0.0.1:6379> TTL session:abc123
(integer) 3590
127.0.0.1:6379> TTL session:xyz999
(integer) -2Extending and Removing Expiration
127.0.0.1:6379> EXPIRE session:abc123 7200
(integer) 1
127.0.0.1:6379> TTL session:abc123
(integer) 7199
127.0.0.1:6379> PERSIST session:abc123
(integer) 1
127.0.0.1:6379> TTL session:abc123
(integer) -1
127.0.0.1:6379> PTTL cache:homepage
(integer) 298456The Problem
3. The Redimo Way (Smart Mode)
Redimo shows a live countdown timer for every key with TTL. Watch the seconds tick down in real-time. Color-coded warnings when keys are about to expire.
What Redimo Does for TTL:
- ●Live Countdown: TTL counts down in real-time right next to the key. No need to run TTL commands repeatedly.
- ●Color Warnings: Yellow when under 60 seconds, red when under 10 seconds. Never be surprised by expired keys.
- ●TTL Preservation: When you edit a value, Redimo automatically preserves the existing TTL. No more accidentally removing expiration.
- ●Quick Actions: Extend TTL, remove expiration, or set new timeout with a few clicks.
- ●Filter by TTL: Sort keys by remaining time to find those about to expire.
CLI Pain Points
- • Manual TTL checks constantly
- • No visual countdown
- • No warning for expiring keys
- • SET without EX loses TTL
- • Easy to forget expiration
Redimo Solutions
- • Live countdown display
- • Yellow/red color warnings
- • TTL preserved on edit
- • Click to extend or remove
- • Sort by expiration time
4. Common Patterns
Session Management
# Create session with 1-hour expiration
SET session:abc123 "{\"userId\":1001,\"role\":\"admin\"}" EX 3600
# Extend session on user activity (sliding window)
EXPIRE session:abc123 3600
# Check if session is still valid
TTL session:abc123 # Positive = valid, -2 = expired
# Use GETEX to get and refresh in one command (Redis 6.2+)
GETEX session:abc123 EX 3600Cache with Expiration
# Cache API response for 5 minutes
SET cache:api:users "{\"users\":[...]}" EX 300
# Cache with millisecond precision
SET cache:realtime:data "..." PX 500 # 500ms
# Set expiration at specific time (end of day)
EXPIREAT cache:daily:stats 1703548800 # Unix timestamp
# Check remaining time in milliseconds
PTTL cache:realtime:dataDistributed Lock with Timeout
# Acquire lock with 30-second timeout (prevents deadlocks)
SET lock:resource:123 "worker-42" NX EX 30
# If SET returns OK, lock acquired
# If SET returns nil, lock held by someone else
# Extend lock while working (if needed)
EXPIRE lock:resource:123 30
# Release lock when done
DEL lock:resource:1235. TTL Gotchas
Understanding TTL edge cases will save you from production bugs:
SET removes TTL by default
When you use SET without EX/PX, any existing TTL is removed. The key becomes permanent. This is a common bug when updating cached values.
# Key has 5 minute TTL
SET mykey "value1" EX 300
# Later: update without EX - TTL is LOST!
SET mykey "value2" # Now permanent!
# Fix: Use SETEX or SET with EX
SET mykey "value2" XX EX 300 # XX = only if existsGETEX preserves value, updates TTL
Redis 6.2+ added GETEX which gets the value while updating the expiration - perfect for session refresh patterns.
# Get and refresh TTL in one atomic operation
GETEX session:abc123 EX 3600
# Get and remove TTL (make permanent)
GETEX session:abc123 PERSIST
# Get and set absolute expiration
GETEX session:abc123 EXAT 1703548800Expiration is lazy + active
Expired keys aren't immediately deleted. Redis uses passive deletion (on access) and active deletion (periodic sampling). Memory isn't freed instantly.
6. Pro Tips
Seconds vs Milliseconds
EXPIRE/TTL work in seconds.PEXPIRE/PTTL work in milliseconds. For rate limiting and real-time systems, millisecond precision is often necessary.Useful TTL Commands
# Set TTL only if key has no expiration (Redis 7.0+)
EXPIRE mykey 3600 NX
# Set TTL only if key already has expiration (Redis 7.0+)
EXPIRE mykey 3600 XX
# Set TTL only if new TTL is greater than current (Redis 7.0+)
EXPIRE mykey 7200 GT
# Set TTL only if new TTL is less than current (Redis 7.0+)
EXPIRE mykey 1800 LT
# Get expiration as Unix timestamp (Redis 7.0+)
EXPIRETIME mykey # Returns absolute timestampRedimo Bonus
Ready to Visualize?
Stop running TTL commands repeatedly.
Download Redimo and watch your keys countdown live.