This project demonstrates and benchmarks the performance difference between fetching data directly from MongoDB versus using Redis as a caching layer. It includes a Node.js/Express application and scripts to run load tests using autocannon.
Ensure you have the following installed and running on your machine:
If you don't have MongoDB or Redis installed locally, run them via Docker:
docker run -d --name mongo -p 27017:27017 mongo
docker run -d --name redis -p 6379:6379 redis- Clone the repository:
git clone https://github.com/Ismail-047/redis.git
cd redis- Install dependencies:
npm install- Configure Environment Variables:
Create a
.envfile in the root directory by copying the sample file:
cp .env.sample .env- Seed the Database: Import the sample product data into MongoDB before starting the application:
mongoimport --uri "mongodb://localhost:27017/redis-cache" --collection products --file products.json --jsonArrayIf using Docker, copy the file into the container first:
docker cp products.json mongo:/products.json
docker exec mongo mongoimport --uri "mongodb://localhost:27017/redis-cache" --collection products --file /products.json --jsonArrayStart the development server:
npm run devThe server will start on http://localhost:3000.
This project includes two benchmark scripts to compare performance. Make sure the server is running in a separate terminal before executing these scripts.
Run the benchmark that requests the endpoint using Redis for caching:
node src/benchmark-with-redis.jsRun the benchmark that requests the endpoint fetching directly from MongoDB every time:
node src/benchmark-without-redis.jsFor a detailed performance comparison including latency metrics, and throughput analysis see:
📊 Redis Comparison Results.pdf
src/index.js: Main application entry point, sets up Express, connects to DBs.src/benchmark-with-redis.js: Autocannon script testing the Redis-cached route.src/benchmark-without-redis.js: Autocannon script testing the direct MongoDB route.src/lib/redis.js: Redis client configuration.src/db/db.js: MongoDB connection setup.products.json: Sample product data for seeding the database.1. Redis Comparison Results.pdf: Detailed benchmark comparison report.