Redis

Redis Expire Hash Key

Redis is an open-source, in-memory data structure store that’s used as a distributed, in-memory key-value database. Because of its nature, Redis is a high-performance data storage, suitable for implementation in different situations, for example, cache, message broker, etc.

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.”

Prerequisites

To perform the steps demonstrated in this guide, you will need the following components ready to go:

Redis Keys

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:

  • Strings
  • Lists
  • Sets
  • Hashes

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.

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:

  • user_database
  • 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_1 "david" pass_1 "password_1"

$ HMSET user_database user_2 "alex" pass_2 "password_2"

$ HMSET user_database user_3 "mason" pass_3 "password_3"

As demonstrated, Redis uses the command HMSET to register hash keys. To retrieve the data stored in the key, we need to use the HGET command:

$ HGET user_database user_1

$ HGET user_database pass_3

To get the info of all the fields at once, use the HGETALL command instead:

$ HGETALL user_database

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:

$ SET hello "the quick brown fox"

$ EXPIRE hello 999

Here, the key hello will expire in 999 seconds. To check the time left for a key to expire, use the TTL command:

$ TTL hello

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:

$ EXPIRE user_database 999

To verify this action, check the TTL value of user_database:

$ TTL user_database

EXPIRE options

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:

$ EXPIRE <key> <expiration_time> <options>

Continuing from the previous example, let’s update the expiration time of user_database:

$ EXPIRE user_database 9999 GT

Verify if the expiration time was updated:

$ TTL user_database

Final Thoughts

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.

Interested in learning more about Redis? The Redis sub-category is full of in-depth guides. Learn more about common Redis commands, the BITCOUNT command, pipelining etc.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.