Redis

Redis EXPIRETIME

Redis keys are persistent until you remove them manually. In some cases, you might need to store the temporary key-value pairs in the Redis store. It is a good practice to remove them when not needed. It is a tedious and time-consuming task to remove the keys manually every time. Redis has the feature to set an expiry time on a given key when you are creating it or after the creation. We call this the TTL value of a Redis key.

Set the TTL Value When the Key Is Creating

The following syntax is used to set the expiration time for a given Redis key at the creation time:

SET Temp_Age 24 EX 5

The “Temp_Age” key expires after 5 seconds.

Set the TTL Value After the Key Is Created

Redis allows you to create the key first and then set an expiry time for the key using the EXPIRE command as follows:

SET Temp_Age 24
EXPIRE Temp_Age 5

It is also possible to specify an expiration date as a UNIX timestamp for a given Redis key using the EXPIREAT command as follows:

EXPIREAT Temp_Age 1669929041

The specified UNIX timestamp is associated with the following human readable date:

Thu, 01 Dec 2022 21:10:41 GMT

The “Temp_Age” key expires on 01-12-2022 at 21:10:41.

The EXPIRETIME Command

The EXPIRETIME command returns the expiry date as a UNIX timestamp which is the number of seconds that is calculated from the UNIX epoch date (1970-01-01T00:00:00Z). This command is useful when you have several keys in the Redis database with expiry times and you need to know the exact expiry date of a specific key. The following is the syntax of the EXPIRETIME command:

EXPIRETIME redis_key

The EXPIRETIME command is very fast due to its O(1) execution time complexity. If there is an expiration time that is associated with the Redis key, the EXPIRETIME command returns the expiration time in seconds as a UNIX timestamp. Otherwise, the command returns a negative number -1 or -2:

-1 reply – The Redis key exists but there is no expiry time (TTL) associated with it.

-2 reply – The specified Redis key doesn’t exist in the database.

Use Case:

Let’s assume that the ABC company needs to keep its motor car spare parts order history only for the latest week. Hence, they use a Redis database to store the customer name as the key and order type as the value. At the same time, they store each key with an expiration time of one week from the creation date.

John buys an engine from the ABC company on 2022-12-02. The company needs to add this record to the Redis database is as follows:

SET customer_john "Engine"

Also, the “customer_john” key should expire after one week from 2022-12-02. So, the expiration date is 2022-12-09 at 12:00 AM. In this case, we use the EXPIREAT command to set the expiration date for this key.

The EXPIREAT command accepts a UNIX timestamp in seconds. We need to provide the expiration date as a UNIX timestamp as follows:

The associated UNIX timestamp for the expiry date (2022-12-09 12:00 AM) is 1670524200).

Let’s set the expiration date as follows:

EXPIREAT customer_john 1670524200

Here, we add only one customer order record to the database. But it is not the case when it comes to a real-world scenario. There can be hundreds of customer orders that are processed per week. It is hard to keep track of expiration dates for all the customer order records.

The EXPIRETIME command comes in handy in these types of scenarios. If you have the Redis key, the EXPIRETIME command helps you to retrieve the associated expiration date in seconds from the epoch date.

Let’s fetch the expiration date for the customer_john key as follows:

EXPIRETIME customer_john

As expected, the UNIX timestamp is returned. If you convert this timestamp value to a human-readable format, it should give the exact date that we set in the previous step.

The EXPIRETIME command can be used when you have a large amount of data in your Redis database and you need to know about the expiry dates of any of the keys.

When No Expiration Time for a Redis Key

Let’s create a Redis key without an expiration time (TTL) as follows:

SET no_ttl_key "no ttl scenario"

If we execute the EXPIRETIME command for this key, it should return the -1. This is because the “no_ttl_key” is not associated with a TTL.

EXPIRETIME no_ttl_key

When the Key Doesn’t Exist

If we run the EXPIRETIME command by specifying a key that doesn’t exist in the current Redis database, it should return the -2 reply as follows:

EXPIRETIME doesnt_exist_key

Conclusion

To conclude, the Redis EXPIRETIME command is used to retrieve the expiration date or time of a given key. It returns a UNIX timestamp in seconds. If there is no expiry time that is associated with a given key, the EXPIRETIME command returns negative 1. The command returns -2 if the specified key doesn’t exist. Most importantly, this command can be used in high-performance applications due to its fast execution with O(1) time complexity.

About the author

Nimesha Jinarajadasa

Being a Full-stack Senior Software Engineer for more than five years, I love technology, as technology has the power to solve our many problems within just a minute. I try to learn more and create more opportunities for this new world.