Bonér’s Reactive Microservices Architecture: Design Principles for Distributed Systems is fundamental reading on architecting microservices.
We can’t add too much more here. Becoming a good architect of microservice systems requires time and experience. However, Lagom does attempt to guide your decision-making by offering opinionated APIs, features, and defaults.
Below are questions you should ask yourself as you design your services. The answers will guide you towards designing appropriately sized services with the right protocols.
The same questions apply, regardless of whether you’re building a new system from scratch, or extracting microservices from an existing monolith.
It should. If you can’t state a microservice’s full purpose with a short sentence, your service may be too big.
- Good: “This service manages friend relationships between users.” This service is appropriately sized.
- Bad: “This service manages friend relationships between users and aggregates all the activity for a user’s friends so it can be consumed”. This service is doing more than one thing and should probably be split up.
A service should be responsible for its own behavior. It shouldn’t rely on other services to do its job.
For example, let’s say you have an order service. Its protocol allows you to create an order, add items to it, add payment details to it, and confirm it. Confirming it requires invoking the payment service to make a payment. But what if the payment service isn’t running? This dependency means the order service is not autonomous.
An autonomous service would accept the confirmation request regardless of the status of the payment service. It might also, probably asynchronously, ensure that the payment is eventually processed.
A service “owns” data if it is the sole writer and the sole reader of the database where the data lives.
If you find yourself designing multiple services that access the same database, consider a redesign. Pick one service as owner; require the other services to use that service’s protocol to make read requests.