ioredis is a robust, full-featured Redis client for Node.js. It is the recommended Redis client for NestJS, used internally by both the @nestjs/microservices Redis transporter and by developers building custom Redis services.
Role in NestJS
- Microservices transport: The Redis transporter in
@nestjs/microservicesuses ioredis under the hood. Callingunwrap()on a Redis client proxy returns a tuple of two ioredis instances:[pubClient, subClient]. - Custom Redis services: Developers commonly instantiate ioredis directly in custom
RedisServiceclasses, often via a FactoryProvider Pattern to create a singleton instance injected by token.
Key API used in NestJS
import { Redis } from 'ioredis';
const client = new Redis({ host: 'localhost', port: 6379 });
await client.set('key', 'value', 'EX', ttlInSeconds);
const value = await client.get('key');
await client.del('key');ioredis supports all Redis features including pub/sub, pipelines, transactions, Lua scripting, and cluster mode. Connection events ('error', 'connect', 'ready') should be handled for production resilience.