Redis: Enhancing Application Performance with Caching

Redis: Enhancing Application Performance with Caching

Redis is an in-memory data structure store used in modern application development for its ability to significantly enhance performance through caching. Unlike traditional databases that store data on disk, Redis stores data in-memory, enabling rapid access and retrieval of information. This feature makes Redis particularly valuable for scenarios where speed and responsiveness are critical.


In the provided example code, Redis is utilized to cache data fetched from an external API. Here's how it works:

Step-by-Step Setup

Step 1: Set Up Redis with Docker (You need to have Docker Engine installed on your machine)

First, we need to set up a Redis server. We will be using Docker to simplify this process, ensuring a consistent environment across different deployments. Run the following command to start a Redis server in a Docker container:

docker run -d --name my-redis -p 6379:6379 -p 8001:8001 redis/redis-stack:latest        

This command does the following:

  • -d: Runs the container in detached mode.
  • --name my-redis: Names the container my-redis.
  • -p 6379:6379: Maps port 6379 of the container to port 6379 on the host, which is the default port for Redis.
  • -p 8001:8001: Maps port 8001 of the container to port 8001 on the host for a GUI interface to interact with Redis (not recommended for production uses).
  • redis/redis-stack:latest: Specifies the Redis image to use.

Step 2: Set Up the Node.js Application

Create a Node.js application and install the necessary dependencies. Initialize your project and install express, axios, and ioredis.

npm init -y
npm install express axios ioredis        

Step 3: Configure Redis Client

Create a file named client.js to configure the Redis client using ioredis:

Setting up Redis Client to interact with the Redis Server

Step 4: Create the Express Server

Create a server.js, set up an Express server and use Redis to cache data fetched from an external API:

import express from "express";
import redis from "./client";
import axios from "axios";
const app = express();


app.get("/getdata", async (req, res) => {
    const cache = await redis.lrange("dummydata:1", 0, -1);
    if (cache.length > 0) {
        return res.json(cache.map((c: any) => JSON.parse(c)));
    }
    const { data } = await axios.get("https://jsonplaceholder.typicode.com/posts");
    data.forEach(async (post: any) => {
        await redis.rpush("dummydata:1", JSON.stringify(post));
    })
    await redis.expire("dummydata:1", 20);
    return res.json(data);
})


app.listen(3000, () => {
    console.log("Server is running on port 3000");
}
);        

  • redis.lrange("dummydata:1", 0, -1): Retrieves all elements from the list stored at the key "dummydata:1".
  • redis.rpush("dummydata:1", JSON.stringify(post)): Adds a new element to the end of the list stored at the key "dummydata:1".
  • redis.expire("dummydata:1", 20): Sets a time-to-live (TTL) of 20 seconds on the key "dummydata:1", after which the key will be automatically deleted.


When a user requests data from the application (/getdata endpoint):

First Request: If the requested data is not already cached in Redis, the application fetches it from the external API. This initial fetch might take some time, typically around 100-200 milliseconds, depending on network conditions and API response times.

Getting Data from the Database (Cache Miss)


Subsequent Requests: Redis stores the fetched data in-memory and sets an expiration time (20 seconds in this case). For subsequent requests within this timeframe, the application retrieves the data directly from Redis. This operation is blazingly fast, often completing in just 10-15 milliseconds or less, due to Redis' in-memory storage and O(1) time complexity for data retrieval.

Getting Data from Redis Cache (Cache Hit)

Getting data from Redis Cache(Cache hit)


Reducing Load on External Systems: By caching data with Redis, applications reduce the load on external APIs or databases. Instead of repeatedly querying these external resources for the same data, Redis serves as a high-performance intermediate layer. This not only speeds up application response times but also improves the overall scalability and reliability of the application by reducing dependency on external systems.

Shared Memory Considerations

Redis and your application do not share the same memory pool. Redis operates as an entirely separate service that your application communicates with over a network connection.

For small-scale, single-instance Node.js services, an in-memory cache within the Node.js process might suffice, provided there is enough available RAM. However, as soon as you scale your Node.js service horizontally (i.e., running multiple instances), a shared caching service like Redis becomes essential. This is because each Node.js process will have its own memory space, and they won’t share their in-process memory cache.

By using Redis or a similar shared caching service, all instances of your Node.js application can access a common cache, ensuring consistency and improving performance across multiple processes.

Conclusion

Redis caching is a powerful tool in optimizing application performance. By storing frequently accessed data in-memory, Redis minimizes latency, improves throughput, and enhances user experience. Whether used for caching API responses, session data, or frequently accessed database records, Redis helps developers to build fast, scalable, and resilient applications.


There are various other applications of Redis such as its Pub/Sub architecture which is essential for real-time communication and notifications.

Redis is also used for session management to store user session in the server's memory.

Redis Streams are designed to handle high throughput data ingestion and processing which are used to collect and process large amount of data efficiently. This can be used for tracking user click activity on a website and processing them and storing user preferences in the database.

We only store the computed value in the database since unlike Redis Streams, the databases are not meant to have high data ingestion capabilities simultaneously.

We will discuss about other applications of Redis in depth in my upcoming blogs. So stay tuned for them!

And as always, thank you for sticking till the end.

要查看或添加评论,请登录

Shaikh Abdul Sami的更多文章

社区洞察

其他会员也浏览了