This project demonstrates a backend application implementing caching strategies using NestJS, Redis, and Prisma. It utilizes the Cache-Aside (Lazy Loading) pattern to optimize database retrieval performance.
Current Status: Work in Progress.
- Runtime: Node.js
- Framework: NestJS
- Language: TypeScript
- Database: PostgreSQL (via Prisma ORM)
- Caching: Redis
- Containerization: Docker
- Docker
- Node.js (Latest LTS recommended)
- Postman (optional, for API testing) or Terminal
- Redis Insight (optional)
Create a .env file in the root directory (or inside your backend folder). Ensure the following variables are set to match your Docker configuration:
DATABASE_URL="postgres://user:password@localhost:5432/test_data?connection_limit=10"
REDIS_HOST="localhost"
Run the database and Redis containers using Docker Compose:
docker compose up -d
Install dependencies for both the shared packages and the backend application (Monorepo structure):
# 1. Install backend dependencies
cd ../../apps/backend
npm install
# 2. Install shared packages
cd shared/packages
npm install
Generate the Prisma client and synchronize the schema with the database. This creates the necessary tables in your PostgreSQL container:
# Run inside the shared/packages directory
npm run build
Start the backend server in development mode:
npm run start:dev
You can verify the caching mechanism using Postman or your terminal (cURL).
Create a new article. This operation writes directly to PostgreSQL.
- URL: http://localhost:3000/articles
- Method: POST
Terminal Command:
curl -X POST http://localhost:3000/articles \
-H "Content-Type: application/json" \
-d '{"name": "Redis Architecture", "description": "Deep dive into caching strategies"}'
Copy the id returned in the response.
Fetch the article using the ID from Step 1.
- URL: http://localhost:3000/articles/<YOUR_ARTICLE_ID>
- Method: GET
Terminal Command:
curl -X GET http://localhost:3000/articles/<YOUR_ARTICLE_ID>
Paste id copied from post response
Observation:
- Response time: ~20–30ms (only visible from postman)
- System Behavior: Data is fetched from the Database, then stored in Redis.
Fetch the same article again immediately.
Observation:
- Response time: ~1–5ms
- System Behavior: Data is fetched directly from Redis RAM (The Database is not queried).
To inspect stored keys and values visually:
- Install Redis Insight.
- Connect to the database using redis://localhost:6379.
- Verify that keys are created after the first GET request.