Storing Lists in Redis: Insertion Commands

Let’s look at the commands used to insert Lists in the Redis database.

As well as storing Strings in Redis, we can also store Lists. The Redis database internally stores List as a linked list. This linked list has a head and tail. Whenever we insert a new element, we can insert it either at the head or tail. The ‘head’ of the list is considered as the left-most element of the list and the ‘tail’ is considered as right-most element of the list.

Let’s look at some of the commands that are used to handle the List in the Redis database. We need to store a list of all the companies which build cars. We will store the list in a key, called cars. If a new company arrives then it is added to the list. If a company stops producing cars, then it is removed from the list. We are going to maintain this list in Redis.

LPUSH command

The LPUSH command is used to insert a value at the head of the list. We can provide one or more values with this command. The syntax of this command is.

LPUSH key value… In the example below, we are inserting the car manufacturers in the Redis database. The key is cars. We have inserted four manufacturers.

Get hands-on with 1200+ tech skills courses.