Redis is an open-source key-value database that provides performant storage for caching, messaging, and other contexts where speed and low latency are needed.

Redis has multiple data types for working with collections. The most common are Lists and Sets.

What Are Redis Lists and Sets?

Redis Lists and Sets each manage collections of string values. Each has its own approach, advantages, and disadvantages. The sections below discuss each data type in more detail.

Redis Lists

In Redis, Lists are collections of strings kept in the order they were inserted. They operate like Linked Lists in other programming contexts.

Lists are useful precisely because of their ordering methodology. Their ordering is consistent and can be controlled by the way in which you insert new elements.

Redis Sets

Redis Sets are unordered collections of unique strings. Because Sets only include unique values, they can be useful in contexts where you need to avoid duplicates.

Sets are unordered, so values are either fetched randomly or managed by the content of the values themselves.

Differences Between Lists and Sets

Lists are, as observed above, ordered based on insertion. Sets, by contrast, are unordered. Use Lists when you need to consistently access elements based on their positions in the collection.

But Sets also have a particular advantage when it comes to performance. Lists perform well when it comes to fetching elements from the beginning and end of the collection, no matter the collection's size. However, they lag when it comes to fetching values from somewhere in the middle of large collections.

With Sets, on the other hand, you can fetch elements from the middle of a collection much more quickly. This is useful when you frequently need to check whether a certain value exists in a large collection. It is also useful when you need to efficiently fetch a random value from a collection, as you can do with Sets.

Sorted Sets in Redis

Redis has another related data type: Sorted Sets. These are Sets that include a scoring system, allowing you to order a Set by element scores.

Because of ordering, hashes, and labeling of values, Sorted Sets in Redis actually work as a cross between Lists.

Sorted Sets have an array of commands and ways that you can work with their collections.

How to Use Lists in Redis

The following sections introduce you to some of the most useful operations and commands for Lists, and how you can create, view, and modify them.

Adding Elements to a Redis List

You can add elements to the beginning and ending of Lists in Redis with the LPUSH and RPUSH commands, respectively. These commands each also create a new List if the named List does not already exist.

Example:

LPUSH example_list "This is a test."
RPUSH example_list 2 4 6 8

As you can see from the RPUSH example above, the commands support adding multiple elements to a List at the same time.

NOTE: In Redis, numbers are automatically converted to strings, though Redis also has a few special commands for handling strings that contain numbers. Like INCR, which increments the integer held in a string by one.

Selecting Elements from a Redis List

You have a few options when it comes to fetching elements from a List in Redis.

Use the LINDEX command to get a single element based on its index (Redis's index begins at 0). The example below fetches the third element of the example_list created in the previous section.

LINDEX example_list 2
"4"

Use LRANGE to get a range of elements or to get all elements in a List based on their indices. The example below fetches all the elements from the example_list.

LRANGE example_list 0 -1

Output:

1) "This is a test."
2) "2"
3) "4"
4) "6"
5) "8"

Negative indices start at the end of the list, so the example above fetches all elements from the first (index 0) to the last (index -1).

Here is another example that fetches only the middle three elements:

LRANGE example_list 1 -2
1) "2"
2) "4"
3) "6"

Use LPOP and RPOP to get one or more elements from the beginning or end of a List and then remove those elements from the List. This can be useful, for instance, in the case of a Task List, where you want to remove an element as it is being worked on.

LPOP example_list
"This is a test."

Using RPOP Command:

RPOP example_list 2
"8"
"6"

When you take a look at the list now, you get:

LRANGE example_list 0 -1
1) "2"
2) "4"

Removing Elements from a Redis List

Similar to how you can remove elements from the List using the LPOP and RPOP commands, you can also remove elements using the LREM command. It removes one or more elements from a List based on the elements' values. For example:

LPUSH new_example_list "this" "is" "a" "test" "and" "a" "test" "to" "test"
LREM new_example_list -2 "test"
LRANGE new_example_list 0 -1
1) "test"
2) "to"
3) "a"
4) "and"
5) "a"
6) "is"
7) "this"

The -2 in the command above has LREM remove the last two instances of test from the List. You could, alternatively, use a positive number to remove the first matching instances from the List.

Finally, using the DEL command allows you to delete a List entirely.

DEL new_example_list

Using Capped Lists

Redis allows you to use the LTRIM command to implement a Capped List, which is essentially a List limited to a certain length. This can be useful, for instance, when you only want a List to contain the newest "X" number of entries.

For example, assume that long_example_list contains at least 100 items and that new items are added to the end of the list with RPUSH. To only ever keep the newest 100 items, you run the LTRIM command every time you add a new item to the List:

RPUSH long_example_list "New item"
LTRIM long_example_list -100 -1

How to Use Sets in Redis

The sections below show some of the most useful operations and commands for Sets, from creating to viewing, and modifying them.

Adding Elements to a Redis Set

You can add elements to a Set using the SADD command. Like with Lists, this command also creates a new Set if one does not already exist.

SADD example_set 1 3 5 7 9
(integer) 5

Sets do not hold duplicate values. If you try to add a duplicate to the above example_set, there are no elements (0) added to the Set.

SADD example_set 9
(integer) 0

Checking and Selecting Elements from Redis Sets

Redis provides two means of fetching elements from a Set.

Use the SMEMBERS command to get all elements of a Set:

SMEMBERS example_set
1) "1"
2) "3"
3) "5"
4) "7"
5) "9"

Use the SPOP command to get a random element from a Set; at the same time, the command removes the selected element from the Set:

SPOP example_set
"3"

Use the SMEMBERS command to get members of a set stored at a given key.

SMEMBERS example_set

Output:

1) "1"
2) "5"
3) "7"
4) "9"

You can also check a Set to see whether an element with a certain value is a member, using the SISMEMBER command. It returns 1 for true and 0 for false:

SISMEMBER example_set 7
(integer) 1

Removing Elements from Redis Sets

You have seen how the SPOP command returns and removes an element from a Set.

However, you can use the SREM command to remove an element from a Set based on its value. For instance:

SADD new_example_set "test 1" "test 2" "test 3"
SREM new_example_set "test 3"
SMEMBERS new_example_set
1) "test 1"
2) "test 2"

Ending..

This article should help you get started in working with Redis sets and lists. Stay tuned for more upcoming posts in the Redis series.

Thanks for reading!!

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.