Getting Started with Redis in Nodejs
Tags: node, redis, javascript
Published: 2021-08-24 00:16:14

---
title: 'Getting Started with Redis in Nodejs'
published: true
description: "In this article we will get started with Redis in Nodejs."
tags: ['node', 'redis', 'javascript']
cover_image: https://koalatea.io/static/3e8b4c0e1152436f37932c914c93f96e/8ec66/blocks.jpg
---

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.

```bash
mkdir redis-example
cd redis-example
npm init -y
touch index.js
touch docker-compose.yml
```

## 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.

```yml
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

```bash
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.

```bash
npm install ioredis
```

## 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.

```js
const Redis = require("ioredis")

const redis = new Redis()
```

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.

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

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

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.


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

Here is the full file for context.

```js
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()
})()
```

## 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](https://koalatea.io/static/a9aa100971642b0a2968ebf9fd132e7e/1c3a5/redis-new-connection.png "redis connection")

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

![new connect](https://koalatea.io/static/64121405161e88741cc02fba8e8b5a04/914c7/redis-connect.png "redis connect")

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

![redis add](https://koalatea.io/static/b1eba0ceca5c6320e9f02e3e1c32aede/87a80/redis-add.png "redis add")

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

![new home](https://koalatea.io/static/cc0ddbcc3bb0e61d9e5053f76e8aa50d/d30ee/redis-home.png "redis home")