python.redis.missing_ttl
Stability
Medium
Detects redis.set() without ex, px, exat, or pxat parameter.
Why It Matters
Section titled “Why It Matters”Keys without TTL accumulate indefinitely:
- Memory exhaustion — Redis memory grows without bound
- OOM crashes — Redis becomes unresponsive when memory fills
- Stale data — Old data persists long after it should expire
- Eviction chaos — When Redis hits maxmemory, random keys get evicted
Redis is an in-memory store. Every key without TTL is a memory leak.
Example
Section titled “Example”# ❌ Beforeredis.set(f"session:{user_id}", session_data)This key will exist forever unless explicitly deleted.
# ✅ Afterredis.set(f"session:{user_id}", session_data, ex=3600) # 1 hourSession expires automatically after 1 hour.
What Unfault Detects
Section titled “What Unfault Detects”redis.set()without TTL parametersredis.setex()(deprecated but OK)redis.hset()without laterexpire()callredis.lpush(),redis.sadd()etc. without expiration
TTL Parameters
Section titled “TTL Parameters”# Secondsredis.set("key", "value", ex=3600) # 1 hour
# Millisecondsredis.set("key", "value", px=500) # 500ms
# Unix timestamp (seconds)redis.set("key", "value", exat=1704067200) # Jan 1, 2024
# Unix timestamp (milliseconds)redis.set("key", "value", pxat=1704067200000)Auto-Fix
Section titled “Auto-Fix”Unfault adds ex=3600 (1 hour default). Adjust based on your data lifecycle.
Best Practices
Section titled “Best Practices”# Cache with TTL based on data freshness needsredis.set("user:profile:123", data, ex=300) # 5 min for frequently updated
# Sessions with reasonable expiryredis.set("session:abc", data, ex=86400) # 24 hours
# Rate limiting windowsredis.set("ratelimit:user:123", count, ex=60) # 1 minute window
# For data that should persist, use a database instead# Redis is not a primary data store