In this guide, we are going to explore how to expire keys in Redis. As the title suggests, we will focus on hash key expiry, but the general concept applies to other keys as well.”
To perform the steps demonstrated in this guide, you will need the following components ready to go:
- A properly-configured Linux system. Learn more about installing Ubuntu.
- Redis server installed and configured. Learn more about installing Redis on Ubuntu.
Redis keys are persistent by default, meaning any entry will remain intact unless deleted manually. While this configuration is good in general, there are situations where you may want the keys to expire after a certain amount of time. In other words, you’d want volatile keys.
Now, Redis supports multiple types of keys, categorized by the data type. Here are some of the most common data types you will encounter:
Check out the official Redis documentation on data types for more in-depth info. For the purpose of this guide, we will be focusing on the hash keys.
In Redis, hashes are maps between fields (string) and values (string). Because of this structure, it’s the perfect data type for representing objects. Let’s explain with an example. Have a look at the following structure:
- david: password_1
- alex: password_2
- mason: password_3
Here, we can consider user_database as an hash key. Inside this hash, we have multiple field-value pairs, representing username and password. Let’s implement it in Redis:
$ HMSET user_database user_2 "alex" pass_2 "password_2"
$ HMSET user_database user_3 "mason" pass_3 "password_3"
$ HGET user_database pass_3
To get the info of all the fields at once, use the HGETALL command instead:
In Redis, a hash key can have up to 2^32 – 1 (more than 4 billion) field-value pairs. Most of the time, hashes are used to represent objects. However, because of its nature, hashes can be useful in numerous situations.
Redis Key Expire
All the Redis objects have an expiration time. By default, the value is set to never. However, by changing this value, we can set Redis keys that will expire automatically after a fixed amount of time. Once the expiration time is passed, the key is deleted from the database.
While it’s a generalized explanation, the actual mechanism of key expiry is somewhat complicated. Check out the official Redis documentation for in-depth explanation of the key expiry mechanism.
To set an expiration time, we will use the EXPIRE command. The following example demonstrates its usage:
Here, the key hello will expire in 999 seconds. To check the time left for a key to expire, use the TTL command:
The output will return the time left before the key expires. However, in case of an error, TTL can return different values:
- -2 if the key doesn’t exist.
- -1 if the key has no associated expiration.
Expiring Hash Key
Now that we understand the hash keys and how key expiration works, time to put them into action.
Continuing from the hash key example, let’s put an expiration time to user_database:
To verify this action, check the TTL value of user_database:
The EXPIRE command comes with some additional options that implement some additional conditions when setting the expiration time. Those are as follows:
- NX: The expiration time is set only if the key has no expiry.
- XX: The expiration time is set only if the key has an existing expiry.
- GT: The new expiration time is set if the new expiry is greater than the current one.
- LT: The new expiration time is set if the new expiry is less than the current one.
When implementing the options, the command would look something like this:
Continuing from the previous example, let’s update the expiration time of user_database:
Verify if the expiration time was updated:
In this guide, we explored expiring a hash key in Redis. Redis comes with a dedicated command EXPIRE to set an expiration time for keys. We demonstrated how to use this command to expire a demo key. We also explored various options the EXPIRE command supports.