NestJS Senior interview questions and answers

75 questions for a NestJS (Senior) interview - with correct answers, explanations and links to the docs.

Take this quiz
  1. 1.How does Nest know what to inject into constructor(private users: UsersService)?
  2. 2.Why can a bundler or compiler without decorator metadata support break a Nest app?
  3. 3.How is the DI container organized across modules?
  4. 4.A REQUEST-scoped provider is injected into a singleton service. What happens?
  5. 5.What are durable providers for?
  6. 6.How do you inject the current request into a provider, and what does it cost?
  7. 7.How can you discover all providers with a custom decorator at startup, e.g. to register event handlers automatically?
  8. 8.How does ConfigurableModuleBuilder help when writing a reusable dynamic module?
  9. 9.Why can forwardRef hide a design problem even when it fixes the error?
  10. 10.Why might an injected value be undefined inside a constructor but defined later, with circular dependencies?
  11. 11.Where does Nest's overhead usually matter, and where doesn't it?
  12. 12.Why can class-transformer serialization become a bottleneck for large responses?
  13. 13.How do you reduce cold-start time of a Nest app on serverless platforms?
  14. 14.Why is SWC often used in Nest development?
  15. 15.In Nest microservices over Kafka, what must handlers be prepared for?
  16. 16.Why can request-response (client.send) between microservices be risky at scale?
  17. 17.How do you get reliable event publishing from a Nest service that also writes to the database?
  18. 18.What does the gRPC transport in Nest rely on?
  19. 19.How do you propagate a trace or correlation ID across Nest microservices?
  20. 20.How would you structure a large Nest codebase to keep boundaries clear?
  21. 21.What does hexagonal (ports and adapters) architecture look like in Nest?
  22. 22.When does @nestjs/cqrs pay off?
  23. 23.How do you keep transactions consistent across several services in one request without passing a transaction object everywhere?
  24. 24.How would you implement multi-tenancy with a database per tenant in Nest?
  25. 25.What's a risk of putting authorization logic only in guards?
  26. 26.Why is it useful to have the same validation and authorization for HTTP, GraphQL and message handlers?
  27. 27.How should a Nest app shut down in Kubernetes to avoid dropped requests?
  28. 28.What should a readiness check in Nest verify, and what should liveness avoid?
  29. 29.How do you make outbound HTTP calls from Nest resilient?
  30. 30.What problem can a global @Cron cause in a scaled Nest deployment, and how do you solve it?
  31. 31.How should BullMQ workers in Nest be deployed under load?
  32. 32.Why should errors in event handlers (@OnEvent) be handled carefully?
  33. 33.How do you add distributed tracing to a Nest app?
  34. 34.How do you make Nest's logs structured and correlated?
  35. 35.How can you test a module that uses a request-scoped provider?
  36. 36.What's a good strategy for testing a Nest app's database layer in CI?
  37. 37.How do you test that guards, pipes and filters are wired correctly for a route?
  38. 38.How do you protect a Nest GraphQL API from very expensive queries?
  39. 39.Why must DataLoader instances be created per request in a Nest GraphQL app?
  40. 40.What does Apollo Federation (supported by Nest's GraphQL module) allow?
  41. 41.Which settings make a Nest API safer by default?
  42. 42.How do you prevent mass assignment when updating entities in Nest?
  43. 43.Why should secrets not be logged by Nest's default exception or request logging?
  44. 44.How do you safely handle file uploads at scale in Nest?
  45. 45.What's a sensible approach to upgrading a large Nest app to a new major version?
  46. 46.Why might a team choose Nest over a minimal framework like Fastify alone?
  47. 47.How can you share DTOs and validation between a Nest backend and a TypeScript front end?
  48. 48.What's a concern with exposing Swagger UI in production?
  49. 49.How can one interceptor implement idempotency keys for POST endpoints?
  50. 50.How would you implement response caching that varies by user in Nest?
  51. 51.How can you implement API versioning without duplicating whole controllers?
  52. 52.What's a pitfall of @Res() without passthrough in interceptors and filters?
  53. 53.How do you stream a large file download in Nest without loading it into memory?
  54. 54.What's the difference between onModuleInit and onApplicationBootstrap?
  55. 55.Why do lifecycle hooks not run for request-scoped or transient providers in the usual way?
  56. 56.What happens to global enhancers in a hybrid app's microservice part by default?
  57. 57.How do you write a guard that works for HTTP and GraphQL?
  58. 58.What's the effect of throwing inside an interceptor's map operator?
  59. 59.Why should long CPU-bound work not run in a Nest request handler?
  60. 60.How can you enforce module boundaries automatically in a Nest monorepo?
  61. 61.Why is it a good idea to separate the Nest app bootstrap (main.ts) from app configuration (e.g. a configureApp(app) function)?
  62. 62.How can Nest's ValidationPipe errors be made more useful to API clients?
  63. 63.What does app.useGlobalInterceptors() NOT give you compared to APP_INTERCEPTOR?
  64. 64.How would you implement soft-delete filtering globally with TypeORM in Nest?
  65. 65.Why can @nestjs/event-emitter handlers run before the database transaction commits?
  66. 66.What does onApplicationShutdown(signal) receive and why is it useful?
  67. 67.How do you limit the impact of a slow downstream service on a Nest API's thread of execution?
  68. 68.Why should controller tests not be the only tests for authorization rules?
  69. 69.What does useMocker() on a testing module builder help with?
  70. 70.How can Nest consume messages from a broker it has no built-in transport for, like AWS SQS?
  71. 71.Why should domain services throw domain errors rather than HttpExceptions?
  72. 72.How do you rate limit per authenticated user instead of per IP with @nestjs/throttler?
  73. 73.How do you broadcast WebSocket messages across several Nest instances?
  74. 74.What is a Backend for Frontend (BFF) and how might Nest implement it?
  75. 75.Why may a global ClassSerializerInterceptor not hide fields when you return results from Prisma?