DEV Community

Cover image for Getting Started with Redis in Nodejs
Keith Holliday
Keith Holliday

Posted on

Getting Started with Redis in Nodejs

This was originally posted here: https://koalatea.io/node-js-redis-getting-started/

Intro

When building large scale applications, there comes a need for scaling. There are many places to start with scaling, but one place my be scaling your reads. Let's say that you have a read heavy application, like an ecommerce store or a comment system.
You may want to consider caching to address this concerns. Redis is a good place to start (and to end) when solving this problems.
In this article we will get started with Redis in Nodejs.

More about Redis and Caching

Caching is a large topic that is outlined here: https://github.com/donnemartin/system-design-primer#cache. We will simply introduce Redis here and in later articles, we will learn to implement this practices on large scale features.

Redis is use for a lot more than caching. For example, queues are implemented in Redis using bullqueue: https://optimalbits.github.io/bull/. I highly recommend checking out this package (and the bullq UI). This is a great start to scaling out services, not just microservices. You can
read about more use cases for redis here: https://redis.com/blog/5-industry-use-cases-for-redis-developers/, and I will write articles in the future about implementing this features later on.

Creating the project

Let's create the project as follows.

mkdir redis-example
cd redis-example
npm init -y
touch index.js
touch docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Ensure you have docker installed and run

docker-compose up
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Writing the Code

Let's start by opening up the index.js file and importing our module. We will also connect to the redis server. By default, the module will assume we are using localhost on port 6379, which is what we setup in our docker compose file.

const Redis = require("ioredis")

const redis = new Redis()
Enter fullscreen mode Exit fullscreen mode

Next, let's run some redis commands. We will start very basic with the set and get commands. As implied by the names, the set command will set a key and the get will retrieve the key.

async function main() {
    const setResult = await redis.set("foo", "bar")
    console.log(setResult)

    const getResult = await redis.get("foo")
    console.log(getResult)
}
Enter fullscreen mode Exit fullscreen mode

Note, I usually create a main funciton to start a node file that will be an entry. We can call the above using a self invoking funciton. Eventually in later version of node we will not need this as we will be able to call await at the root level.

(async () => {
    await main()
})()
Enter fullscreen mode Exit fullscreen mode

Here is the full file for context.

const Redis = require("ioredis")

const redis = new Redis()

async function main() {
    const setResult = await redis.set("foo", "bar")
    console.log(setResult)

    const getResult = await redis.get("foo")
    console.log(getResult)
}

(async () => {
    await main()
})()
Enter fullscreen mode Exit fullscreen mode

A Redis GUI

Often over looked in the community is the use of a UI. Many are outdated or hard to run. One that I often use is patrikx3/redis-ui. Although a little
clunky, it usually does what I need. I will also suggest getting use to the redis-cli to help where GUIs cannot.

You can download the GUI here: https://github.com/patrikx3/redis-ui.

Once you have that downloaded, open up the app. Then go to Settings -> New Connection.

new connection

Type in the following to connect to local, then hit the "Add" button at the bottom.

new connect

Finally, click the bottom right, then select your localhost to connect.

redis add

Click Home and then you should see a screen like below, but with no keys on the left.

new home

Top comments (0)