Skip to content

0-nira-0/redis_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis-based Backend Application

Overview

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.

Tech Stack

  • Runtime: Node.js
  • Framework: NestJS
  • Language: TypeScript
  • Database: PostgreSQL (via Prisma ORM)
  • Caching: Redis
  • Containerization: Docker

Prerequisites

  • Docker
  • Node.js (Latest LTS recommended)
  • Postman (optional, for API testing) or Terminal
  • Redis Insight (optional)

Installation & Setup

1. Environment Configuration

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"

2. Start Infrastructure

Run the database and Redis containers using Docker Compose:

docker compose up -d

3. Install Dependencies

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

4. Database Setup

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

5. Run Application

Start the backend server in development mode:

npm run start:dev

Usage & Testing

You can verify the caching mechanism using Postman or your terminal (cURL).

Step 1: Create Data (Write to DB)

Create a new article. This operation writes directly to PostgreSQL.

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.

Step 2: Retrieve Data (Cache Miss)

Fetch the article using the ID from Step 1.

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.

Step 3: Retrieve Data Again

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).

Monitoring

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors