Repository navigation
MicroService Messaging Patterns Events
On the previous pages we explained that Commands and Queries are both request-types that are sent to and consequently processed by the back-end of a specific service. They can originate from either a front-end or from a back-end. In addition, requests can be sent and processed both in synchronous and asynchronous fashion.
Events are, in this respect, the complete opposite to request-messages. First of all, Events represent facts: pieces of information that reflect a change or occurrence in the system. These changes and occurrences are managed by back-end components and, as such, Events are exclusively created and published by the back-end of a service. They can, however, be handled by both front-end and back-end components. Front-end component may, for example, update the user interface as a reaction to incoming Events, whereas back-end components may handle Events by executing more logic or by simply updating some views in a database.
Furthermore, Events are only ever (supposed to be) sent asynchronously through a service bus. This is because the sender of the Event (i.e. its publisher) doesn't know (and doesn't need to know) who its consumers are. It doesn't even know, in fact, if the Event is handled by anybody at all, nor does it expect a response from anybody that might be handling the Event. Consequently, Events are just published to the service bus; any service/component that is interested in handling this Event should subscribe to that Event via the service bus. This pattern is commonly known as the publish-subscribe or notifier-observer pattern, although the latter term is more often used in an Object-Oriented context and the former in a Service-Oriented context:

Events are always published to a service bus and then distributed to its receivers through subscriptions, which can be (other) services and/or client components.
Note that each Event can only be published by one specific service (namely the service that owns and defines that Event in its contract), but that there may be an arbitrary amount of subscribers for one specific Event, and that this amount may even vary dynamically during run-time as subscribers enter or leave the service landscape.
The service bus is the infrastructural component that should implement the publish-subscribe capability. Today, there are many frameworks and services out there that offer this capability for you to buy and use, such as Azure Service Bus, RabbitMQ, MassTransit and NServiceBus. They all work and name things in slightly different ways, but in principle they all use a design along the following lines:
- To publish its Events, a service owns one specific outbox in the service bus. Each time this service publishes an Event, it essentially sends a copy of that Event to this outbox.
- To subscribe to a certain Event, a service can create a subscription to the outbox of the service that publishes that Event, which is basically a queue in which Events can be stored. Whenever an Event is published, a copy of that Event to all subscription-queues that were created for that Event. Each service that is attached to that subscription can now dequeue and process that Event as desired.
Note that the outbox of a service does not actually store the Events sent to it; it merely serves as a message-router to the subscriptions that are created on it. In other words, when a message is published and received by the outbox, it immediately forwards a copy of this Event to each subscription that was created for this Event. If no such subscriptions exist, the Event is effectively ignored.

Events are published to an outbox which then distributes copies of the Event to each subscription that was created for that Event. Different services can choose to handle the same Event; Services can even handle the same Event more than once through multiple endpoints, each having its own subscription in the service bus.
Also note that, although many service bus implementations do not specifically enforce this, it is common to have each subscription only store and forward one specific type of Event. This allows you to map each subscription in the service bus onto one specific endpoint in your service that specializes in handling that Event. There could be, however, multiple subscriptions for the same Event, even created by a single service, if it has multiple endpoints that handle that Event. One Event may trigger, for example, an update in the UI (endpoint A), a piece of business logic (endpoint B) and the update of a read-optimized view in the database (endpoint C), all in the same service.
The fact that a service bus actually stores all Events in subscription-queues makes it an asynchronous messaging pattern and allows for senders and receivers to publish and/or handle Events completely run-time independent from each other. If, for example, a receiver is being updated or temporarily unavailable for some reason, any Event that it would normally handle will remain stored in its subscription(s) until it comes back online again to resume its Event processing. If, on the other hand, the publisher goes down for a moment, then subscribers can continue their normal activity, although they may receive some Events a little later than usual.
Another advantage of the publish-subscribe pattern is that it increases the scalability of your architecture. Scaling out a service while it has several subscriptions in the service bus is very similar to scaling out while attached to a command-queue: every instance attaches itself to the same subscriptions, and for each Event that is forwarded to a subscription, the instance that is first to grab this Event gets to handle it:

When scaling out, all instances of a service are connected to the same subscriptions (queues) in the service bus.
Remark that new instances of the same service should not create their own, separate subscriptions for the same Events/endpoints in the service bus, because this would mean that each instance gets its own copy of the Event and the same Event will be handled N times instead of just one time. This pattern, where multiple instances are attached to the same queue and the messages of the queue are divided among these instances is called the competing-consumers pattern.