Node js Redis Lists

08.25.2021

Intro

Lists are one of the fundemental data types in Redis. You will often use this data type to manage many features. In this article, we look at many of the common list commands in Redis using Node.js.

Setting up Redis

For setting up Redis, I would recommend using a service for you in prod. Azure for example, has a great redis service that scales easily. However, you will want to learn redis and eventually how to scale it yourself. This will help with debugging cloud services or eventually, saving money and not using them.

We will start our intro to redis via using docker compose. Create a docker-compose.yml file and add the following.

version: "3.2"
services:
  redis:
    image: "redis:alpine"
    command: redis-server
    ports:
      - "6379:6379"
    volumes:
      - $PWD/redis-data:/var/lib/redis
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf
    environment:
      - REDIS_REPLICATION_MODE=master

Ensure you have docker installed and run

docker-compose up

Installing Redis Modules

There are two modules I see often used in nodejs. I will tend towards ioredis as it has built in support for promises and many other features in redis.

npm install ioredis

Writing the Code

Let's open up a new file, index.js and go through many of the common commands you will used with lists in redis.

Left Push

We can push items to a list using lpush. The first param is the name of the key and then we can pass in as many items as we want for the list.

// Left Push
const result = await redis.lpush("mylist", "foo", "bar")
console.log(result)

For each of the example below, I will use the following template to run all the commands. Here is my full index.js file. We will just replace the commands each time.

const Redis = require("ioredis")

const redis = new Redis({})

async function main() {
    // Left Push
    const result = await redis.lpush("mylist", "one", "two")
    console.log(result)
}

(async () => {
    await main()
})()

Right Push

Similar to left push, we can add items to the list using rpush, but we can do this from the end or the right side.

// Right Push
const result = await redis.rpush("mylist", "three", "four")
console.log(result)

List Range

We can select a portion of our list using the lrange command.

Here is an example select the first few items.

const result = await redis.lrange("mylist", 0, 2)
console.log(result)

We can also select the full list using -1 as the end index.

const result = await redis.lrange("mylist", 0, -1)
console.log(result)

List Insert

We can use the linsert command to insert in a more specific manner. Here we use the "after" string in the second paramter to tell redis which key to insert after. So, in this case, we will insert the value "five" after the value "four".

// List Insert
const result = await redis.linsert("mylist", "after", "four", "five")
console.log(result)

List Index

We can get an item at a specific index using the lindex command.

// List Index
const result = await redis.lindex("mylist", 2)
console.log(result)

List Length

To see the size of a list, use the llen command.

// List Length 
const result = await redis.llen("mylist")
console.log(result)

Left Pop

The lpop allows you to pop from the left or the start of a list.

// Left Pop 
const result = await redis.lpop("mylist")
console.log(result)

Right Pop

Similar to left pop, we can use rpop to pop from the right side.

// Right Pop 
const result = await redis.rpop("mylist")
console.log(result)

List Trim

We can trim a list using ltrim. Here we tell redis to only keep indexes 1 to 3.

// List Trim
const result = await redis.ltrim("mylist", 1, 3)
console.log(result)

List Set

If you prefer to set an item using a specific index, you can use lset. In the example below, we set the value "foo" at index 1.

// List Set
const result = await redis.lset("mylist", 1, "foo")
console.log(result)