Monday, August 6, 2018

MassTransit, a real alternative to NServiceBus?

Understand how MassTransit could be a real alternative when building distributed systems on the .NET Platform.
Photo by Markus Spiske on Unsplash

Looking for an free and open-source alternative to NServiceBus? Maybe MassTransit could be what you are looking for. Let's understand the platform and how it could be used on your next project.

What is MassTransit?

MassTransit  is a lightweight service bus for building distributed .NET applications. The main goal is to provide a consistent, .NET friendly abstraction over the message transport (RabbitMQ, Azure Service Bus, etc.). MassTransit is not a new project. They've been around since 2007 and were created as an alternative to NServiceBus. In early 2014, the framework was rewritten to support asynchronous programming as well as leveraged the power of messaging platform. The code was also rewritten, resulting in an entirely new, completely asynchronous, and highly optimized framework for message processing.

Why MassTransit

Like NServiceBus, MassTransit helps decoupling your backend from your frontend (and in general, decoupling services), leveraging enterprise design patterns like CQRS and Eventual Consistency. Some of the features you will find in MassTransit are:
  • providing support for messages, sagas
  • supporting different transports
  • allows automated or custom retries on failures
  • asynchronous requests/responses
  • poison message handling
  • exception management
  • custom serialization
  • message correlation
  • routing
  • scheduling

Customizations

MassTransit is also extremely customizable and as mentioned previously can run on different transports (RabbitMQ, Azure Service Bus, etc) providing enormous benefits as both are strong and stable platforms with different characteristics. It also supports the new .NET Standard on .NET Core and runs on multi-platforms.

Sample Code

With that introduction, let's review some code. If you come from a NServiceBus background, you'll probably looking for three things:
  1. How to initialize the bus
  2. How to initialize the host (your backend, where your handlers run)
  3. How to initialize a client (where you send messages from).
Please take a look at examples below to understand how it all works.

Initializing the Bus

The below code shows how to initialize a simple MassTransit Bus:

Initializing a Server (Host)

The below code shows how to initialize a simple MassTransit server:

Initializing a Client

The below code shows how to initialize a simple MassTransit client that talks to the above service:

Final Thoughts

This ends our quick introduction about MassTransit. I've been using NServiceBus for quite some time and while I recognize the value it adds, my frustrations with it grew significantly over time. For that reason I started looking for alternatives and found MassTransit a couple of years ago. I've been using MassTransit since them and I can confidently say that MassTransit is a solid alternative to NServiceBus not only due to its solid codebase but also for its community and growing ecosystem.

Want to learn more about MassTransit? Please consider reading the following posts:
In case you're interested, I recently pushed an updated MassTransit/Docker/.NET Core 3.1 implementation to GitHub at: github.com/hd9/masstransit-rabbitmq

References

See Also

For more posts about MassTransit on this blog, please click here.
Any comment or suggestion about this post? Please DM me on Twitter at @BrunoHilden