Photo by Markus Spiske on Unsplash |
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 Bus, Apache Kafka, Azure 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
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
- How to initialize the bus
- How to initialize the host (your backend, where your handlers run)
- How to initialize a client (where you send messages from).
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
- 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
- Creating a MassTransit client/server application using RabbitMQ, .NET Core and Linux
- Exploring MassTransit InMemory Scheduled Messaging using RabbitMQ and .Net Core
- Async Request/Response with MassTransit, RabbitMQ, Docker and .NET core
- Running NServiceBus on Azure WebJobs
Source Code
As always, the source code is available on my GitHub.References
See Also
- My journey to 1 million articles read
- Adding Application Insights telemetry to your ASP.NET Core website
- Async Request/Response with MassTransit, RabbitMQ, Docker and .NET core
- Distributed caching in ASP.NET Core using Redis, MongoDB and Docker
- Running NServiceBus on Azure WebJobs
- Creating ASP.NET Core websites with Docker
- Send emails from ASP.NET Core websites using SendGrid and Azure
- Hosting NuGet packages on GitHub
- Configuration in .NET Core console applications
- Migrating NServiceBus backends to Azure WebJobs
- Custom Security on NserviceBus Endpoints
- Building and Running ASP.NET Core apps on Linux
- Package Management in .NET Core