If you've ever worked with Redis, you probably know it's lightning fast and great for storing key-value data in memory. But one of its most practical features often flies under the radar—key expiration.
Whether you’re caching API results, managing user sessions, or storing time-sensitive data, Redis’s built-in expiration system can help you keep things clean and efficient without lifting a finger.
What Is Key Expiration in Redis?
In simple terms, Redis lets you assign a time-to-live (TTL) to a key. After that time has passed, Redis will automatically delete the key.
This helps:
- Prevent stale data from sticking around forever
- Free up memory without manual cleanup
- Manage temporary values like tokens, sessions, or cache entries
How to Set Expiration
There are multiple ways to set expiration depending on what you need.
1. Using EXPIRE
This sets the TTL in seconds.
EXPIRE mykey 60
This will delete mykey
60 seconds after you run the command.
2. Using PEXPIRE
Same as EXPIRE
, but lets you define TTL in milliseconds.
PEXPIRE mykey 1500
Now, mykey
will expire in 1.5 seconds.
3. Using SET
with Expiry Options
You can combine setting a key with expiration in a single step using EX
(seconds) or PX
(milliseconds).
SET session:1234 "userdata" EX 300
This stores the value and sets it to expire in 5 minutes—all at once.
When Does Redis Actually Remove Expired Keys?
You might assume Redis has a background timer that deletes keys the moment their TTL runs out. But it doesn’t work exactly like that.
Here’s what actually happens:
- Lazy Deletion: If you try to access an expired key, Redis will notice it's expired and delete it then.
- Active Cleanup: Redis also randomly samples a few keys every second and deletes the expired ones in the batch.
This approach keeps performance fast and avoids the overhead of tracking all expiration timers in real-time.
Why Use Expiration?
Key expiration is not just a neat trick—it’s genuinely useful in production:
- Session Management: Automatically log out users after inactivity
- Rate Limiting: Store request counters with auto-reset
- Temporary Caching: Cache results for a limited time
- One-Time Tokens: Automatically invalidate short-lived access tokens
Check a Key’s TTL
You can always check how much time is left on a key using:
TTL mykey
Or, if you’re using milliseconds:
PTTL mykey
If the key doesn’t exist or has no expiration, you’ll get a -1
or -2
as a result.
Final Thoughts
Redis expiration is simple, yet incredibly powerful. It lets you manage memory and data lifecycle with just a few commands. Whether you're building a login system, a cache, or any service that needs short-term data, knowing how to use expiration effectively will make your Redis setup much more robust.
If you're a software developer who enjoys exploring different technologies and techniques like this one, check out LiveAPI. It’s a super-convenient tool that lets you generate interactive API docs instantly.
LiveAPI helps you discover, understand and use APIs in large tech infrastructures with ease!
So, if you’re working with a codebase that lacks documentation, just use LiveAPI to generate it and save time!
You can instantly try it out here! 🚀
Top comments (0)