Redis (Remote Dictionary Server) is an open-source, in-memory data structure store used as a database, cache, and message broker. It achieves sub-millisecond response times through in-memory storage and supports strings, hashes, lists, sets, sorted sets, streams, and geospatial indexes.
Role in NestJS
Redis serves two primary roles in NestJS applications:
- Caching: Frequently accessed data (database query results, API responses) is stored in Redis to reduce latency and database load. Both the CacheModule/CacheInterceptor approach and direct ioredis integration are supported.
- Message broker: Through the Redis Transporter, NestJS microservices can communicate via Redis pub/sub channels.
Connection and configuration
- Default host:
localhost, default port:6379 - Connections can be managed via
OnModuleInit/OnModuleDestroylifecycle hooks, or through NestJS’sCacheModule.register(). - Supports connection pooling, clustering, and sharding for high availability.
TTL and expiry
Redis supports per-key expiry via the EX option (in seconds). Common TTL values in NestJS caching patterns range from 10 minutes (short-lived tokens) to 30 minutes (product data) to 1 day (stable reference data).