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:
- 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. - 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.
- Passive (Lazy) Expiration: When a client tries to read a key, Redis checks if it has expired. If expired, it deletes it and returns