Why NestRS
The thesis, the architecture — a modular monolith, deployed per workload — who NestRS is for, and the six structural properties that follow.
The thesis
Section titled “The thesis”The Rust framework for modular, scalable backends — you write the business logic, it carries the rest.
Cross-cutting concerns — security, transactions, input conversion, lifecycle, discovery — are error-prone exactly because they are repetitive. Every place a codebase has to wire them by hand is a place they will eventually be forgotten, inconsistent, or wrong. NestRS treats this as a framework problem: those concerns must be transparent to the application code.
The leverage is procedural macros. A controller, a resolver, a processor is a struct with a decorator; the framework expands it into the boilerplate a contributor would otherwise have written by hand — and would, eventually, have written slightly differently in two different files.
A modular monolith, deployed per workload
Section titled “A modular monolith, deployed per workload”The tagline’s two adjectives are one architecture, and it has a name: you write one modular monolith, and you deploy it as one binary per workload — or as a single binary, from the same modules. Google built Service Weaver for the same goal and put it in one line: “the development velocity of a monolith, with the scalability, security, and fault-tolerance of microservices”. Its HotOS paper names the cause: microservices conflate how code is written with how it is deployed. NestRS is built on that separation.
- users
- orgs
- posts
- audio
- chat
- notifications
- audit
- authn
- authz
- sign_in
apps/<app>/module.rs imports the edges it serves- sign_in
- users
- orgs
- posts
- audio
- notifications
- audit
- authn
- authz
- users
- chat
- notifications
- authn
- users
- posts
- audio
- audio
- notifications
- users
- orgs
- posts
- audio
- chat
- notifications
- audit
- authn
- authz
- sign_in
nestrs run dev apiusers is one module, served by three binaries: over HTTP and GraphQL, a WebSocket, and MCP. In one binary, no two edges may share a path, and the boot refuses a pair by name: Publish's users controller and gateway both mount /users, so merging it takes one rename. Drawn after the overview figure in Google's Service Weaver announcement: the same three layers, with the queue and the database where it draws RPC.Write. Your domain — entities, services, policy — lives once, in
crates/features/, cut into modules whose boundaries the framework holds: a
module reaches only what it imports, or the
access graph
fails the boot. A call from one module to another is a Rust method call, with
no endpoint and no serialization.
Run. A workload is one operational job — serving the API, holding the
sockets, issuing tokens, draining the queue; the
twelve-factor app calls it a process type.
Each app under apps/ is a thin composition root importing the edges it
serves, and an edge it does not import is never mounted. A workload scales on
its own signal, and a crash stays in its own binary. One app importing every
edge is a plain monolith and a sound place to start: splitting it later edits
composition roots, never a module. Merging two apps back is the same edit, once
no two of their edges share a path — the boot refuses a duplicate and names
both.
Deploy. Every binary builds from one workspace into one image, and the
Publish chart runs each app from it with its own command.
Your machine runs the same binaries with nestrs run dev.
Between binaries, the queue and the database. Where Service Weaver’s
runtime turned a method call into an RPC, NestRS links a module into every
binary that uses it — users is served by the API, the WebSocket server and
the MCP assistant alike — and binaries coordinate through the data they share.
That removes the costs Service Weaver’s own motivation lists: no endpoint
between binaries, no API frozen between them, and a cross-binary change is one
commit the compiler checks whole. What crosses between them — a table, a queue
payload — is a type in the shared crate, compiled into both sides. An app may
still call another over HTTP with its own client, but the framework carries
none of that call, from its authentication to its retries, and never makes it
look like a method call.
The line is the database. Per workload is the shape NestRS proposes, and
one binary is the same modules in one process. Binaries that share a database
belong in one workspace: they release together from one commit, so every
contract between them is a type the compiler checks. Each still connects as
its own database user, through its own <PREFIX>_SEAORM__URL, with only the
rights it needs: nothing in the framework changes the schema at boot, so only
the migration job holds the owner’s. An app that needs a
database of its own is simpler as a project of its own — its own workspace,
repository and release. What it shares with the first goes through a library
crate both depend on, so the compiler still checks each project against the
contract, or through the queue. Each project pins its own version of that
library, and its semver says when the two have drifted; the framework versions
its own queue envelope
(wire-format mismatch), and
split deployment weighs one
database against several.
Service Weaver itself stopped development in December 2024; the separation it argued for outlives it.
Who it is for
Section titled “Who it is for”Three starting points, one framework. What differs is which half of the thesis above you came for.
- You write NestJS or Spring, and you want Rust’s memory and latency profile. The layer vocabulary carries over intact; what changes is that the guarantees are checked rather than remembered. Coming from NestJS maps every reflex.
- You write Rust, and a router is below the altitude of your application. axum and tower do their job; tenancy, policy, transactions and masking are still yours to keep correct across every query. Why not axum compares the two on that axis.
- You are starting a backend that has to hold its shape as the team grows. Modules, an access graph checked at boot, and one place per concern — the structure is the framework’s, not a convention your reviews defend.
A service with no cross-cutting concerns — a proxy, a single-tenant tool, an embedded component — is better served by a bare router, and that answer is part of the thesis rather than an exception to it.
Why now
Section titled “Why now”Two shifts make this trade-off worth reopening.
RAM decides your instance class. A Node service idling at 100 MB fixes the smallest box you can rent before it serves a request; a 5 MB binary packs an order of magnitude more services per node. Managed runtimes — Node among them — are genuinely productive, but they buy that productivity with a garbage collector and a runtime whose footprint is always resident, whether or not a request is in flight.
Native code is no longer the hard path. Much of the boilerplate that once made higher-level runtimes feel faster to ship — scaffolding, ceremony, repetitive wiring — is carried by the framework’s decorators today.
NestRS reopens the trade-off between the two: keep the declarative,
decorator-driven style that makes the managed-runtime model productive, but stand
it on a native, compiled foundation that doesn’t bill you for it in RAM. One
cargo step compiles and type-checks, modules wire up regardless of import
order, and the result ships as one lean binary.
How it delivers
Section titled “How it delivers”Six consequences of the thesis — not a feature list. They compose: drop any one and the others weaken. Each section below states the guarantee; the mechanics live in Fundamentals, Security, and Database.
1. Declarative wiring
Section titled “1. Declarative wiring”Modules, providers, controllers, resolvers, gateways, processors — each is a struct decorated with an attribute macro. The decorator carries the entire integration contract. There is no service locator to call, no registration list to keep in sync, no central manifest a contributor has to remember to edit.
#[controller(path = "/users")]pub struct UsersController { #[inject] svc: Arc<UsersService>,}2. Secure by default
Section titled “2. Secure by default”Once the security modules are imported, every read through the
data layer is filtered, every mutating write is gated, and every operation declaring
#[authorize] on an entity answers rows judged for its caller. A route declaring none reads no
row, and one no guard identifying a caller covers refuses the boot unless it says
#[public].
Security is structural, not vigilant: forgetting a check is a category error the framework prevents, not a bug a code reviewer has to catch.
3. Transactional by default
Section titled “3. Transactional by default”A mutating HTTP request installs a transactional executor
before the handler runs. The service reaches the database only through
Repo, which picks up that executor
automatically — commit on 2xx/3xx, rollback otherwise. A GraphQL mutation
is one transaction whose root fields each roll back on their own answer, and
worker contexts install a per-attempt transaction on the same code path.
// Ambient executor — no per-method ceremony.impl UsersService { pub async fn rename(&self, id: Uuid, name: String) -> Result<User, ServiceError> { let user = Repo::<Users>::find_by_id(id) .await? .ok_or_else(|| ServiceError::not_found("user"))?; let mut active = user.into_active_model(); active.name = Set(name); Ok(User::from(&Repo::<Users>::update(active).await?)) }}4. Multi-transport, one shape
Section titled “4. Multi-transport, one shape”A feature is a port (entity, service, contract) plus one adapter per
transport it exposes. The port sits at the feature root; each adapter lives in
its own sub-folder with its own module.rs. An app picks the edges it serves —
a worker imports the queue adapter; an API imports HTTP, GraphQL, and WS.
Directoryfeatures/users/
- module.rs, service.rs, entity.rs, dto.rs, error.rs
Directoryhttp/
- …
Directorygraphql/
- …
Directoryws/
- …
Directoryqueue/
- …
Directorymcp/
- …
See Modules — A feature of its own for the composition rule and the reference layout.
5. Wiring verified at boot
Section titled “5. Wiring verified at boot”The dependency-injection graph is not resolved by reflection. Every module
records its imports and its providers’ dependencies at compile time; at startup,
App::builder().build() walks the graph and fails with a clear error if a provider
injects something its module cannot reach.
A misconfigured import is a startup error naming the missing dependency, not
a Cannot resolve at first request. See
Providers — The access graph.
6. Lean per-binary
Section titled “6. Lean per-binary”Capabilities ship as separate crates and integrate via discovery gated on module reachability. A binary that imports only the queue adapter does not mount HTTP routes or GraphQL resolvers — even when those files live in the same shared feature crate. One codebase, one binary per workload.
On the benchmarks page, the same service — idiomatic on both
sides, byte-identical contract — against NestJS 12.4 on node:http, its fastest
adapter: ×1.3 the throughput on a bare hello world, ×2 once a guard, an
interceptor or a request body is in the path, a p99 two to thirteen times lower,
in ~17× less RAM, cold-starting ~20× faster.
Six guarantees are worth what checks them. 7,000+ tests cover the framework workspace, and the end-to-end suites run against live Postgres, Redis and S3 rather than mocks — wiring bugs are the ones unit tests never see.
What this is not
Section titled “What this is not”- Not an HTTP layer. NestRS sits on top of
hyper/tokio— it gives them structure, it does not replace them. - Not a thin convenience layer. The framework’s value is in the cross-cutting guarantees (authz, transactions, masking, boot-time wiring). A controller-only decorator on top of an existing stack would not deliver them.
- Not a runtime DI container. The container exists, but the contract it
enforces is static: types and module imports, checked at boot. There is no
resolve<T>()you are expected to call from user code.
Going further
Section titled “Going further”- Getting started — install, run an app, build your first feature.
- The Publish workspace — the fictional product every example is drawn from.
- Tutorial — a complete feature, end to end, with every layer wired.
- Fundamentals — modules, providers, guards, pipes, interceptors, filters.