Redis transport helps different parts of a NestJS app talk to each other fast and reliably using Redis. It makes sending messages between services easy.
Redis transport in NestJS
import { ClientProxyFactory, Transport } from '@nestjs/microservices'; const client = ClientProxyFactory.create({ transport: Transport.REDIS, options: { url: 'redis://localhost:6379', }, });
The transport option must be set to Transport.REDIS to use Redis.
The options object includes Redis connection details like the URL.
const client = ClientProxyFactory.create({
transport: Transport.REDIS,
options: { url: 'redis://localhost:6379' },
});const client = ClientProxyFactory.create({
transport: Transport.REDIS,
options: {
url: 'redis://:password@redis-server:6379',
},
});const server = await app.connectMicroservice({ transport: Transport.REDIS, options: { url: 'redis://localhost:6379' }, }); await app.startAllMicroservices();
This example creates a NestJS microservice that listens for messages with the pattern 'greet' over Redis. When it receives a name, it replies with a greeting.
import { Controller } from '@nestjs/common'; import { MessagePattern, Payload } from '@nestjs/microservices'; import { NestFactory } from '@nestjs/core'; import { MicroserviceOptions, Transport } from '@nestjs/microservices'; @Controller() class AppController { @MessagePattern('greet') greet(@Payload() name: string): string { return `Hello, ${name}!`; } } async function bootstrap() { const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppController, { transport: Transport.REDIS, options: { url: 'redis://localhost:6379', }, }); await app.listen(); console.log('Microservice is listening using Redis transport'); } bootstrap();
Make sure Redis server is running before starting the microservice.
Use @MessagePattern to listen for specific message patterns.
Redis transport works well for scalable microservice communication.
Redis transport lets NestJS microservices communicate using Redis as a message broker.
Set transport: Transport.REDIS and provide Redis connection options.
Use @MessagePattern to handle messages sent over Redis.
Practice
Redis transport in a NestJS microservice?Solution
Step 1: Understand Redis transport role
Redis transport in NestJS is designed to let microservices send and receive messages through Redis as a broker.Step 2: Differentiate from data storage or caching
Redis transport is not for storing data or caching HTTP responses; it is for messaging between services.Final Answer:
To enable communication between microservices using Redis as a message broker -> Option DQuick Check:
Redis transport = message broker communication [OK]
- Confusing Redis transport with Redis database usage
- Thinking Redis transport replaces HTTP server
- Assuming Redis transport caches HTTP responses
Solution
Step 1: Identify correct transport type
Transport.REDIS is the correct transport for Redis messaging in NestJS.Step 2: Check Redis connection options
The Redis connection URL should be provided asurl: 'redis://localhost:6379'inside options.Final Answer:
app.connectMicroservice({ transport: Transport.REDIS, options: { url: 'redis://localhost:6379' } }) -> Option AQuick Check:
Transport.REDIS with Redis URL = correct config [OK]
- Using wrong transport type like HTTP or TCP
- Providing Redis host and port instead of URL
- Using incorrect port number for Redis
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
@Controller()
export class MathController {
@MessagePattern('sum')
sum(data: { a: number; b: number }) {
return data.a + data.b;
}
}Solution
Step 1: Understand @MessagePattern handler
The method listens for messages with pattern 'sum' and receives data object with properties a and b.Step 2: Calculate the sum
The method returns the sum of data.a and data.b, which is 5 + 3 = 8.Final Answer:
8 -> Option CQuick Check:
5 + 3 = 8 [OK]
- Concatenating numbers as strings instead of adding
- Returning the whole data object instead of sum
- Not matching the message pattern correctly
app.connectMicroservice({
transport: Transport.REDIS,
options: {
host: 'localhost',
port: 6379
}
});
app.listen();Solution
Step 1: Check Redis transport options format
Redis transport expects a single connection string URL, not separate host and port keys.Step 2: Confirm correct usage
The correct option is to provideurl: 'redis://localhost:6379'inside options.Final Answer:
Incorrect Redis options; should use 'url' instead of 'host' and 'port' -> Option AQuick Check:
Redis options require URL string [OK]
- Using separate host and port keys for Redis options
- Calling app.listen() before connecting microservice
- Confusing Transport.REDIS with Transport.TCP
Solution
Step 1: Verify Redis transport and connection options
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.REDIS, options: { url: 'redis://localhost:6379' } }); @Controller() export class MathController { @MessagePattern('multiply') multiply(data: { x: number; y: number }) { return data.x * data.y; } } await app.listen(); correctly uses Transport.REDIS with a Redis URL string in options.Step 2: Check message handler logic
The multiply method correctly returns the product of x and y, matching the 'multiply' pattern.Step 3: Confirm app lifecycle
App is created as microservice and listens properly with await app.listen().Final Answer:
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.REDIS, options: { url: 'redis://localhost:6379' } }); @Controller() export class MathController { @MessagePattern('multiply') multiply(data: { x: number; y: number }) { return data.x * data.y; } } await app.listen(); -> Option BQuick Check:
Transport.REDIS + correct URL + multiply logic = const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.REDIS, options: { url: 'redis://localhost:6379' } }); @Controller() export class MathController { @MessagePattern('multiply') multiply(data: { x: number; y: number }) { return data.x * data.y; } } await app.listen(); [OK]
- Using Transport.TCP instead of REDIS
- Providing host and port instead of URL for Redis
- Incorrect arithmetic operation in handler
Ready to prove it?
Pass the test and this pattern moves unknown
