DEV Community

Jayvee Ramos
Jayvee Ramos

Posted on

Optimizing Data Retrieval with Redis Caching in Your Express App

Optimizing Data Retrieval with Redis Caching in Your Express App

In our previous tutorials, we successfully set up Redis and explored basic operations like adding and retrieving data. Now, let's take our application to the next level by incorporating Redis caching to dramatically improve data retrieval speed.


Simulating Real-World Scenarios:

In a real-world scenario, our application may need to fetch data from an external API or a database. To simulate this, we'll make an HTTP request to the JSONPlaceholder API, a free API for testing and prototyping. This API allows us to retrieve posts using a dynamic post ID.

Here's a glimpse of the Express route:

make sure to install axios just run this command on your terminal npm install axios

// index.js

// index.js

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

const app = express();
const port = 8080;

const client = redis.createClient({
  host: "127.0.0.1",
  port: 6379,
});

client.on("error", (error) => {
  if (error) {
    console.error("ERROR***", error);
  } else {
    console.log("Redis connect.");
  }
});

(async () => {
  client.connect();
})();

app.use(express.json());

// Endpoint to add data to Redis
app.post("/", async (req, res) => {
  const { key, value } = req.body;

  // Set data in Redis
  const response = await client.set(key, value);

  // Respond with the Redis response
  res.json(response);
});

app.get("/:key", async (req, res) => {
  const { key } = req.params;

  // Get data from Redis
  const value = await client.get(key);

  // Respond with the Redis value
  res.json({ key, value });
});

app.get("/existing/:key", async (req, res) => {
  const { key } = req.params;
  // Check if data exists in Redis
  const redisData = await client.get(key);

  if (redisData) {
    // If data is in Redis, return it
    res.json({ key, redisData });
  } else {
    // If not, interact with the database and store the data in Redis
    const databaseData = await fetchDataFromDatabase(key);
    await client.set(key, databaseData);

    // Return the data fetched from the database
    res.json({ key, databaseData });
  }
});

// Endpoint to get posts from JSONPlaceholder API
app.get("/posts/:id", async (req, res) => {
  const id = req.params.id;

  // Check if the post is cached
  const cachedPost = await client.get(`post-${id}`);

  if (cachedPost) {
    // If cached, return the data
    return res.json(JSON.parse(cachedPost));
  }

  // If not cached, make an HTTP request to JSONPlaceholder API
  const response = await axios.get(
    `https://jsonplaceholder.typicode.com/posts/${id}`
  );
  const postData = response.data;

  // Cache the retrieved data in Redis
  await client.set(`post-${id}`, JSON.stringify(postData));

  // Return the retrieved data
  res.json(postData);
});

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

Enter fullscreen mode Exit fullscreen mode

Leveraging Redis Caching for Faster Responses:

Now, let's break down the logic:

  1. We check if the requested post is already cached in Redis using the key post-${id}.
  2. If the post is cached, we retrieve and return it, avoiding the need for an external API request.
  3. If not cached, we make an HTTP request to the JSONPlaceholder API to get the post data.
  4. We cache the retrieved data in Redis for future use.
  5. Finally, we return the post data.

Enhancing Speed with Redis Cache:

By using Redis caching, we've significantly optimized our application. The first request to an uncached post might take some time due to the API call, but subsequent requests for the same post are lightning-fast, thanks to the cached data.


Handling Data Updates:

In real-world applications, we must consider how to handle data updates. If a post gets updated or deleted, we need to ensure our cache is updated accordingly. This often involves implementing cache invalidation or expiration strategies.


In our next section, we'll explore strategies for handling data updates in a Redis-cached environment. Stay tuned for more Redis and Express optimization tips!

Top comments (0)