gRPC & Kafka

Preview — 3 of 10 questions

With the gRPC transport, how is a service method on a controller bound to a proto-defined RPC?

javascript
import { GrpcMethod } from '@nestjs/microservices';

@Controller()
export class HeroesController {
  @GrpcMethod('HeroesService', 'FindOne')
  findOne(data: { id: number }) {
    return heroes.find((h) => h.id === data.id);
  }
}
AUse @MessagePattern('Method')
BUse @GrpcMethod('ServiceName', 'MethodName') matching the service and rpc names in the .proto
CUse @EventPattern('Method')
DgRPC requires no decorator — names auto-bind by convention only

How does a NestJS gRPC handler implement server streaming back to the client?

javascript
import { GrpcStreamMethod } from '@nestjs/microservices';
import { Observable, Subject } from 'rxjs';

@GrpcStreamMethod('ChatService', 'Converse')
converse(messages$: Observable<ChatMsg>): Observable<ChatMsg> {
  const out$ = new Subject<ChatMsg>();
  messages$.subscribe({
    next: (m) => out$.next({ ...m, echoed: true }),
    complete: () => out$.complete(),
  });
  return out$.asObservable();
}
AReturn a plain array
BReturn an Observable (e.g. an RxJS Subject) whose emissions are streamed to the client until it completes
CCall res.write() like Express
DStreaming is unsupported in NestJS gRPC

In the Kafka transport, what role does the consumer.groupId play across multiple instances of a service?

javascript
ClientsModule.register([{
  name: 'KAFKA',
  transport: Transport.KAFKA,
  options: {
    client: { brokers: ['localhost:9092'] },
    consumer: { groupId: 'orders-consumer' }, // scale instances under same group
  },
}]);
AIt is cosmetic and has no effect
BEvery instance in the group receives every message
CInstances sharing a groupId form a consumer group; partitions are distributed among them so each message is processed by exactly one instance in the group
DIt controls the producer's topic name

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.