Skip to content

About

Built this to learn Redis caching the right way. Shows how to cut API response times from 500ms to under 10ms using cache-aside patterns. Includes examples with strings, hashes, and lists.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

🚀 Node-Redis Performance Lab

Node.js Redis Express License

A high-performance backend demonstration showcasing Redis integration with Node.js. This repository illustrates how to use Redis as both a lightning-fast cache and a versatile data structure store to build scalable, low-latency applications.

🧐 Why This Repository?

Modern applications face two major challenges: Latency and Database Overload. This project demonstrates industry-standard solutions:

  • API Caching: Reducing response times from hundreds of milliseconds to near-instant.
  • Data Persistence: Managing state with Strings, Hashes, and Lists.
  • Cost Optimization: Reducing calls to expensive third-party APIs.

🛠️ Features & Implementations

1. API Caching (Cache-Aside Pattern)

In server.js, I implemented a middleware-style logic that caches external API data from JSONPlaceholder.

  • First Request: Fetches from the external API and stores it in Redis.
  • Subsequent Requests: Served directly from Redis memory in < 10ms.
  • Self-Cleaning: Automatically expires cache every 30 seconds to ensure data freshness.

2. Redis Data Structures

  • Hashes (hash.js): Optimized storage for objects (User profiles). Allows accessing specific fields without fetching the entire object.
  • Lists (list.js): Implementation of a message queue or activity feed using LPUSH and LPOP.
  • Strings (string.js): Fast key-value storage with TTL logic for session management.

3. Connection Singleton

  • client.js: Centralized Redis client configuration using ioredis, ensuring a single connection instance is reused across the application to prevent memory leaks.

📊 Performance Impact

Request Type Source Latency (Avg)
Initial Fetch External API ~350ms - 500ms
Cached Fetch Redis Memory 5ms - 12ms

Optimization result: ~97% reduction in response time.

🚀 Getting Started

Prerequisites

  • Node.js (v16 or higher)
  • Redis Server (Running on localhost:6379)

Installation

  1. Clone the repository:
git clone https://github.com/dharamdan01/redis-nodejs-performance-lab.git
cd redis-nodejs-performance-lab
  1. Install dependencies:
npm install

Running the Application

Start the API Server:

node server.js

Test the endpoint at http://localhost:9000

Test Data Structures:

node string.js
node hash.js
node list.js

📦 Tech Stack

  • Runtime: Node.js
  • Framework: Express.js (v5)
  • Redis Client: ioredis
  • HTTP Client: Axios

👨‍💻 Author

Dharam Dan


If this project helped you understand Redis better, feel free to give it a ⭐!

About

Built this to learn Redis caching the right way. Shows how to cut API response times from 500ms to under 10ms using cache-aside patterns. Includes examples with strings, hashes, and lists.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages