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
  • support for modern technologies like Azure Service BusApache KafkaAzure Event Hub and Amazon SQS

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.

Cloud Support

todo

Transports

MassTransit includes full support for several transports, most of which are traditional message brokers. RabbitMQ, ActiveMQ, Azure Service Bus, Apache Kafka, Azure Event Hub and Amazon SQS are supported.

Sample Code

With that introduction, let's review some code. If worked with NSB before, you're 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:

Other aspects to consider

Before we end, I'd like to bring to your attention other things you should evaluate before committing on such an important framework to your organization. I'd recommend that you research about:
  • Support: NServiceBus, being backed by a commercial organization deserves a point on this item. Having a commercial organization backing the product may potentially be a more compelling argument to your employer. NSB offers multiple support options including consulting and on-site training.
  • Online Documentation: both frameworks have good documentation but NSB is definitely ahead on this criteria. You will find way more documentation about NSB.
  • Community: both MT and NSB have decent online communities, with the latter being bigger and more active.
  • Access to current technologies: MT and NSB will definitely provide you access to modern technologies like cloud and serverless but NSB seems to be ahead on that regard.
  • Active development: both NSB and MT have very active developments.
  • Open-Source: I preferably like to have access to the source code of the product I'm using, in case there are issues
  • Ecosystem: Particular, NSB's parent company, offers lots of other products that are worth considering. These products integrate well with NServiceBus and depending on your use of the platform may 

Final Thoughts

This ends our quick introduction about MassTransit. I've been using NServiceBus for quite some time and while I recognize its value, I wanted to explore viable (and cheaper) alternatives for my open-source projects. I've been using MT for a couple of years I can confirm that it's a solid alternative to NServiceBus. MassTransit today is also a bigger and more solid project than it was 5 years ago and definitely deserves your attention.

However, choosing a critical framework like this is not only about code. I recommend you to also evaluate other criteria such as support, documentation, training and ecosystem. On pretty much every criteria (except for price) NServiceBus is ahead. And maybe those things will count for your organization.

More about MassTransit

Want to learn more about MassTransit? Please consider reading these articles:

Source Code

As always, the source code is available on my GitHub.

References

See Also

    About the Author

    Bruno Hildenbrand      
    Principal Architect, HildenCo Solutions.