Subscribe to get my new tutorials in your inbox.

Insert Array into Redis using NodeJS

Insert Array into Redis using NodeJS

In this article, we will learn how you can insert Array into Redis using NodeJS. Redis is used as a key-value data store. It supports numerous data storage structures such as simple strings, lists, hash maps, etc. The example that we will code in this article will be using NodeJS and we will create a simple Node ExpressJS server that will read and write Array data into Redis.

But before going into details, let’s quickly find out what is Redis?

What is Redis?

Work with Array data in Redis using NodeJS example

Redis is an open-source (BSD licensed), in-memory data store, used as a database, cache, and message broker (pub-sub). It is highly reliable, scalable, and has high availability.

Redis can be used to cache content . For eg. — database queries that are supposed to be used by users, for eg. Flight Search Results (of course with an expiry time), static content like HTML, JS, CSS. Redis is also highly used as a Session Store for users authorizations. Unique user sessions are stored in Redis. Rather than making roundtrips to Databases on the disk, reading and writing user sessions in Redis is faster.

Lists vs Arrays

In my introductory paragraph, I mentioned that Redis can store list data, while this article talks about Arrays. So what’s the fuss about lists and arrays? To be honest, Array like data in Redis are called Lists, and we push and pop items from lists. But Lists are not native Array structures behind the scenes. A bit of theory…

Redis lists are implemented via Linked Lists. This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time.

Redis Lists are implemented with linked lists because for a database system it is crucial to be able to add elements to a very long list in a very fast way. Another strong advantage, as you’ll see in a moment, is that Redis Lists can be taken at constant length in constant time.

Now that we know, we will work with Lists, lets look at some common Redis commands to work with Lists.

rpush – append one or more elements to the end of the list. Returns the length of the list after the push operation. Example below,

> rpush userlist "jeniffer"

lpush – add one or more elements to the start of the list. Returns the length of the list after the push operations. Example below,

> lpush userlist "joseph"

lrange – get a range of elements from the list. This is basically used to retrieve elements. Example below,

> lrange userlist 0 -1
//returns
1) "joseph"
2) "jeniffer"

There are a lot of other useful commands to operate with Redis Lists. I have added a link to it in the references section.

In case you are looking for basic Redis commands to get started, I have a separate article on that.

Working with NodeJS

Now, let’s add a NodeJS Express server to our example to complete it. We will be creating 2 routes to read and write users into Redis.

1. GET /users
2. POST /users

Prerequisites

You need to install Redis on your computer or have it running as a Docker container for the example that I will be covering below. You also need NodeJS installed. I would prefer Node version equal to or higher than 10. I will not go into the installation part as you can browse and follow the steps.

Now, let’s get back to our code example.

First, let’s install all the dependencies. We need node redis client and express server. Run this command by opening a terminal and navigating to your project folder.

npm install redis express

Next, start the Redis server. Open your terminal and run this command,

redis-server

This will start the Redis instance on your computer. You should be able to see the screen below

Start Redis server

Then let’s connect to the Redis instance running locally on your computer. 6379 is the default port for Redis. Create a server.js file inside your project and add the code below,

const redisClient = redis.createClient({
    host: 'localhost',
    port: 6379
});

Now, lets read and write items to our list. First we will create the GET route for reading Array/List data from Redis.

app.get('/users', (req, res) => {
    //return list items
    redisClient.lrange('userlist', 0, -1, (err, data) => {
        if(err) {
            return res.status(500).send('Error');
        }
        //data comes as array
        res.json({
            data: data
        });
    })
});

Now, we will append/add items to our Redis Array/List. I have created a POST route for that.

app.post('/users/:name', (req, res) => {
    const { name } = req.params;

    //push at the end of list
    redisClient.rpush('userlist', name, (err, data) => {
        if(err) {
            return res.status(500).send('Error');
        }
        //data is the length of the list
        res.json({
            data: data
        });
    })
});

Instead of reading the user information from the POST body, I am reading from the request parameters. This is mainly to keep the example simple. You can read the data from POST request body.

Full code

const express = require('express');
const app = express();
const redis = require('redis');
const redisClient = redis.createClient({
    host: 'localhost',
    port: 6379
});

app.get('/ping', (req, res) => {
    res.send('pong');
});

app.get('/users', (req, res) => {
    //return list items
    redisClient.lrange('mylist', 0, -1, (err, data) => {
        if(err) {
            return res.status(500).send('Error');
        }
        //data comes as array
        res.json({
            data: data
        });
    })
});

app.post('/users/:id', (req, res) => {
    const { id } = req.params;

    //push at the end of list
    redisClient.rpush('mylist', id, (err, data) => {
        if(err) {
            return res.status(500).send('Error');
        }
        //data is the length of the list
        res.json({
            data: data
        });
    })
});

app.listen(3000, () => {
    console.log('server started at 3000');
});

Link to the source code is at the end of this article.

Is there any other way?

There is one more way that we can store Array data inside Redis. But it is not very efficient and you will have problems adding and removing items from the list. Let’s see an example,

> set userlist "['joseh', 'jeniffer']"

And then read the array from the key,

> get userlist
//returns
"['joseh', 'jeniffer']"

Which will return the array in string format, which you can then serialize in the Node side and send to the browser in the response.

Source code repo

You can find the full example on Github – https://github.com/jsphkhan/node-redis-array

Reference

If you liked this article and have any comments or suggestions, give me a shoutout in the comments section below.

Cheers!



If you enjoyed this post and want similar articles to be delivered to your inbox directly, you can subscribe to my newsletters. I send out an email every two weeks with new articles, tips & tricks, news, free materials. No spamming, of course.

Write a Comment

Your email address will not be published. Required fields are marked *