This article will examine implementing data caching in Node.js using Redis, a high-speed, in-memory database. Caching is vital for improving application performance and scalability by lessening the burden on primary data sources. Redis is particularly noteworthy for its quick data retrieval capabilities, making it an ideal option for setting up caching layers in Node.js applications.
Throughout this article, we’ll cover the fundamentals of caching, and the benefits of Redis, and offer practical examples of integrating Redis into Node.js projects to optimize data access. Let’s explore how Redis can effectively enhance data caching in Node.js applications.
Node.js Environment: Ensure Node.js is installed on your development machine for running the backend application.
npm (Node Package Manager): Ensure npm is available for installing packages, including the Firebase Admin SDK.
Redis Server: You must have Redis installed and running on your system or accessible via a hosted service (like Redis Labs, Amazon ElastiCache, etc.).
Install Redis Server locally by below steps:
1. Install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2. Install Redis Using Homebrew:
brew install redis
3. Start Redis:
brew services start Redis
1. Update Package Index:
sudo apt update
2. Install Redis:
sudo apt install redis-server
3. Start Redis:
sudo systemctl status redis-server
If it’s not running, start Redis manually with:
sudo systemctl start redis-server
Verifying Redis Installation:
1. After installation, you can verify Redis is running by connecting to it using the Redis CLI:
redis-cli ping
If Redis is running, you’ll get a PONG response.
2. You can also check the version of Redis installed:
redis-server --version
1. Real-Time Data Storage:
Redis is a powerful database system that stores and manipulates data entirely in memory. This approach allows for extremely rapid data access and retrieval, making it ideal for applications that require real-time responsiveness.
2. Diverse Data Structures and Operations:
Redis supports a variety of data structures beyond simple key-value pairs, including lists, sets, hashes, and sorted sets. Each structure has a rich set of commands tailored for efficient data manipulation and retrieval.
3. Data Durability Options:
Despite its in-memory nature, Redis provides options for data persistence to ensure data durability. It can periodically save snapshots of the dataset to disk or log every write operation, providing recovery capabilities in case of failures.
4. Efficient Caching Solution:
One of Redis’s primary use cases is caching. By storing frequently accessed data in Redis’s memory cache, applications can reduce latency and improve overall system performance by avoiding repetitive database or API calls.
5. Advanced Functionality:
Redis offers advanced features like Pub/Sub messaging for real-time communication, atomic transactions for ensuring data integrity, Lua scripting for custom business logic, and clustering for scalability and fault tolerance in distributed environments.
Caching within API environments involves the temporary storage of often-requested data in a swiftly accessible location, such as memory or a specialized caching service. This strategy enables subsequent requests for the same data to be rapidly fulfilled without the need to repeat the entire data retrieval or computation process.
Instead of consistently querying backend systems like databases or external services for static or slowly changing information, cached responses can be directly delivered to clients, resulting in reduced response times and enhanced overall system efficiency.
1. High-Performance In-Memory Data Storage:
2. Automatic Data Expiration:
3. Versatile Data Structures and Commands:
4. Efficient Cache Eviction Policies:
5. Scalable and Robust Framework:
“Dive deep into Redis’s comprehensive documentation to learn advanced caching strategies and how to leverage Redis for optimizing API performance by reducing response times.
Read more here: Redis Documentation .
1. Setup:
2.Navigate to Project Directory:
cd path/to/your/project
3. Initialize Node.js Project:
npm init -y
4. Install Required Packages:
Install express, dotenv, axios, cors, and response-time using npm:
npm install express dotenv axios cors response-time
5. Create .env File:
PORT=5002
6. Create app.js File:
const express = require("express");
const dotenv = require("dotenv");
const axios = require("axios");
const cors = require("cors");
const responseTime = require("response-time");
dotenv.config({ path: ".env" });
const PORT = process.env.PORT || 5002;
const app = express();
app.use(responseTime());
app.use(cors());
const getPosts = async (req, res) => {
try {
const response = await
axios.get("https://jsonplaceholder.typicode.com/posts");
const posts = response.data;
console.log("Posts from API");
res.status(200).json({ data: posts });
} catch (error) {
console.error("Error fetching posts:", error);
res.status(500).send("Error in Fetching Posts");
}
};
app.get("/posts", getPosts);
app.listen(PORT, () => {
console.log(`App is running on ${PORT}`);
});
7. Run the Server:
Start the Express server by executing the following command in the terminal:
node app.js
8. Test the API Endpoint:
http://localhost:5002/posts
You should receive a JSON response containing posts fetched from the JSONPlaceholder API.
Related read: Boost Node.js App Speed: Caching Using Redis
Integrating caching functionality using Redis into your existing Node.js application to enhance data retrieval efficiency and optimize system performance we have to install the redis dependencies and other packages required for implementing caching.
1. Install Required Packages:
npm install redis
2.Add the REDIS_PORT into Your .env File:
REDIS_PORT=6379
3. Create a Redis Client Using createClient Function Provided by Redis Package and Connect the Redis Server:
const redis=require(‘redis’);
const redisClient = redis.createClient({
legacyMode: true,
PORT: REDIS_PORT,
});
redisClient.connect();
redisClient.on("error", (error) => {
console.error("Redis connection error:", error);
});
Important Note: In node-redis V4, the client does not automatically connect to the server, you need to run .connect() before any command, or you will receive the error ClientClosedError: The client is closed.
import { createClient } from 'redis';
const client = createClient();
await client.connect();
Or you can use legacy mode to preserve the backward compatibility.
const client = createClient({
legacyMode: true
});
4. Now Implement the Caching in getPosts Function Using Redis Client Get and Set Command:
First, understand what are GET and SET commands:
1. GET Command:
The GET command is used to retrieve the value stored at a specified key in Redis.
Syntax: `GET key`.
> SET mykey "Hello"
OK
> GET mykey
"Hello"
2. SET Command:
Syntax: `SET key value [EX seconds] [PX milliseconds] [NX|XX]`.
> SET mykey "Hello"
OK
EX Ceconds: Set an expiration time in seconds.
PX Milliseconds: Set an expiration time in milliseconds.
NX: Set the key only if it does not already exist.
XX: Set the key only if it already exists.
> SET mykey "Hello" EX 3600 # Set 'mykey' with expiration time of 3600 seconds (1 hour)
OK
> SET mykey "Hello"
OK
If the key mykey already exists, executing SET mykey “Hello” will overwrite the existing value.
const getPosts = async (req, res, next) => {
const cacheKey = "posts";
redisClient.get(cacheKey, async (err, cachedPosts) => {
try {
if (err || !cachedPosts) {
const response = await axios.get(
`https://jsonplaceholder.typicode.com/posts`
);
const posts = response.data;
redisClient.set(cacheKey, JSON.stringify(posts),
"EX", 3600);
console.log(`Posts from API`);
res.status(200).send({ data: posts });
} else {
console.log(`Posts from Redis`);
res.status(200).send({ data:
JSON.parse(cachedPosts) });
}
} catch (error) {
res.status(500).send("Error in Fetching Posts");
}
});
};
5. Your Final Code in Index.js File Will Look Like This:
const express = require("express");
const dotenv = require("dotenv");
const axios = require("axios");
const cors = require("cors");
const redis = require("redis");
const responseTime = require("response-time");
dotenv.config({ path: ".env" });
const PORT = process.env.PORT || 5002;
const REDIS_PORT = process.env.REDIS_PORT || 6379;
const app = express();
app.use(responseTime());
app.use(cors());
const redisClient = redis.createClient({
legacyMode: true,
PORT: REDIS_PORT,
});
redisClient.connect();
redisClient.on("error", (error) => {
console.error("Redis connection error:", error);
});
const getPosts = async (req, res, next) => {
const cacheKey = "posts";
redisClient.get(cacheKey, async (err, cachedPosts) => {
try {
if (err || !cachedPosts) {
const response = await axios.get(
`https://jsonplaceholder.typicode.com/posts`
);
const posts = response.data;
redisClient.set(cacheKey, JSON.stringify(posts),
"EX", 3600);
console.log(`Posts from API`);
res.status(200).send({ data: posts });
} else {
console.log(`Posts from Redis`);
res.status(200).send({ data:
JSON.parse(cachedPosts) });
}
} catch (error) {
res.status(500).send("Error in Fetching Posts");
}
});
};
app.get("/posts", getPosts);
app.listen(PORT, () => {
console.log(`App is running on ${PORT}`);
});
6. Run the Server:
node app.js
7. Test the API Endpoint:
http://localhost:5002/posts
You should receive a JSON response containing posts fetched from the JSONPlaceholder API.
Response Explanation:
Integrating Redis caching into your Node.js application yields significant performance improvements. Without caching, response times average over 200ms due to repeated data retrieval from the server. However, with Redis, response times plummet to a mere 1ms, showcasing the dramatic efficiency gains from cached data.
This optimization extends beyond just speed. By minimizing repeated server requests, Redis caching reduces strain on resources and enhances overall system responsiveness. This translates to a noticeably smoother user experience with faster response times. As a strategic solution, Redis caching offers a powerful way to streamline data retrieval and optimize performance in your Node.js applications. Remember, ongoing monitoring and utilization are key to sustaining these benefits over time.
The team at Mindbowser was highly professional, patient, and collaborative throughout our engagement. They struck the right balance between offering guidance and taking direction, which made the development process smooth. Although our project wasn’t related to healthcare, we clearly benefited...
Founder, Texas Ranch Security
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork