In a microservice-based system, it exist some principles. Sam Newman often refers to them as these:
- You should always model your application around the domain
- A culture of automation. Automate whats possible to automate
- Hide your implementation details
- Decentralize everything
- Deploy indepentently
- Isolate your failures
- Highly observable
https://samnewman.io/talks/principles-of-microservices/
When building a system where you microservices reflects the domain, which basically means to me that you have one microservice for each defined bounded context, I find it quite difficult to deal with a servicebus architecture and events published.
An example:
Lets say you're creating a system for a foodstore chain where the customers can sign up for a bonus program, which gives them x % back for every purchase they make.
So you and your team sits down with the customer to identify the domain and you identified at least two bounded contexts. The contexts identified are: Customer and Loyalty. You start out by making two microservices based on these two contexts, CustomerService
and LoyaltyService
.
The business defines a rule that, whenever a customer signs up, a 5 % discount coupon shall be sent to the customer's email.
The CustomerService exposes an API to create the customer in the database and when the customer is created an event CustomerCreatedEvent
is published on the servicebus. As the LoyaltyService
is interested in this event to send the 5 % discount coupon, it has to subscribe to this message. In NServiceBus
, which is my prefered servicebus, its done this way:
public class SomeClassName : IHandleMessage<CustomerCreatedEvent>
You might already figured it out, but this is where I struggle to find a good solution to be "compatible" with the principles of microservices. How should the LoyaltyService
be able to subscribe and deserialize this event without having a reference to CustomerService
either over NuGet or direct reference? Should we duplicate our code so that the CustomerCreatedEvent are defined in both services?
Any suggestions, blog posts, books, pluralsight courses on how to create a domain driven microservice architecture application would be very helpful.
Thanks.