skip to content

As the lead for a NestJS 12 order service that serves a REST API and consumes broker messages, would you run one hybrid app or a separate microservice, and what does each commit the team to?

level: principalimportance: nice to knowfreq 15%

answer

  1. one container, one event loop
  2. inheritAppConfig is opt-in
  3. listen() before startAllMicroservices()
  4. a standalone microservice has no HTTP server
  5. write down the split trigger

basics

~20 s

A hybrid app (NestFactory.create plus connectMicroservice) shares one process, container and release, so it is cheap but couples scaling, failure and configuration. A standalone createMicroservice app scales and fails on its own but adds a second deployable. Start hybrid while handlers are light; split on a written trigger.

solid answer

~50 s

Both shapes run the same handlers. A hybrid app is `NestFactory.create()` with listeners attached by `app.connectMicroservice()` and started by `startAllMicroservices()`: HTTP controllers and message handlers share singleton providers and one Node.js event loop, deploy together and scale together. Its traps are configuration and order: connected listeners do not inherit global pipes, guards, interceptors or filters unless `{ inheritAppConfig: true }` is passed after the `useGlobal*()` calls, and `listen()` or `init()` must come before `startAllMicroservices()`. A standalone `NestFactory.createMicroservice()` app has one transport, its own `useGlobal*()` enhancers and no HTTP server, so it scales with queue depth and fails alone, at the cost of a second image, probes and pipeline. Choose by handler cost, load shape, ownership and operational budget; a reasonable default is hybrid while handlers are light, with the handlers in their own module and a written trigger for splitting.

go deeper

for a junior

Know that a hybrid app combines an HTTP server and microservice listeners in one Nest application, and that createMicroservice() builds a standalone one.

for a middle

Explain connectMicroservice(), startAllMicroservices() and why inheritAppConfig is needed for globals to reach message handlers.

for a senior

Diagnose the hybrid traps: skipped validation on messages, enhancers registered after connecting, handlers consuming before lifecycle hooks finish.

for a principal

Choose hybrid or split from handler cost, load shape, ownership and operational budget, set a written split trigger, and make the bootstrap order a reviewed standard.

## The decision An order service often has two kinds of work: a public REST API, and handlers for messages and events from other services, such as `{ cmd: 'check_stock' }` requests and `'order_placed'` events. NestJS 12 lets you run both from **one hybrid application** or deploy the handlers as **a standalone microservice**. Neither is right in general. The choice fixes how the two workloads share a process, a configuration and a release, and that is what a lead has to own. ## What each shape is - **Hybrid**: `NestFactory.create(AppModule)` builds the HTTP application, `app.connectMicroservice()` attaches one listener per transport, and `app.startAllMicroservices()` starts them. `createMicroservice()` supports only one server, so several transports in one process always means a hybrid. Closing the app also closes the connected listeners. - **Standalone**: `NestFactory.createMicroservice(AppModule, { transport, options })` returns an `INestMicroservice` with one transport and no HTTP server, started with `listen()`. The REST API runs as a separate application, usually from the same repository and shared modules. ## What the hybrid commits you to 1. **One container and one event loop.** HTTP controllers and message handlers share singleton providers, connection pools and caches. That is cheap and consistent, but a CPU-heavy handler delays HTTP responses, and both workloads scale together: every replica you add for API traffic also adds a consumer, and the reverse. 2. **Opt-in configuration sharing.** Connected listeners do not inherit global pipes, guards, interceptors or filters, whether registered with `useGlobal*()` or as `APP_PIPE`, `APP_GUARD`, `APP_INTERCEPTOR` or `APP_FILTER` providers. You pass `{ inheritAppConfig: true }` to `connectMicroservice()`, call the `useGlobal*()` methods before it, and make every shared enhancer RPC-aware. Forgetting the flag fails silently: messages skip validation and auth. 3. **Bootstrap order is a contract.** Call `app.listen()`, or `app.init()` without HTTP, before `startAllMicroservices()`, or handlers can consume before `onModuleInit()` and `onApplicationBootstrap()` finish. 4. **One release and one failure domain.** A bad deploy or a crash takes down the API and the consumers together. ## What the split commits you to 1. **Two deployables.** Two images or entry points, two sets of probes and alerts, and a shared library or modules both import, which you must version together. 2. **Its own enhancers, with no inheritance trap.** An `INestMicroservice` has `useGlobalPipes()`, `useGlobalGuards()` and the rest, so its validation and auth are declared where they apply. 3. **No HTTP server.** An orchestrator's HTTP health probe needs another mechanism; adding an HTTP listener makes it a hybrid again. 4. **Independent scaling and failure.** Consumers scale with queue depth, the API with request rate, and a poison message cannot slow the API. ## Signs the choice has gone wrong Revisit the decision when the evidence says so, not on a schedule: - **Hybrid that should split:** API latency rises whenever the message backlog grows; consumer fixes wait for an API release, or the reverse; replicas are added for one workload and sit idle for the other; a poison message takes the API down with it. - **Split that should merge:** the two services always deploy together, every change touches the shared module and both entry points, and the consumer is idle most of the day while still paying for its own probes, alerts and pipeline. Either way, the cost of changing course is set by the module boundary, not by Nest: handlers that live in their own module move between the two shapes in an afternoon, while handlers spread through the API's controllers turn a deployment decision into a refactor. ## How to decide | Signal | Leans hybrid | Leans split | |---|---|---| | Handler cost | light lookups that reuse the API's providers | CPU-heavy or long-running work | | Load shape | messages track API traffic | bursts, backlogs, separate peaks | | Team and release | one team, one cadence | different owners or cadences | | Operational budget | one deployable is all you can run well | probes, alerts and pipelines for two | A reasonable default is to start hybrid while the handlers are light, and to write down the trigger for splitting: handler CPU, backlog growth or a release conflict. Keep the handlers in their own module from day one, so the split is a new `main.ts` rather than a refactor. If you stay hybrid, make `inheritAppConfig` and the `listen()`-then-`startAllMicroservices()` order a reviewed part of the bootstrap, and test a message through the real guard and pipe.

  • If you keep a NestJS 12 order service hybrid, what would you put in the bootstrap review checklist?
    That every `useGlobal*()` call comes before `connectMicroservice()`, that each listener passes `{ inheritAppConfig: true }` when the global pipes and guards must apply to messages, that every shared enhancer branches on `context.getType()` for `'rpc'`, and that `app.listen()` or `app.init()` runs before `startAllMicroservices()`. Back it with a test that sends a message through the real guard and pipe.
  • What makes a later split of a hybrid NestJS app cheap?
    Keeping the message controllers, and the providers only they use, in their own module that imports the shared domain modules. The split is then a second entry point that calls `NestFactory.createMicroservice()` with that module and declares its own global enhancers, plus removing the `connectMicroservice()` call from the HTTP app.
  • Why does adding HTTP replicas to a hybrid NestJS app change message consumption?
    Each replica is the same process with the same connected listeners, so every new instance also starts consuming. Consumer count then follows API traffic instead of queue depth, which is wrong when the two loads peak at different times.

  • A hybrid app's message handlers get the global ValidationPipe and guards automatically.
  • A standalone NestJS microservice cannot register global pipes or guards.
  • Splitting is always right because microservices scale better.
  • Message handlers in a hybrid app run on their own thread, so heavy work cannot slow HTTP.
  • Calling startAllMicroservices() before listen() is harmless.