This repository demonstrates a simple Node.js application that integrates both Redis and MongoDB. Redis is used as an in-memory cache and temporary storage layer, while MongoDB is available for persistent data storage. The sample includes OTP management, banner storage, and connectivity checks.
- Name: Swapnil Menkar
- Mobile: 8149005578
- LinkedIn: https://www.linkedin.com/in/swapnil-menkar-7051852b/
- Express-based REST API
- Redis integration using
ioredis - OTP generation with time-based expiration (TTL)
- Banner storage with Redis key/value operations
- MongoDB connectivity test endpoint
- Docker Compose setup for Redis and MongoDB containers
Redis is used for high-performance, in-memory key-value operations. The project demonstrates real Redis usage in the following ways:
otp_with_ttl.jsstores OTP codes in Redis with an expiration time using theEXoption.- OTP entries are stored under keys like
otp:<phone>and automatically expire after 30 seconds. - Redis TTL is queried with
redis.ttl()to show remaining lifetime of an OTP. site_banner.jsstores and retrieves application banner text using Redis keys.src/index.jsincludes a/redisendpoint to verify Redis connectivity viaPING.
SET key value EX seconds— store value with TTLGET key— retrieve stored valueDEL key— delete a key after successful verificationTTL key— check remaining time-to-livePING— validate connection status
- Node.js 18+ installed
- Docker and Docker Compose installed
npm installdocker compose up -dThis starts:
- Redis on
localhost:6379 - MongoDB on
localhost:27017
Use one of the following entry points depending on the feature you want to test:
npm run dev
npm run dev:otp
npm run dev:bannerGET /redis- Returns
PINGresponse from Redis.
POST /otp— generate and store OTP for a phone numberPOST /otp/verify— verify OTP and delete it from RedisGET /otp/:phone/ttl— get remaining TTL for the OTP key
POST /banner— save banner text in RedisGET /banner— retrieve banner textDELETE /banner— delete banner text from RedisGET /banner/exists— check whether the banner key exists
GET /mongo— connect to MongoDB and report database status
The application can use these environment variables:
REDIS_URL— Redis connection stringMONGO_URL— MongoDB connection string
If not set, the app defaults to local containers:
redis://localhost:6379mongodb://localhost:27017/my_app_database
- Redis is primarily used for fast, temporary storage and TTL-based data expiration.
- MongoDB is included for persistent storage and connectivity verification.
- The Docker Compose file includes named volumes for Redis and MongoDB data.
Thanks for using this project, and feel free to connect on LinkedIn for any updates or improvements.