Last week I found myself struggling to find some documentation about dead letter exchanges with Nest.js & RabbitMQ. Some GitHub issues recommend external packages, but you can actually do it with plain Nest.js!

We’ll require some basic knowledge of both RabbitMQ and Nest.js.

The complete source code is available here. If you wanna skip to it right ahead.

Image for post

Photo by Joanna Kosinska on Unsplash

So what is this dead lettering you’re talking about?

RabbitMQ dead letters messages to which at least one of the following points apply:

  • The message was rejected or not acknowledged with requeue: false.
  • The TTL (time to live) of the message expired.
  • The message was dropped because it exceeded the max length of the queue.

The dead lettered messages are then forwarded to a dead letter exchange if one was specified. In our example, we’ll simply forward them to another RabbitMQ queue and process them there. This pattern is great to implement error-resilient workers!

What will we build?

Glad that you ask! We’ll build two microservices that will simulate the preparation of a burger. Sadly our chef drops every third burger patty. We’ll introduce some error handling to cover that.

Image for post

Sequence Diagram of our Burger Ordering

As we can see we have 4 components involved in the making of a burger:

  1. Burger Queue: Receives all messages around the making of a burger and communicates with the customer.
  2. Burger Worker: Takes the messages from the Burger Queue and processes them.
  3. Recovery Queue: Receives all dead letters.
  4. Recovery Worker: Takes the messages from the Recovery Queue and redirects them to the Burger Queue. Also increases retry count by 1. Emits failure if retry count exceeds 4.

#javascript #programming #nestjs #typescript #node

Dead Lettering in Nest.js & RabbitMQ — Is it even possible?
9.20 GEEK