Skip to main content
InterviewHubPRO
Section Overview
Q3

Question 3 of 50

Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

6%
Redis & Caching (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

How does TTL (Time to Live) and Key Expiration work in Redis?

junior LevelredisBasic

In Simple Words: You can set an expiration timeout on any key using EXPIRE key seconds or SET key value EX seconds:

  • When the timer expires, the key becomes inaccessible to clients.
  • Eviction Mechanisms:
    1. Passive (Lazy) Expiration: When a client tries to read a key, Redis checks if it has expired. If expired, it deletes it and returns nil.
    2. Active Expiration: Every 100ms, Redis randomly samples 20 keys with expiration set, deletes expired keys, and repeats if more than 25% of keys sampled were expired.