Redis TTL (Time-To-Live)

  • Attach an expiration time (TTL) to a key so cached data is automatically removed after a given period.
  • Prevents long-lived keys from occupying memory.
  • TTL is not enabled by default for every key
    • keys persist indefinitely unless you explicitly set an expiration.
  • Set TTL
    • when writing the key (e.g., SET key value EX 60).
    • after writing (e.g., EXPIRE key 60).
  • Once the TTL elapses
    • Redis deletes the key automatically, and reads return nil (key not found).