Rafael Andrade
  • PT
  • EN (current language)
All projects

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
Jaeger trace of a POST /users: 17 spans across 2 services, with the middlewares, UsersController, Prisma operations, the PostgreSQL query, the RabbitMQ publish and ms-email processing user.created.
One POST /users request in Jaeger: the database query, the queue publish and the consumption in ms-email show up in the same trace, with 17 spans in 262 ms. Captured with the services running locally.

Architecture overview

  1. 01Input

    • POST /users — Input

      optional x-correlation-id header

  2. 02ms-users

    • Controller → Service → Repository — Deterministic code

      validated DTOs; publishing with emit

    • PostgreSQL — Data

      Prisma 7

  3. 03Messaging

    • RabbitMQ — Service or infrastructure

      user.created queue; payload with correlationId

  4. 04ms-email

    • Consumer — Deterministic code

      @nestjs/microservices, RMQ transport

    • Mailer — Service or infrastructure

      Nodemailer → SMTP (Mailtrap)

  5. 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
ms-users publishes and doesn’t wait for a reply. Logs, metrics and traces make it possible to follow the same request across both services.

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. 1.

    Sign-up

    ms-users receives POST /users, persists the user in PostgreSQL through Prisma and publishes user.created with emit, without waiting for a reply.

  2. 2.

    Correlation

    The correlationId comes from the x-correlation-id header or is generated on the spot, travels in the event payload and shows up in both services’ logs.

  3. 3.

    Consumption

    ms-email consumes 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. 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

RabbitMQ dashboard showing the user.created queue: one connected consumer, an empty queue and a spike of publishes in the last-minute chart.
The user.created queue in the RabbitMQ dashboard after three sign-ups: one connected consumer and no message left waiting.
Inbox of a local SMTP server with three “Email de Boas-Vindas” (welcome email) messages; the open one reads “Seja bem-vindo, Carla Mendes”.
The welcome emails sent by ms-email, received by a local SMTP server instead of Mailtrap. The users are sample data and the email is in Portuguese.

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_id and span_id on every line. Authentication headers, cookies and password fields are redacted automatically.

  • Metrics

    On top of the default Node metrics, events_published_total, events_consumed_total and the event_processing_duration_seconds histogram. 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
Email
Nodemailer · Mailtrap

06Engineering decisions

  • An event instead of a direct call

    Sign-up doesn’t depend on the email service being up. Publishing with emit decouples the two flows.

  • Explicit correlation on top of the trace

    Traces aren’t always at hand when investigating a problem from the logs. The correlationId in the payload makes it possible to cross-reference both services’ logs even without Jaeger.

  • Tracing initialization order

    tracing.ts is the first import in main.ts in 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: false and 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