Guide to Redis Lists and How to Use Them

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re getting started with Redis, it’s important to familiarize yourself with the various data types available for use. One key data type to become acquainted with is the Redis list. In this article, we’ll provide an overview of Redis lists and review some examples that demonstrate how to use them.

Prerequisites

Before proceeding with this guide, be sure the following prerequisites are in place:

  • You’ll need to have a Redis server installed on your local device. To view the version number for your Redis installation, just execute the command redis --version in the terminal.

  • You’ll also need to know how to use the Redis command-line interface in order to follow along with the examples in this guide.

Redis lists

Redis lists can be defined as lists of strings that are stored in order of insertion. If you’re familiar with some common data structures found in programming, you can also think of them as linked lists. A key advantage of this linked list implementation is the speed of inserting and deleting list elements: Whether your list has a million elements or just a few elements, it’s just as easy for Redis to add and remove elements.

There are a number of operations that can be performed on Redis lists. For example, you can add new elements to a list using the commands RPUSH and LPUSH. Similarly, you can pop, or remove, elements from a list using the commands RPOP and LPOP. The LINDEX command can be used to get an element located at a specific position in a list, and LRANGE is used to retrieve a specific subset of list elements.

Let’s take a closer look at each of these Redis list commands:

  • RPUSH: This command adds a new element at the right of the list. If you’re picturing your list as a classic queue structure, you can think of this as inserting the element at the tail of the list.

  • LPUSH: This command adds a new element at the left of the list. You can think of this as inserting the element at the head of the list.

  • RPOP: This command removes an element at the right, or tail, of the list and retrieves the value to display as an output.

  • LPOP: This command removes an element at the left, or head, of the list and retrieves the value to display as an output.

  • LINDEX: This command retrieves the item located at the specified position in the list.

  • LRANGE: This command retrieves a subset of list elements based on the provided “start” and “stop” offsets.

Using the Redis RPUSH command

The RPUSH command in Redis is used to append a new key value to the list. You can insert multiple elements into a list using a single RPUSH command by specifying multiple arguments at the call of the command. Let’s look at the example shown below, which demonstrates the creation of a list using the RPUSH command:

1
2
3
4
5
6
127.0.0.1:6379> RPUSH compware "keyboard"
(integer) 1
127.0.0.1:6379> RPUSH compware "mouse"
(integer) 2
127.0.0.1:6379> RPUSH compware "fan"
(integer) 3

Using the Redis LPUSH command

The LPUSH command in Redis is used to add list elements at the left, or beginning, of a list. An example of the LPUSH command is shown below:

1
2
3
4
127.0.0.1:6379> LPUSH compware "monitor"
(integer) 4
127.0.0.1:6379> LPUSH compware "speaker"
(integer) 5

Using the Redis RPOP command

In Redis, the RPOP command is used to remove and display the value of the last element in a list:

1
2
127.0.0.1:6379> RPOP compware
"fan"

Using the Redis LPOP command

The LPOP command is used to remove and display the value of the first element in the list:

1
2
127.0.0.1:6379> LPOP compware
"speaker"

Using the Redis LINDEX command

The LINDEX command can be used to retrieve the element stored at a specified index:

1
2
3
4
127.0.0.1:6379> LINDEX compware 1
"keyboard"
127.0.0.1:6379> LINDEX compware 2
"mouse"

Using the Redis LRANGE command

The Redis LRANGE command is used to retrieve and return a subset of list elements that are stored in a range specified by the “start” and “stop” offset arguments. In the example below, we retrieve the elements stored in positions 0 to 5 of the list:

1
2
3
4
127.0.0.1:6379> LRANGE compware 0 5
1) "monitor"
2) "keyboard"
3) "mouse"

Notice that only three values are returned from this command. This occurs because we used RPOP and LPOP to remove two values from the list: “fan” and “speaker”. Thus, the output contains just the three remaining values in the list.

Conclusion

Understanding how to manipulate and retrieve data in Redis lists is essential for maximizing the power of this key-value data store. In this article, we introduced a number of commands that can be used with lists, and we provided examples of how to use each one of these commands. With our examples to guide you, you’ll be able to make use of lists in your own Redis environment.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.