Database / REDIS
What is the purpose of the TTL command?
TTL returns how many seconds remain before a key expires, letting an application check a key's remaining lifetime without having to track expiration times itself in separate application logic.
SET session:abc123 "data" EX 3600 TTL session:abc123 # returns remaining seconds, e.g. 3599 TTL nonexistent:key # returns -2, key doesn't exist TTL permanent:key # returns -1, key exists but has no TTL set
The two special return values are worth memorizing since they're a common source of subtle bugs: -2 means the key doesn't exist at all, while -1 means the key exists but has no expiration set — conflating these two (for example, treating any negative TTL as "expired") can cause an application to misdiagnose a permanent key as missing. PTTL returns the same information in milliseconds for finer-grained checks.
More Related questions...
