Introduction to Redis Data Structures

RedisBeginner
Practice Now

Introduction

In this lab, you will explore fundamental Redis data structures and how to interact with them using the redis-cli command-line tool. The lab focuses on practical exercises to help you understand how to store and retrieve data in Redis.

You'll begin by working with Strings, learning how to set, get, check for existence, and delete string values. Then, you'll move on to Lists, using commands like LPUSH and LRANGE. Next, you'll manage Sets with SADD and SMEMBERS. Finally, you'll explore Hashes using HSET and HGET. This hands-on experience will provide a solid foundation for using Redis in various applications.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a beginner level lab with a 95% completion rate. It has received a 100% positive review rate from learners.

Work with Strings for Simple Data

In this step, we'll explore how to use Redis to store and retrieve simple string data. Redis is often used as a cache or a simple key-value store, and strings are the most basic data type it offers.

First, let's connect to the Redis server using the redis-cli command-line tool. Open a terminal in the LabEx VM. You should already be in the ~/project directory.

Type the following command to connect to the Redis server:

redis-cli

You should see a prompt that looks like this:

127.0.0.1:6379>

This indicates that you are now connected to the Redis server.

Now, let's set a simple string value. We'll use the SET command. The SET command takes two arguments: the key and the value. Let's set a key called mykey to the value Hello Redis:

SET mykey "Hello Redis"

You should see the following output:

OK

This means that the value has been successfully set.

Now, let's retrieve the value using the GET command. The GET command takes one argument: the key. Let's retrieve the value of mykey:

GET mykey

You should see the following output:

"Hello Redis"

This confirms that we have successfully stored and retrieved a string value in Redis.

Let's try another example. This time, let's store a number as a string.

SET counter 100
GET counter

You should see:

"100"

Redis treats this as a string, even though it represents a number.

You can also use the EXISTS command to check if a key exists.

EXISTS mykey

You should see:

(integer) 1

This indicates that the key mykey exists. If the key does not exist, the command will return (integer) 0.

Finally, let's delete the key using the DEL command.

DEL mykey

You should see:

(integer) 1

This indicates that the key mykey has been successfully deleted.

Now, if you try to get the value of mykey again:

GET mykey

You should see:

(nil)

This confirms that the key has been deleted.

Remember to exit the redis-cli to ensure your commands are logged. Type:

exit

This will return you to the regular terminal prompt.

Use Lists with LPUSH and LRANGE

In this step, we'll explore how to use Redis lists to store and retrieve ordered collections of data. Redis lists are implemented as linked lists, which makes them efficient for adding and removing elements from the beginning or end of the list. We'll focus on the LPUSH and LRANGE commands.

We'll continue using the redis-cli command-line tool. If you're not already connected, open a terminal in the LabEx VM and type:

redis-cli

Now, let's create a list and add some elements to it using the LPUSH command. LPUSH adds elements to the left (head) of the list. The LPUSH command takes two or more arguments: the key of the list and the value(s) to add. Let's create a list called mylist and add the values item1, item2, and item3:

LPUSH mylist item1

You should see the following output:

(integer) 1

This means that one element has been added to the list. The return value of LPUSH is the length of the list after the operation.

Now, let's add the other items:

LPUSH mylist item2
(integer) 2
LPUSH mylist item3
(integer) 3

Now, let's retrieve the elements of the list using the LRANGE command. LRANGE returns a range of elements from the list. The LRANGE command takes three arguments: the key of the list, the start index, and the end index. The indices are zero-based, so the first element is at index 0. To retrieve all elements of the list, we can use the start index 0 and the end index -1.

LRANGE mylist 0 -1

You should see the following output:

1) "item3"
2) "item2"
3) "item1"

Notice that the elements are returned in the reverse order that we added them, because LPUSH adds elements to the beginning of the list.

Let's add a few more items to the list:

LPUSH mylist item4
LPUSH mylist item5

Now, let's retrieve the first 3 elements of the list (indices 0 to 2):

LRANGE mylist 0 2

You should see:

1) "item5"
2) "item4"
3) "item3"

You can also use negative indices to access elements from the end of the list. For example, to retrieve the last element of the list, you can use the index -1:

