CodePark/ questions / TTL & eviction policies
START PRACTICINGPRACTICE
Backend question · Redis & caching › TTL & eviction policies

What does the last command return?

SET page:home "v1" EX 60
SET page:home "v2"
TTL page:home
-1, because a plain SET clears the TTL

A plain SET replaces the whole key, including its expiry, so TTL returns -1. To keep a TTL, you must pass EX or PX again, or pass KEEPTTL.

"About 60, because the TTL is kept" is the tempting answer. People expect it because commands that change a value in place, like
INCR, HSET or LPUSH, do keep the existing TTL. SET does not.