Bird
Raised Fist0
NestJSframework~20 mins

Redis transport in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Transport Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when a NestJS microservice using Redis transport receives a message?

Consider a NestJS microservice configured with Redis transport that listens for a message pattern 'sum'. The handler adds two numbers from the data and returns the result.

What will be the response when the client sends {a: 5, b: 7}?

NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class MathController {
  @MessagePattern('sum')
  sum(data: {a: number; b: number}): number {
    return data.a + data.b;
  }
}
ANaN
B12
Cundefined
D57
Attempts:
2 left
💡 Hint

Think about what the sum function returns when given two numbers.

📝 Syntax
intermediate
2:00remaining
Which option correctly configures a NestJS microservice to use Redis transport?

Choose the correct way to create a NestJS microservice using Redis transport on port 6379.

NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Transport } from '@nestjs/microservices';

async function bootstrap() {
  const app = await NestFactory.createMicroservice(AppModule, {
    transport: Transport.REDIS,
    options: {
      host: 'localhost',
      port: 6379,
    },
  });
  await app.listen();
}
bootstrap();
Atransport: Transport.REDIS, options: { host: 'localhost', port: 6379 }
Btransport: 'redis', options: { url: 'redis://localhost:6379' }
Ctransport: Transport.REDIS, options: { url: 'redis://localhost:6379' }
Dtransport: Transport.TCP, options: { host: 'localhost', port: 6379 }
Attempts:
2 left
💡 Hint

NestJS Redis transport supports host and port in options.

🔧 Debug
advanced
2:00remaining
Why does this NestJS Redis microservice fail to connect?

Given the following microservice bootstrap code, why does the connection to Redis fail?

NestJS
const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.REDIS,
  options: {
    host: '127.0.0.1',
    port: 6380,
  },
});
await app.listen();
ATransport.REDIS does not accept 'host' and 'port' options, only 'url'.
BThe host '127.0.0.1' is invalid for Redis connections.
CRedis server is not running on port 6380, causing connection failure.
DMissing await before app.listen() causes the failure.
Attempts:
2 left
💡 Hint

Check if Redis is running on the specified port.

❓ state_output
advanced
2:00remaining
What is the value of 'response' after sending a message to a Redis microservice?

In a NestJS client, a message with pattern 'multiply' is sent with data {x: 3, y: 4}. The microservice multiplies these numbers and returns the result.

What is the value of 'response' after awaiting the send call?

NestJS
const client = app.connectMicroservice({
  transport: Transport.REDIS,
  options: { url: 'redis://localhost:6379' },
});

await client.connect();
const response = await client.send('multiply', { x: 3, y: 4 }).toPromise();
A12
B7
C{x: 3, y: 4}
Dundefined
Attempts:
2 left
💡 Hint

Think about what the microservice returns for the 'multiply' pattern.

🧠 Conceptual
expert
2:00remaining
Which statement about Redis transport in NestJS microservices is TRUE?

Choose the correct statement about using Redis transport in NestJS microservices.

ARedis transport does not support pattern-based message routing.
BRedis transport requires manual handling of message serialization and deserialization by the developer.
CRedis transport can only be used for event-based fire-and-forget messaging, not request-response.
DRedis transport supports request-response messaging patterns with automatic message serialization.
Attempts:
2 left
💡 Hint

Consider how NestJS microservices handle messaging with Redis transport.

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

  1. Step 1: Understand Redis transport role

    Redis transport in NestJS is designed to let microservices send and receive messages through Redis as a broker.
  2. 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.
  3. Final Answer:

    To enable communication between microservices using Redis as a message broker -> Option D
  4. 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' } })
C. app.connectMicroservice({ transport: Transport.REDIS, options: { host: 'localhost', port: 8080 } })
D. app.connectMicroservice({ transport: Transport.TCP, options: { url: 'redis://localhost:6379' } })

Solution

  1. Step 1: Identify correct transport type

    Transport.REDIS is the correct transport for Redis messaging in NestJS.
  2. Step 2: Check Redis connection options

    The Redis connection URL should be provided as url: 'redis://localhost:6379' inside options.
  3. Final Answer:

    app.connectMicroservice({ transport: Transport.REDIS, options: { url: 'redis://localhost:6379' } }) -> Option A
  4. Quick Check:

    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

  1. Step 1: Understand @MessagePattern handler

    The method listens for messages with pattern 'sum' and receives data object with properties a and b.
  2. Step 2: Calculate the sum

    The method returns the sum of data.a and data.b, which is 5 + 3 = 8.
  3. Final Answer:

    8 -> Option C
  4. 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:
app.connectMicroservice({
  transport: Transport.REDIS,
  options: {
    host: 'localhost',
    port: 6379
  }
});

app.listen();
medium
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

  1. Step 1: Check Redis transport options format

    Redis transport expects a single connection string URL, not separate host and port keys.
  2. Step 2: Confirm correct usage

    The correct option is to provide url: 'redis://localhost:6379' inside options.
  3. Final Answer:

    Incorrect Redis options; should use 'url' instead of 'host' and 'port' -> Option A
  4. 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?
hard
A. const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { host: 'localhost', port: 6379 } }); @Controller() export class MathController { @MessagePattern('multiply') multiply(data: { x: number; y: number }) { return data.x + data.y; } } await app.listen();
B. 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();
C. const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.REDIS, options: { host: 'localhost', port: 6379 } }); @Controller() export class MathController { @MessagePattern('multiply') multiply(data: { x: number; y: number }) { return data.x * data.y; } } await app.listen();
D. 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();

Solution

  1. 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.
  2. Step 2: Check message handler logic

    The multiply method correctly returns the product of x and y, matching the 'multiply' pattern.
  3. Step 3: Confirm app lifecycle

    App is created as microservice and listens properly with await app.listen().
  4. 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 B
  5. Quick 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]
Hint: Use Transport.REDIS with URL and multiply numbers in handler [OK]
Common Mistakes:
  • Using Transport.TCP instead of REDIS
  • Providing host and port instead of URL for Redis
  • Incorrect arithmetic operation in handler
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard