Backend · Distributed systems
Microservices with NestJS
Two NestJS services that talk through events on RabbitMQ, with correlated logs, metrics and end-to-end distributed tracing.
- Trace context propagated through the AMQP message
- Custom publish and consume metrics
- correlationId in both services’ logs
- Type
- Study project
- Period
- Jul 2026
- Stack
- NestJS, RabbitMQ, Prisma, PostgreSQL

Architecture overview
01Input
POST /users — Input
optional x-correlation-id header
02ms-users
Controller → Service → Repository — Deterministic code
validated DTOs; publishing with emit
PostgreSQL — Data
Prisma 7
03Messaging
RabbitMQ — Service or infrastructure
user.created queue; payload with correlationId
04ms-email
Consumer — Deterministic code
@nestjs/microservices, RMQ transport
Mailer — Service or infrastructure
Nodemailer → SMTP (Mailtrap)
05Observability
Pino — Service or infrastructure
correlationId, trace_id and span_id on every log line
Prometheus + Grafana — Service or infrastructure
/metrics from the services and RabbitMQ
Jaeger — Service or infrastructure
OTLP; one trace from the request to the email
- Input and output
- Deterministic code
- Data
- Service or infrastructure
01Context and problem
Synchronous calls between services couple their availability: if the email service goes down, sign-up fails with it. Messaging removes that coupling, but the flow becomes asynchronous and hard to follow without observability.
02Goal
Study asynchronous communication between services with full observability — being able to follow a single request, from HTTP to the email being sent, across logs, metrics and traces.
03How it works
- 1.
Sign-up
ms-usersreceivesPOST /users, persists the user in PostgreSQL through Prisma and publishesuser.createdwithemit, without waiting for a reply. - 2.
Correlation
The
correlationIdcomes from thex-correlation-idheader or is generated on the spot, travels in the event payload and shows up in both services’ logs. - 3.
Consumption
ms-emailconsumes the queue with NestJS’s RMQ transport and sends the welcome email. If it is down, the event waits in the queue until it reconnects. - 4.
Distributed trace
OpenTelemetry is initialized before any module, and the amqplib instrumentation carries the trace context in the message. In Jaeger, a single trace shows the request, the query, the publish and the send.
System screens


04Technical architecture
Two independent NestJS services, each with its own package.json. The infrastructure — RabbitMQ, Prometheus, Grafana and Jaeger — runs with Docker Compose.
ms-users
User CRUD organized into controller, service and repository, with validated DTOs and a global PrismaService. It publishes events through the RabbitMQ module.
ms-email
No business HTTP routes: it consumes the queue and sends emails. It exposes a port only so Prometheus can scrape
/metrics.Logs
Pino in both services, with
correlationId,trace_idandspan_idon every line. Authentication headers, cookies andpasswordfields are redacted automatically.Metrics
On top of the default Node metrics,
events_published_total,events_consumed_totaland theevent_processing_duration_secondshistogram. In Grafana, publish versus consume shows when the consumer falls behind or goes down.
05Technologies
- Services
- NestJS 11 · TypeScript · @nestjs/microservices
- Data and messaging
- PostgreSQL · Prisma 7 · RabbitMQ
- Observability
- OpenTelemetry · Jaeger · Prometheus · Grafana · Pino
- Nodemailer · Mailtrap
06Engineering decisions
An event instead of a direct call
Sign-up doesn’t depend on the email service being up. Publishing with
emitdecouples the two flows.Explicit correlation on top of the trace
Traces aren’t always at hand when investigating a problem from the logs. The
correlationIdin the payload makes it possible to cross-reference both services’ logs even without Jaeger.Tracing initialization order
tracing.tsis the first import inmain.tsin both services, because the instrumentations must load before the modules they patch.
07Technical challenges
Context lost in the queue
Without propagation, the trace ends at the publish and consumption becomes a separate trace. The amqplib instrumentation carries the context in the message and joins both sides.
Metrics in a service with no HTTP
The consumer has no routes, but it still needs to be observed. A dedicated port exposes only
/metrics.Delivery guarantees
With NestJS’s default
noAck, the message is acknowledged on delivery; if sending fails, the event is lost. The limitation is documented and the way to fix it is mapped out.
08What was implemented
- User CRUD with event publishing
- Event consumption and email sending
- Structured logs with correlation and redaction of sensitive data
- Custom metrics and a Grafana dashboard
- Distributed tracing with OpenTelemetry and Jaeger
- Local infrastructure with Docker Compose
09Possible next steps
Future improvements — not implemented yet
- Turn on
noAck: falseand set up a dead letter queue to reprocess failed sends. - Publish to a named exchange instead of directly to the queue.
- Version the Grafana dashboard alongside the code.
- Containerize both services, which currently run outside Docker.
10Code and demo
- Source code on GitHub (opens in a new tab)
No public demo. The repository README explains how to run the project locally.
