Redis › Expiration & Eviction
What is the difference between the EXPIRE, TTL, and PERSIST commands in Redis?
BeginnerAnswer
EXPIRE sets a countdown timer on a key after which it will be automatically deleted, TTL checks how much time remains before that countdown reaches zero, and PERSIST removes any existing expiration from a key, making it permanent again until explicitly deleted or given a new expiration.
EXPIRE mykey 60
TTL mykey -- returns remaining seconds
PERSIST mykey -- removes the expiration entirelyReal-world example A promotional discount code is given a temporary expiration using EXPIRE, checked periodically with TTL to show a countdown to customers, and made permanent using PERSIST if the promotion is later extended indefinitely.