Introducing MemBus: In-Memory Publish/Subscribe Messaging
What is a Bus?
A bus carries people from A to B. That much is easy. You might have guessed that I am not in the transport business, nonetheless a bus in the software world also carries payloads around. You put something into the bus, expecting it to deliver your stuff to its designated destination. How it does that isn’t necessarily your business. Differing from the real business of public transportation you may not even care where your payload arrives.
The notion of putting your payload into the bus is called publishing.
You can attach your bus stop to the bus line. Normally you can specify which “passengers” you are interested in, and only those will come along to visit you. Other times you may interested in all “passengers” coming your way. Again, you do not necessarily care where your passengers come from, but you really wish they come in one piece and don’t get lost along the way.
The notion of attaching to the bus line to expect passengers is called subscribing.
A final word about the passengers. They are usually called messages, this time stealing the word from the world of mail delivery.
There are plenty of buses around in .NET world, let’s name a few: NServiceBus, MassTransit and .NET Service Bus. They all cover different grounds – what can be said, though, is that they all help setting up applications that scale well in terms of performance and throughput. This is because the publisher and the subscriber aren’t connected much more than via the payload that moves along the bus line. In other words, there aren’t much guarantees given in terms of when exactly a message arrives at this destination. Such applications are designed to handle node failures, network latencies, peak loads and similar situations.
MemBus is none of that. What is MemBus then?
It is a messaging framework available at github and licensed under the Apache 2.0 license that utilizes the semantics of sending and receiving messages. Those messages are not meant to leave the AppDomain where the Bus lives in. There is no durability - when the AppDomain is gone, the Bus is gone.
MemBus simply allows you to use the messaging paradigm while staying internal to your App. By the way, this concept has a lot of similarities to what people call Event Aggregator, however, I prefer to think of this pattern in a “bus/messaging”-way.
As a final note to this introduction here’s the most simple code possible with MemBus:
var bus = BusSetup.StartWith<Conservative>().Construct(); using (bus.Subscribe<MessageA>(Console.WriteLine)) bus.Publish(new MessageA());
The following posts will show you in what different ways you can setup MemBus in order to support different scenarios.