Redis transport in NestJS is a way to enable microservices communication using Redis as a message broker. It allows different parts of an app to send and receive messages through Redis channels.
Click to reveal answer
beginner
How do you create a Redis microservice client in NestJS?
You create a Redis microservice client by using the ClientProxyFactory.create() method with Transport.REDIS and providing Redis connection options like host and port.
Click to reveal answer
beginner
Which NestJS decorator is used to listen for messages in a Redis microservice?
The @MessagePattern() decorator is used to listen for specific messages or events sent over Redis transport.
Click to reveal answer
intermediate
What is the role of the 'transport' option in NestJS microservice configuration?
The 'transport' option tells NestJS which communication method to use, like Redis, TCP, or MQTT. For Redis transport, you set transport to Transport.REDIS.
Click to reveal answer
intermediate
Why is Redis transport useful in microservices architecture?
Redis transport helps microservices talk to each other quickly and reliably by using Redis as a fast message broker. It supports pub/sub messaging patterns and scales well.
Click to reveal answer
Which NestJS transport option is used to enable Redis communication?
ATransport.REDIS
BTransport.TCP
CTransport.MQTT
DTransport.GRPC
✗ Incorrect
Transport.REDIS is the correct option to use Redis as the transport layer in NestJS microservices.
What decorator do you use to handle incoming Redis messages in a NestJS microservice?
A@EventPattern()
B@MessagePattern()
C@Subscribe()
D@Listen()
✗ Incorrect
@MessagePattern() listens for messages sent via Redis transport.
Which method creates a Redis client proxy in NestJS?
ANestFactory.create()
BRedisClient.connect()
CClientProxyFactory.create()
DTransport.createClient()
✗ Incorrect
ClientProxyFactory.create() is used to create client proxies for different transports including Redis.
What is a key benefit of using Redis transport in microservices?
AIt replaces databases.
BIt provides synchronous HTTP calls.
CIt stores large files.
DIt enables fast pub/sub messaging.
✗ Incorrect
Redis transport enables fast publish/subscribe messaging between microservices.
Which configuration option is NOT required when setting up Redis transport in NestJS?
Ausername
Bport
Cdatabase
Dhost
✗ Incorrect
Username is usually not required for Redis connection unless Redis is configured with authentication.
Explain how Redis transport works in NestJS microservices and why it is useful.
Think about how different parts of an app send messages through Redis channels.
You got /4 concepts.
Describe the steps to set up a Redis microservice client and server in NestJS.
Consider both client creation and message handling on the server side.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of using Redis transport in a NestJS microservice?
easy
A. To cache HTTP responses automatically
B. To store application data persistently in Redis
C. To replace the HTTP server with Redis server
D. To enable communication between microservices using Redis as a message broker
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 D
Quick Check:
Redis transport = message broker communication [OK]
Hint: Redis transport is for messaging, not data storage or caching [OK]
Common Mistakes:
Confusing Redis transport with Redis database usage
Thinking Redis transport replaces HTTP server
Assuming Redis transport caches HTTP responses
2. Which of the following is the correct way to configure a NestJS microservice to use Redis transport?
easy
A. app.connectMicroservice({ transport: Transport.REDIS, options: { url: 'redis://localhost:6379' } })
B. app.connectMicroservice({ transport: Transport.HTTP, options: { url: 'redis://localhost:6379' } })
Transport.REDIS with Redis URL = correct config [OK]
Hint: Use Transport.REDIS and provide Redis URL in options [OK]
Common Mistakes:
Using wrong transport type like HTTP or TCP
Providing Redis host and port instead of URL
Using incorrect port number for Redis
3. Given the following NestJS microservice code using Redis transport, what will be the output when a message with pattern 'sum' and data { a: 5, b: 3 } is sent?
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;
}
}
medium
A. { a: 5, b: 3 }
B. 53
C. 8
D. undefined
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 C
Quick Check:
5 + 3 = 8 [OK]
Hint: Add the numbers from data object in the handler [OK]
Common Mistakes:
Concatenating numbers as strings instead of adding
Returning the whole data object instead of sum
Not matching the message pattern correctly
4. Identify the error in this NestJS Redis microservice setup:
A. Incorrect Redis options; should use 'url' instead of 'host' and 'port'
B. Missing await before app.listen()
C. Transport.REDIS is invalid; should use Transport.TCP
D. app.connectMicroservice should be called after 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 provide url: 'redis://localhost:6379' inside options.
Final Answer:
Incorrect Redis options; should use 'url' instead of 'host' and 'port' -> Option A
Quick Check:
Redis options require URL string [OK]
Hint: Use Redis URL string in options, not separate host/port [OK]
Common Mistakes:
Using separate host and port keys for Redis options
Calling app.listen() before connecting microservice
Confusing Transport.REDIS with Transport.TCP
5. You want to create a NestJS microservice using Redis transport that listens for a message pattern 'multiply' and returns the product of two numbers. Which code snippet correctly implements this with proper Redis connection and message handling?