Optimizing Node.js Performance with Redis Caching
Tags: caching, node, webdev, redis
Published: 2023-01-26 05:00:59

Caching is necessary in Node.js because it can improve the performance and scalability of a web application. Caching allows frequently accessed data to be stored in a fast, in-memory store, so that it can be quickly retrieved without having to be recalculated or fetched from a slower, persistent storage layer. This can significantly reduce the load on the server, improve response times for users, and make the application more scalable by reducing the need for expensive database queries. Additionally, caching can also help to reduce the amount of data that needs to be transferred over the network, which can help to improve the overall user experience.
One popular tool for implementing caching in Node.js is Redis, an in-memory data store. In this article, we’ll walk through the steps for implementing Redis caching in a Node.js application.

Step 1: Install Redis server -
Windows:
- Download the Redis Windows installer from the official website (https://github.com/microsoftarchive/redis/releases)
- Run the installer to install Redis on your system.
- Open the Command Prompt and Run the command redis-cli to start the Redis server.

macOS:
- Install Redis using Homebrew by running the command brew install redis.
- Start the Redis server by running the command redis-server.

To check if the Redis server is running correctly, type PING in the terminal. The server should respond with PONG, indicating that the server is up and running.

Step 2: Install Redis Client for node.js

To use Redis in a Node.js application, we’ll need to install the Redis client for Node.js.

```
npm i redis
```

Step 3: Create a Redis client instance

Next, we’ll create a Redis client instance in our Node.js application.
```
const redis = require("redis");
const client = redis.createClient({
  host: "127.0.0.1",
  port: 6379,
});
```
When creating a Redis client, it’s important to specify the host and port on which the server is running, so that the client can easily connect to the server.

Step 4: Create middleware to handle caching

We’ll create a middleware function to handle caching in our Node.js application. This function will check if the requested data is already stored in the cache, and if so, it will return the cached data to the user. If the data is not in the cache, the function will call the next middleware in the chain and store the response in the cache for future requests.

```
function cache(req, res, next) {
  const key = "__express__" + req.originalUrl || req.url;

  client.get(key).then(reply => {
    
    if (reply) {
      res.send(JSON.parse(reply));
    } else {
      res.sendResponse = res.send;
      res.send = (body) => {
        //expire in 1 min
        client.set(key, JSON.stringify(body), {'EX':60});
        res.sendResponse(body);
      };
      next();
    }
  }).catch(err=>{
    console.log(err);
    res.status(500).send(err)
  });
}
```
In the cache function, we create a key based on the URL, so that we can retrieve and store data according to the URL and send the response. The function also sets a time-to-live (TTL) of one minute for the cached data, after which it will expire and the next request will have to retrieve the data again.

Step 5: Use the caching

middleware in your application
To use the caching middleware in our application, we’ll need to add it to the Express middleware stack using the app.use() function.

```
app.use(cache);
```
Step 6: Use the cache

In this example, we’ll use the caching middleware for a simple endpoint that generates a large amount of data.

```
app.get("/data", (req, res) => {
  let data = 0;
  for (let i = 1; i < 100000000; i++) {
    data += 1;
  }
  res.json(data);
});
```
Step 7: Connecting redis client with server

```
app.listen(3000, () => {
  console.log("Server listening on port 3000");
  client.connect().then(()=> {
    console.log('redis is connected')
  })
});
```
Upon initiating the Express server, we established a connection between the Redis client and its corresponding server, ensuring seamless data storage and retrieval.

Note:
For a detailed, line-by-line implementation, be sure to check out our repository file on [Caching in node.js](https://github.com/aquibzahidi/Advance-NodeJs/blob/main/performance/redis.js)
For accessing more advance Node.Js topics, visit out repository on [Advance Node.js.](https://github.com/aquibzahidi/Advance-NodeJs)

By implementing Redis caching, you can significantly improve the performance and scalability of your Node.js application. Caching is a powerful tool for reducing server load and improving response times, and Redis is a popular choice for implementing caching in Node.js. By following the steps outlined in this article, you can easily add Redis caching to your Node.js application and start reaping the benefits of improved performance and scalability.