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.
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.
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.
- 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.
client.js: Centralized Redis client configuration usingioredis, ensuring a single connection instance is reused across the application to prevent memory leaks.
| Request Type | Source | Latency (Avg) |
|---|---|---|
| Initial Fetch | External API | ~350ms - 500ms |
| Cached Fetch | Redis Memory | 5ms - 12ms |
Optimization result: ~97% reduction in response time.
- Node.js (v16 or higher)
- Redis Server (Running on
localhost:6379)
- Clone the repository:
git clone https://github.com/dharamdan01/redis-nodejs-performance-lab.git
cd redis-nodejs-performance-lab- Install dependencies:
npm installStart the API Server:
node server.jsTest the endpoint at http://localhost:9000
Test Data Structures:
node string.js
node hash.js
node list.js- Runtime: Node.js
- Framework: Express.js (v5)
- Redis Client: ioredis
- HTTP Client: Axios
Dharam Dan
- GitHub: @dharamdan01
- LinkedIn: Dharam Dan
If this project helped you understand Redis better, feel free to give it a ⭐!