LRANGE mylist -1 -1

You should see:

1) "item1"

Remember to exit the redis-cli to ensure your commands are logged. Type:

exit

This will return you to the regular terminal prompt.

Manage Sets with SADD and SMEMBERS

In this step, we'll explore how to use Redis sets to store and manage unordered collections of unique elements. Redis sets are useful for tasks like tracking unique visitors, storing tags, or managing relationships between objects. We'll focus on the SADD and SMEMBERS commands.

We'll continue using the redis-cli command-line tool. If you're not already connected, open a terminal in the LabEx VM and type:

redis-cli

Now, let's create a set and add some members to it using the SADD command. SADD adds one or more members to a set. The SADD command takes two or more arguments: the key of the set and the member(s) to add. Let's create a set called myset and add the members member1, member2, and member3:

SADD myset member1

You should see the following output:

(integer) 1

This means that one member has been added to the set. The return value of SADD is the number of members that were added to the set (excluding members that were already present).

Now, let's add the other items:

SADD myset member2
(integer) 1
SADD myset member3
(integer) 1

Now, let's retrieve the members of the set using the SMEMBERS command. SMEMBERS returns all the members of the set. The SMEMBERS command takes one argument: the key of the set.

SMEMBERS myset

You should see the following output (the order of the members may vary, as sets are unordered):

1) "member3"
2) "member2"
3) "member1"

Let's try adding a duplicate member to the set:

SADD myset member1

You should see:

(integer) 0

This indicates that no new members were added, because member1 was already in the set.

Let's add a few more members to the set:

SADD myset member4
SADD myset member5

Now, let's retrieve all the members again:

SMEMBERS myset

You should see something like:

1) "member5"
2) "member4"
3) "member3"
4) "member2"
5) "member1"

The order may be different.

Remember to exit the redis-cli to ensure your commands are logged. Type:

exit

This will return you to the regular terminal prompt.

Explore Hashes with HSET and HGET

In this step, we'll explore how to use Redis hashes to store and retrieve collections of field-value pairs. Redis hashes are useful for representing objects with multiple attributes. We'll focus on the HSET and HGET commands.

We'll continue using the redis-cli command-line tool. If you're not already connected, open a terminal in the LabEx VM and type:

redis-cli

Now, let's create a hash and add some fields and values to it using the HSET command. HSET sets the value of a field in a hash. The HSET command takes three arguments: the key of the hash, the field, and the value. Let's create a hash called myhash and set the field field1 to the value value1:

HSET myhash field1 value1

You should see the following output:

(integer) 1

This means that a new field has been added to the hash. The return value of HSET is 1 if the field is new in the hash and 0 if the field already exists and the value was updated.

Now, let's add another field:

HSET myhash field2 value2
(integer) 1

Now, let's retrieve the value of a field using the HGET command. HGET gets the value of a field in a hash. The HGET command takes two arguments: the key of the hash and the field. Let's retrieve the value of field1 in myhash:

HGET myhash field1

You should see the following output:

"value1"

This confirms that we have successfully stored and retrieved a field-value pair in the hash.

Let's try updating the value of an existing field:

HSET myhash field1 newvalue1

You should see:

(integer) 0

This indicates that the field already existed, and its value was updated.

Now, let's retrieve the value of field1 again:

HGET myhash field1

You should see:

"newvalue1"

This confirms that the value has been updated.

Let's add a few more fields to the hash:

HSET myhash field3 value3
HSET myhash field4 value4

Remember to exit the redis-cli to ensure your commands are logged. Type:

exit

This will return you to the regular terminal prompt.

Summary

In this lab, we explored fundamental Redis data structures, starting with strings. We learned how to connect to a Redis server using redis-cli and then used the SET command to store string values associated with keys. We then retrieved these values using the GET command. We also saw how Redis treats numbers stored as strings.

Furthermore, we learned to create and manipulate Lists using LPUSH and LRANGE, managing Sets with SADD and SMEMBERS, and exploring Hashes using HSET and HGET. These commands allow you to store and retrieve different types of data in Redis, making it a versatile tool for various applications. Remember to exit the redis-cli after each step to ensure your commands are logged for verification.