0
0
Redisquery~30 mins

Key expiry for memory management in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Key Expiry for Memory Management in Redis
📖 Scenario: You are managing a Redis database that stores temporary session data for an online store. To keep the database clean and efficient, you want to set expiration times on keys so that old sessions are automatically removed.
🎯 Goal: Build a Redis setup where you create keys for user sessions, configure expiration times, and verify that keys expire as expected to manage memory effectively.
📋 What You'll Learn
Create keys representing user sessions with specific values
Set expiration time on these keys using Redis commands
Check the remaining time to live (TTL) for keys
Demonstrate automatic key expiry after the set time
💡 Why This Matters
🌍 Real World
Setting key expiry helps automatically remove old or unused data, keeping the Redis database efficient and saving memory.
💼 Career
Many jobs require managing cache or session data with automatic expiry to ensure performance and resource management.
Progress0 / 4 steps
1
Create session keys in Redis
Use the Redis SET command to create three keys named session:user1, session:user2, and session:user3 with values data1, data2, and data3 respectively.
Redis
Need a hint?

Use the SET command followed by the key name and value.

2
Set expiration time for session keys
Use the Redis EXPIRE command to set an expiration time of 60 seconds on the keys session:user1 and session:user2.
Redis
Need a hint?

The EXPIRE command takes the key name and the time in seconds.

3
Check the time to live (TTL) for session keys
Use the Redis TTL command to check the remaining expiration time for the keys session:user1, session:user2, and session:user3.
Redis
Need a hint?

The TTL command shows how many seconds remain before the key expires. If the key has no expiry, it returns -1.

4
Verify automatic key expiry
Wait for 60 seconds and then use the Redis EXISTS command to check if the keys session:user1, session:user2, and session:user3 still exist.
Redis
Need a hint?

The EXISTS command returns 1 if the key exists, 0 if it does not.