Basic Key-Value Operations in Redis

RedisBeginner
Practice Now

Introduction

In this lab, you will learn the fundamental key-value operations in Redis. We'll use the redis-cli command-line interface to interact with the Redis server and perform basic operations like setting, getting, checking existence, deleting, and setting expiration times for keys. By the end of this lab, you'll have a solid understanding of how to use Redis as a simple data store.

Setting and Retrieving Key-Value Pairs

In this step, we'll focus on the core operations of setting and retrieving key-value pairs in Redis. This is the foundation for using Redis as a data store.

Redis stores data as key-value pairs, similar to a dictionary. The key is a unique identifier, and the value is the data associated with that key.

  1. Connect to Redis:

    Open a terminal in the LabEx VM. You should already be in the ~/project directory. Connect to the Redis server using the redis-cli command:

    redis-cli

    You should see the Redis prompt: 127.0.0.1:6379>. This indicates a successful connection to the Redis server.

  2. Set a Key-Value Pair:

    Let's set a key-value pair using the SET command. We'll set the key mykey to the value myvalue.

    SET mykey myvalue

    Redis will respond with:

    OK

    This confirms that the key-value pair has been successfully stored.

  3. Get the Value of a Key:

    To retrieve the value associated with a key, use the GET command. Let's retrieve the value of mykey:

    GET mykey

    Redis will respond with:

    "myvalue"

    This shows that we have successfully retrieved the value associated with the key mykey.

  4. Set Another Key-Value Pair:

    Let's set another key-value pair with a different key and value. This time, we'll use user:1001 as the key and John as the value.

    SET user:1001 John

    Redis will respond with:

    OK
  5. Get the Value of the New Key:

    Now, let's retrieve the value of the user:1001 key:

    GET user:1001

    Redis will respond with:

    "John"

    You have now successfully set and retrieved key-value pairs in Redis.

  6. Exit Redis CLI:

    It's important to exit the Redis CLI after each step so that the commands are logged correctly. Type:

    exit
    Redis CLI session screenshot

Checking Key Existence

In this step, we'll learn how to check if a key exists in Redis using the EXISTS command. This is useful for determining whether a key has been set before attempting to retrieve its value or perform other operations.

  1. Connect to Redis:

    Connect to the Redis server using the redis-cli command:

    redis-cli

    You should see the Redis prompt: 127.0.0.1:6379>.

  2. Check Existence of an Existing Key:

    In the previous step, we set the key mykey. Let's check if it exists using the EXISTS command:

    EXISTS mykey

    Redis will respond with:

    (integer) 1

    A response of 1 indicates that the key mykey exists in Redis.

  3. Check Existence of a Non-Existent Key:

    Now, let's check if a key that we haven't set exists. For example, let's check for a key named nonexistentkey:

    EXISTS nonexistentkey

    Redis will respond with:

    (integer) 0

    A response of 0 indicates that the key nonexistentkey does not exist in Redis.

  4. Exit Redis CLI:

    Exit the Redis CLI to ensure the commands are logged:

    exit

Deleting Keys

In this step, we'll learn how to delete keys from Redis using the DEL command. This is essential for managing data and removing obsolete or unwanted entries.

  1. Connect to Redis:

    Connect to the Redis server using the redis-cli command:

    redis-cli

    You should see the Redis prompt: 127.0.0.1:6379>.

  2. Delete an Existing Key:

    In the previous steps, we set the key mykey. Let's delete it using the DEL command:

    DEL mykey

    Redis will respond with:

    (integer) 1

    A response of (integer) 1 indicates that one key was successfully deleted.

  3. Delete a Non-Existent Key:

    Let's try to delete a key that doesn't exist, such as nonexistentkey:

    DEL nonexistentkey

    Redis will respond with:

    (integer) 0

    A response of (integer) 0 indicates that no keys were deleted (because the key didn't exist).

  4. Delete Multiple Keys:

    The DEL command can also be used to delete multiple keys at once. Let's delete the user:1001 key we created earlier, and also try to delete nonexistentkey again in the same command:

    DEL user:1001 nonexistentkey

    Redis will respond with:

    (integer) 1

    A response of (integer) 1 indicates that one key was successfully deleted (user:1001), and the attempt to delete nonexistentkey was ignored since it didn't exist.

  5. Exit Redis CLI:

    Exit the Redis CLI:

    exit

Setting Key Expiration

In this step, we'll learn how to set an expiration time for a key in Redis using both the EXPIRE command and the SET command with the EX parameter. This is useful for automatically removing data after a certain period, such as session data or temporary caches.

  1. Connect to Redis:

    Connect to the Redis server using the redis-cli command:

    redis-cli

    You should see the Redis prompt: 127.0.0.1:6379>.

  2. Set a Key-Value Pair with Expiration (Method 1: SET with EX parameter):

    Redis allows you to set a key-value pair with an expiration time in a single command using the EX parameter. Let's set the key sessionkey to the value sessionvalue with an expiration time of 15 seconds:

    SET sessionkey sessionvalue EX 15

    Redis will respond with:

    OK

    This command sets the key-value pair and the expiration time in one operation, which is more efficient than using separate commands.

  3. Check the TTL of the Key Set with EX:

    Let's check the remaining time to live (TTL) for the sessionkey:

    TTL sessionkey

    Redis will respond with the number of seconds remaining until the key expires (e.g., (integer) 14). The value will be slightly less than 15 due to the time elapsed since setting the key.

  4. Set a Key-Value Pair (Method 2: Using EXPIRE command separately):

    Alternatively, you can set a key-value pair first and then set its expiration time separately. Let's set the key tempkey to the value tempvalue:

    SET tempkey tempvalue

    Redis will respond with:

    OK
  5. Set Expiration Time Using EXPIRE Command:

    Now, let's set an expiration time of 10 seconds for the tempkey using the EXPIRE command:

    EXPIRE tempkey 10

    Redis will respond with:

    (integer) 1

    A response of (integer) 1 indicates that the expiration time was successfully set.

  6. Check Remaining Time To Live (TTL):

    To check the remaining time to live (TTL) for the tempkey, use the TTL command:

    TTL tempkey

    Redis will respond with the number of seconds remaining until the key expires (e.g., (integer) 9). The value might be slightly less than 10 due to the time elapsed since setting the expiration. If the key doesn't exist or doesn't have an expiration, TTL returns -2 or -1 respectively.

  7. Exit Redis CLI:

    Exit the Redis CLI:

    exit

Summary

In this lab, you have learned the fundamental key-value operations in Redis using the redis-cli command-line interface. You learned how to connect to the Redis server and use the SET command to store data as key-value pairs. You also practiced retrieving values using the GET command. Furthermore, you learned how to check for the existence of keys using the EXISTS command, delete keys using the DEL command, and set expiration times for keys using both the SET command with the EX parameter and the EXPIRE command. The SET command with EX parameter is particularly useful as it allows you to set a key-value pair with expiration time in a single, efficient operation. These are the building blocks for using Redis as a simple and efficient data store.