However, Redis allows you to set a limited lifetime to a specific key. Using this functionality, you can tell Redis to create a key and delete it after a particular amount of time has elapsed. Volatility in keys is beneficial when you need to store temporary values.
In this guide, you will learn how to create keys with an expiration duration, how to set an expiration time on existing keys, and how to check the time remaining until a key expires.
To follow this tutorial, ensure you have Redis installed and running on your system. All the commands provided in this tutorial are tested on a Debian 11 server running Redis version 6.0.
Use the Redis CLI utility to get similar output as this tutorial shows to execute the commands.
Creating a Key with Expiry Duration
To create a Redis with an expiration time, use the SET command and the EX option to set the expiration time.
The general syntax is as:
The EX option takes a number in seconds and sets the number of seconds the key is valid until expiration.
You can also use PX to specify the expiration time in Milliseconds.
The following examples create a key and set the expiration time to 60 seconds.
The above key will expire after 60 seconds and be removed from the database.
Setting Expiration Time to Existing Keys
To set an expiration time for an existing key in Redis, use the EXPIRE command. This command takes the key and the duration in seconds to assign to the specified key.
The following examples illustrate how to use the EXPIRE command in Redis.
The command above will create a new key and value.
We then use the EXPIRE command to set the expiration time for the key to 60 seconds.
The command returns (integer 1) 1 if the expiration duration is set successfully and (integer) 0 if the expiration time fails.
The above command returns (integer) 0 as the specified key does not exist.
Using Unix Time
If you want a key to expire at a specific time, you can use the EXPIREAT command. This command takes a Unix timestamp as the duration.
For example, to set the key to expire on the 1st of 2022, first convert the time to Unix timestamp using tools such as UnixTimestamp.com
Copy the value of the Unix Timestamp and use it in the command as shown:
127.0.0.1:6379> EXPIREAT mykey 1640984400
The first commands create a key and value using the SET command. We then use the EXPIREAT command to set the key to expire in 2 months.
Check Time to Live
To the time remaining before a key expires, also known as Time to Live, use the TTL command as:
The command will return the number of seconds remaining until a key expires.
Expiration Time Persistence.
If you set an expiration time on a specific key, it will be automatically be overwritten by any command that modifies the value of the key.
To manually remove the expiration value, use the PERSIST command.
This guide discussed the number of commands and the syntax to set and modify the expiration time for keys in a Redis database.
Thank you for reading, and stay tuned for more tutorials.