Basic Key-Value Operations in Redis

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL redis(("Redis")) -.-> redis/RedisGroup(["Redis"]) redis/RedisGroup -.-> redis/access_cli("Connect Using CLI") redis/RedisGroup -.-> redis/store_string("Set String Value") redis/RedisGroup -.-> redis/fetch_string("Get String Value") redis/RedisGroup -.-> redis/remove_key("Delete Single Key") redis/RedisGroup -.-> redis/verify_key("Check Key Existence") redis/RedisGroup -.-> redis/expire_key("Set Key Timeout") subgraph Lab Skills redis/access_cli -.-> lab-552077{{"Basic Key-Value Operations in Redis"}} redis/store_string -.-> lab-552077{{"Basic Key-Value Operations in Redis"}} redis/fetch_string -.-> lab-552077{{"Basic Key-Value Operations in Redis"}} redis/remove_key -.-> lab-552077{{"Basic Key-Value Operations in Redis"}} redis/verify_key -.-> lab-552077{{"Basic Key-Value Operations in Redis"}} redis/expire_key -.-> lab-552077{{"Basic Key-Value Operations in Redis"}} end

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
    20250409-15-29-23-6S2Gf57d.png

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 the EXPIRE command. 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:

    Let's set a key-value pair that we can then expire. We'll use the SET command. For example, let's set the key tempkey to the value tempvalue.

    SET tempkey tempvalue

    Redis will respond with:

    OK
  3. Set Expiration Time:

    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.

  4. Check Remaining Time To Live (TTL):

    To check the remaining time to live (TTL) for a key, 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.

  5. 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 the EXPIRE command. These are the building blocks for using Redis as a simple and efficient data store.