Redis holds everything in memory, which raises an obvious question: what stops it from filling up and falling over? The answer is two mechanisms working together. Expiration lets keys delete themselves after a set time, which is what makes Redis safe as a cache. Eviction decides what Redis throws away when it hits its memory limit anyway. Understanding both is what separates a cache that quietly manages its memory from one that crashes with an out-of-memory error at the worst moment.
This is part 8 and the last of the fundamentals module in the Redis Masterclass, following sorted sets.
Setting expirations
You attach a time-to-live to a key, and Redis deletes it automatically when the time elapses:
SET session:abc "..." EX 3600 # expires in 3600 seconds









