Redis cache- Caching API request

This exercise walks through how to set up and how to use Redis as a cache.

Objective

Set up Redis to cache API requests

Description

Caching can help to boost the performance of your application by saving the results and avoiding network requests for a specified period of time and thus making your application faster.

In this exercise, set up caching for your application using Redis. We will be making API requests to a mock API endpoint to fetch data. You should respond to the request with the response received from the API and also save this data in the Redis cache. If the data is already saved in the cache, then respond with the cached data.

Acceptance criteria

  • Install Redis on your local system
  • Create an Express server
  • Create a GET route and make a request to the endpoint to fetch posts.
  • Use the following endpoint to make the GET request: https://jsonplaceholder.typicode.com/posts
  • If the request is made for the first time, respond with the response received and save the returned data as a cache.
  • Save the data in cache for 3600 seconds
  • If the data is saved in the cache, respond with the cached data.

Hints

Install Redis on your local machine. For MAC installation:

brew install redis

For other platforms, please follow the guide on https://redis.io/download.


To start Redis, you the following command:

redis-server

To start the Redis CLI:

redis-cli

 

Install Redis and import in your application:

npm i redis
const redis = require('redis');

 

Install and import Axios or use node-fetch to make HTTP requests

npm i axios
const axios = require('axios');

 

Define a port number for our express app and the port number on which redis is running on your local machine.

const port = 3457;
const redis_port = 6379;

 

Create a redis client

const client = redis.createClient(redis_port);

Where redis_port is a port where redis is running on your system

 

Use client.set() to save data in cache.

Use client.get() to get data from cache.

Create a middleware for cache function for simplicity

 

Solution

Introduction

Caching is the process of storing data into a cache. A cache is a temporary data store where data is kept for later use. A cache as a data store is easier for the client (or server) to reach, as opposed to a permanent data store that might be located on a different service, which takes more time and resources to reach (a database or an external API endpoint).

If we have some data coming from a third-party API, and that data won’t be changed in the “near future,” we can store them in cache once we retrieve them and avoid unnecessary service calls. In this way, we do not have to wait for the API call to complete, as we already have them, and we can retrieve them from the cache. This will make our application much faster and more performant.

What is Redis?:

Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker.

 

How to use Redis Cache

A Redis server can be used as a cache by connecting to it from a client and storing data in its key-value store. When data is stored in a Redis cache, it is stored in memory, which makes retrieval very fast. Data that is frequently accessed can be stored in a Redis cache so that it can be quickly retrieved when needed. To use Redis as a cache, the client must first connect to the Redis server. Once connected, the client can then set key-value pairs and expiry times for the data that is to be cached. When retrieving data from the cache, the client simply needs to specify the key for the desired data. If the key exists, Redis will return the cached data; if not, it will return null. By using Redis as a cache, clients can benefit from faster data retrieval times.

 

How to clear Redis Cache 

One of the benefits of using Redis is that it can help improve the performance of your application by caching data. However, there are times when you may need to clear the Redis cache in order to refresh the data. Clearing the Redis cache means flushing all of the data from the Redis database. First, you will need to connect to Redis using the redis-cli client. Once you are connected, you can use the flushall command to clear all of the data from the cache.

redis-cli flushall

Alternatively, you can use the flushdb command to clear only the data from the current database. Finally, you can use the keys command to view a list of all of the keys in the cache. By carefully selecting which keys to delete, you can selectively clear the Redis cache without deleting all of the data.

redis-cli flushdb

Prerequisites:

Install Redis on your local machine. For MAC installation:

brew install redis

For other platforms, please follow the guide on https://redis.io/download.


To start Redis, you the following command:

redis-server

To start the Redis CLI:

redis-cli

 

Project setup

Start by creating a new directory for the project.

mkdir redis-app

The move into this directory and scaffold a new project using the following npm command:

npm init

This creates a new file package.json . It holds the project’s metadata such as name , description and it is used for managing the project’s dependencies, scripts, and version.

Next, install Express as a dependency.

npm i express

Install Redis and Axios

npm i redis axios

Create a new file index.js in our folder, which will be the main entry point to our application.

 

Code Solution

 


const express = require("express");
const redis = require("redis");
const axios = require("axios");

const port = 3457;
const redis_port = 6379;

const client = redis.createClient(redis_port);

const app = express();

function getPosts(req, res) {
  try {
    axios.get("http://jsonplaceholder.typicode.com/posts").then((resp) => {
      // Set data to redis
      client.setex("blogs", 60, JSON.stringify(resp.data));
      res.send(resp.data);
    });
  } catch (err) {
    res.status(500);
  }
}

//Cache middleware
function cache(req, res, next) {
  client.get("blogs", (err, data) => {
    if (err) throw err;
    if (data !== null) {
      res.send(JSON.parse(data));
    } else {
      next();
    }
  });
}

app.get("/", cache, getPosts);

app.listen(port, () => console.log(`Listening on port ${port}`));

 

Code Walkthrough

Start off by requiring express in our index.js file and save it to a constant variable. Next, execute express() like a function that will spin up an express application. Save this in another constant variable called app.

const express = require("express");
const app = express();

Require redis and save it in another constant variable.

const redis = require("redis");

 

Also, import Axios since we will be making API requests.

const axios = require('axios');

Define a port number for our express app and another port number on which Redis is running on your local machine.

const port = 3457;
const redis_port = 6379;

Create a Redis client

const client = redis.createClient(redis_port);

Where redis_port is a port where Redis is running on your system

 

Creates a GET route handler:

app.get("/", cache, getPosts);

Fetch the posts from the API:

function getPosts(req, res) {
  try {
    axios.get("http://jsonplaceholder.typicode.com/posts").then((resp) => {
      // Set data to redis
      client.setex("blogs", 3600, JSON.stringify(resp.data));
      res.send(resp.data);
    });
  } catch (err) {
    res.status(500);
  }
}

 

client.setex() is used to save data in cache. It takes 3 parameters:

  • A key
  • seconds (time for expiration)
  • data to be saved. Here “blogs” is the key and the expiration time is set to 3600 seconds which is one hour.

 

In order to maximize our application performance, we will implement a cache middleware that will search for a key on the Redis local server before requesting new data.

Cache middleware is called before getPosts, this means it will first check if the cache is present. If not, the control is passed to the next handler. This is how we create a cache in a Node.js application.

//Cache middleware
function cache(req, res, next) {
  client.get("blogs", (err, data) => {
    if (err) throw err;
    if (data !== null) {
      res.send(JSON.parse(data));
    } else {
      next();
    }
  });
}

 

client.get() is used to get the data from the cache for the key “blogs”. If data is available in the cache, it will respond with the data. If not the control is passed to the next middleware.

 

We can see that is a big difference while using cache. It took only four milliseconds, while it took 1 second without it. The reason is that we have already had this data retrieved before and we have stored them in memory with Redis as a key-value pair. Every time we will call this endpoint until 1 hour, the server will not make a real request to the API and will serve as the data that it has in memory.

 

As we can see from the example above, using cache could be beneficial for our application. Redis is blazingly fast; it’s a NoSQL database and in order to save your cloud database calls.

how to use redis cache
how to clear redis cache
clear redis cache