DEV Community

Cover image for How to use Redis with Express
Dev Sharma
Dev Sharma

Posted on • Originally published at blog.devsharma.live

How to use Redis with Express

Building an App that can cache

Let's take a look at trying to build an app without Redis that caches response data. We can use a global variable. As soon as the process is terminated, the variable will be destroyed and as you see we no longer have a persistent cache as we used a variable, which is ephemeral.

Redis to the rescue

redis-logo-cropped.png

Redis really becomes useful when you want to persist data in memory, for faster access that allows you to use it as a cache. So what does Redis do, Redis is an in-memory data store but a persistent on-disk database, meaning we can use Redis as a database that performs reads and writes directly into memory while also achieving persistence as the Redis server writes to disk from time to time.

Let's get coding

For this tutorial, I have decided to go with a combination of express as a server and mongoose ODM with JavaScript.

To work with Redis inside of a node server, we need a Redis driver and we are going to use the official Redis package.

Let's install the dependencies:

npm i express mongoose redis dotenv
npm i -D nodemon
Enter fullscreen mode Exit fullscreen mode

Once we have the set up complete, we can go ahead and configure our .env file.

MONGODB_URI=<your mongodb connection string>
Enter fullscreen mode Exit fullscreen mode

If you configured Redis locally, you can skip the REDIS_* variables, and just pass in the password.

If you used a docker container, spin it up with a port mapped to 6379 (or something else like 63791 here).

docker run --name some-redis -d -p 63791:6379 redis
Enter fullscreen mode Exit fullscreen mode

if you used a Redislabs instance, log in and select your database and go to the configuration page, where you can find your connection env variables.

Redis Labs config

.env

REDIS_HOST=<your Redis HOST>
REDIS_PORT=<your Redis PORT>
REDIS_PASSWORD=<your config PASSWORD>
Enter fullscreen mode Exit fullscreen mode

We are completely configured to use Redis in our app. To keep this article short I'm gonna skip over setting up mongoose and express, as this is a more generalised approach towards using Redis in any of your node apps.
(You can find the code for models, controllers, etc on the GitHub repository)

Let's set up the Redis Client in our index.js, add the following lines to your code.

index.js

import redis from "redis";
import { promisify } from "util";

// create redis client
const client = redis.createClient({
  host: process.env.REDIS_HOST,
  port: parseInt(process.env.REDIS_PORT),
  password: process.env.REDIS_PASSWORD,
});

// redis.js doesn't support async utils as of writing this article
// we can use the recommended workaround
export const getAsync = promisify(client.get).bind(client);
export const setAsync = promisify(client.set).bind(client);
Enter fullscreen mode Exit fullscreen mode

We will use getAsync to get the values from Redis store and setAsync to set values in our Redis store.

We need to modify our APIs to use Cache when we request data, via Get requests.

Express GET route

app.get("/api/<resource>", async (req, res) => {
  try {
    // Get from cache using the "Key"
    const getRes = await getAsync("resourceKey" );
    if (getRes)
      return res.json({ success: true, data: JSON.parse(getRes) });

    // On cache-miss => query database
    const users = await <Model>.find({});

    // Set cache
    await setAsync("resourceKey",   // Key
      JSON.stringify({res}),        // Value
      "EX",                         // Set explicit expiry
      60                            // TTL in seconds
    );

    return res.status(200).json({success: true, data: <resource>});
  } catch (error) {
    // Handle errors
  }
});
Enter fullscreen mode Exit fullscreen mode

Add the Cache getAsync and setAsync to all your GET routes and that's it. Now you have a basic cache setup with your express server.

πŸŽ‰πŸŽ‰πŸŽ‰ Super fast and efficient caching.

DB vs Cache

This blog is part of a Redis learning series, in the previous post, I have explained setting up Redis using 3 different ways.

Feel free to reach out to me on Twitter @cryptus_neoxys and connect with me on LinkedIn.

Refs

NPM Redis.js

yoursTRULY YouTube

Traversy Media

Top comments (0)