Redis

Redis Rpush and Rpop

If you are using Redis, you must be well versed in the various data types it offers. One fundamental data type provided by Redis is lists. A Redis list is simply a collection of string keys and values that are stored in their specific insert order.

In this guide, we will narrow down the concept of Redis lists by learning how to use fundamental commands such as RPUSH, RPOP, LPUSH, and LPOP.

Before proceeding, ensure you have a Redis server installed and running on your system. We highly recommend using Redis version 5.0 and above.

Basic Concepts

As stated above, a Redis list is a collection of keys and values that are stored according to their insert order. Hence, the first element in the list is the newly added.

A Redis list is straightforward but powerful enough to hold up to 4 billion elements. However, a significant win for a list is adding or removing elements. Redis will have no performance issues when adding or removing elements if the list contains a few or billions of elements.

You can perform various operations on lists, and Redis provides you commands to execute them with ease.

For example, you can use the RPUSH and LPUSH commands to add elements to a list. RPOP and LPOP commands are helpful when you need to remove elements from a list.

Redis RPUSH Command

Think of a list as a queue data structure. The RPUSH command is used to add new elements to the right of the list. Using the RPUSH command, you add the element to the far right of the queue.

The following example diagram shows how elements in a list are stacked. (not an accurate representation).

You can use the RPUSH command to create a new list or append a new value to the tail of the list.

The following example commands show how to use the RPUSH command to create a list and add new values.

127.0.0.1:6379> RPUSH distros Debian

(integer) 1

127.0.0.1:6379> RPUSH distros Ubuntu

(integer) 2

127.0.0.1:6379> RPUSH distros Manjaro

(integer) 3

The first RPUSH command creates a new list and adds the specified value. The subsequent two commands append the specified values to the existing list.

Redis LPUSH Command

The LPUSH command is similar to the RPUSH command. However, it appends the specified values to the head or left of the list. Like RPUSH, if the list does not exist, the command will automatically create it.

The commands below show how to use the LPUSH command.

127.0.0.1:6379> LPUSH newlist newvalue

(integer) 1

127.0.0.1:6379> LPUSH newlist newvalue2

(integer) 2

127.0.0.1:6379> LPUSH newlist newvalue3

(integer) 3

Both LPUSH and RPUSH commands append values on the left and right of the list, respectively.

Redis RPOP Command

The RPOP command removes the element on the tail or right of the list. The command returns the value of the removed element.

For example:

127.0.0.1:6379> RPOP distros

"Manjaro"

Redis LPOP Command

The LPOP command works similar to the RPOP command but removes the element at the head/left of the list.

For example:

127.0.0.1:6379> LPOP distros

"Debian"

Conclusion

This guide provides you with the basic knowledge of Redis lists and the valuable commands to manipulate lists.

Thank you for reading.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list