Redis

Redis Set a Timeout on Key

Redis is a free in-memory key-value database. As a key-value database, Redis works by mapping a specific string key to a particular type of value. Once you create a key in Redis, it will continue to exist unless you remove it manually.

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:

SET key value EX <time_to_live_in_seconds>

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.

SET key value PX <time_to_live_in_milliseconds>

The following examples create a key and set the expiration time to 60 seconds.

127.0.0.1:6379> SET my_key my_value EX 60
OK

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.

127.0.0.1:6379> SET newkey newvalue
OK

The command above will create a new key and value.

127.0.0.1:6379> EXPIRE newkey 60
(integer) 1

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.

127.0.0.1:6379> EXPIRE doesNotExist 10
(integer) 0

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> SET mykey myvalue
OK
127.0.0.1:6379> EXPIREAT mykey 1640984400
(integer) 1

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:

127.0.0.1:6379> TTL mykey
(integer) 3936897

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.

127.0.0.1:6379> PERSIST mykey
(integer) 1

Closing

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list