10 best practices for microservice Architecture Design
Microservices have dramatically changed the way server engines are architected. Microservices are not a single giant code base that hosts all of an application’s business logic, but rather reflect a distributed system model in which a set of application components work together to meet business requirements. By following ten basic microservices best practices, you can implement an efficient microservices ecosystem that avoids unnecessary architectural complexity.
Benefits of microservices architecture
When properly migrating from monolithic applications to microservices architectures, the following benefits can be achieved:
You can develop microservices in a language of your choosing, release them independently at your own pace, and expand them independently.
Different teams in an organization can have their own specific microservices independently, and with increased parallel development and reuse, product release times are faster. Failures are better isolated because errors that occur in a particular microservice are handled in the corresponding service and therefore do not affect other services in the ecosystem.
However, if you don’t follow the right principles when building microservices, you can end up like a tangled spaghetti mess.
This makes maintenance very difficult because it requires different teams to work together to make changes, release, or implement fault tolerance.
Making the most of microservices is a science and requires some deliberate practice. The following microservices best practices and design principles will help you build loosely coupled, distributed and optimized microservices for optimal value.
10 best practices for microservices
1. Principle of single liability
Just like a class in code that changes only for a single reason, microservices are modeled in a similar way. Building such bloated services that may change more than one business is a bad practice.
2. Independent data store
If all your microservices share a database, it defeats the purpose of using microservices. Any changes or failures to the database affect all microservices that use the database. Choose the right database for microservices, customize the infrastructure and storage of the corresponding data, and let your services own it. Ideally, any other microservice that needs access to this data can only be accessed through the API provided by the microservice that has write permission.
3. Loose coupling using asynchronous communication
To avoid building a tightly coupled mesh of components, consider using asynchronous communication between microservices.
A. Invoke the dependent service asynchronously.
For example, here is an example where service A depends on service B. When service B returns A response message, service A returns success to the caller who called service A. If the caller does not care about the contents of service B, then service A can call service B asynchronously and immediately return success to the caller.
B. A better option is to use event mechanisms between microservice communications. Your microservice can publish an event message to the message bus, which can be used to notify a state change or a failed event, and any microservice interested in the event can obtain the message and act accordingly.
For example, in the pizza order system mentioned above, asynchronous communication can be used to send notification messages to the customer when the customer’s order has been received or the order has been completed and the shipping status messages are sent. The notification service can listen for an order submission message event and push the corresponding notification to the customer.
4. Use fuses to quickly realize fault tolerance
If your microservice relies on another system to provide responses, and that system takes a long time to respond, your overall response SLA will suffer. To avoid this scenario and respond quickly, a simple microservice best practice you need to follow is to use fuses to timeout external calls and then return a default response or error. The fuse mode can be referenced at the bottom. This approach isolates failed services without causing cascading failures and keeps your services in a healthy state.
5. Proxy microservice requests through the API gateway
It is more valuable to use a separate API gateway to do these things, rather than each microservice in the system providing API authorization, request/corresponding logging, and flow limiting functions separately. The client calling your microservice can connect to the API gateway instead of calling the microservice interface directly. This way your microservice avoids making those extra calls, and the internal URL of the microservice can be hidden, which gives you more flexibility to redirect traffic from the API gateway to a newer version of the microservice. This is even more necessary when allowing third parties access to your microservice because you can limit incoming traffic and reject unauthorized requests from the API gateway before they reach the microservice. You can also select a separate external gateway to receive traffic from the external network.
6. Ensure API changes are backward compatible
You can safely make changes to the API and quickly publish them, as long as the changes don’t affect existing callers. One possible option is to notify your callers to validate the changes through integration tests. However, this can be costly because all dependencies need to be queued in one environment, which slows down your coordination. A better option is to use contract tests on your API. Your API consumer provides the contract for the expected response to the API. As an API provider, you can integrate these contract tests as part of your build and these can safely guarantee significant API changes. Consumers can test your published stubs as part of their build. This allows you to release the product more quickly by testing contract changes independently.
7. Major changes in versioning microservices
It is not always possible to make changes backward compatible. When you make a major change and need to continue supporting the old interface, you can expose a new version of the interface. Consumers can choose a new version at their convenience. But having too many versions of the API can be a nightmare for the people who maintain them. Therefore, a canonical approach is to deprecate older versions by collaborating with clients or internally rerouting traffic to newer versions.
8. Host microservices using dedicated infrastructure
You’ve developed the best microservice that meets all the checks, but with a poor hosting platform, the end result will still be poor. Isolating your microservices infrastructure from other components allows for failure isolation and optimal performance. Isolating the component infrastructure that microservices depend on is equally important.
9. Create a separate publishing channel
Your microservices need to have a separate distribution channel that is not associated with other components in your organization. That way you won’t have conflicts with others and waste time coordinating with multiple teams.
10. Build organizational efficiency
While microservices give you the freedom to develop and publish independently, for cross cutting concerns, certain standards need to be followed so that each team doesn’t have to spend time creating unique solutions to these problems. This is important in distributed architectures such as microservices, where you need to be able to connect all the pieces of the puzzle to see the big picture. Therefore, enterprise-level solutions are a must for API security, log aggregation, monitoring, API documentation, key management, configuration management, distributed tracing, and so on.
By following these microservice best practices, you can have a loosely coupled, distributed, and independent microservice system, and you can reap the real benefits of the microservice architecture outlined at the beginning of this article.
Does more articles and materials | click behind the text to the left left left 100 gpython self-study data package Ali cloud K8s practical manual guide] [ali cloud CDN row pit CDN ECS Hadoop large data of actual combat operations guide the conversation practice manual manual Knative cloud native application development guide OSS Operation and maintenance actual combat manual cloud native architecture white paper Zabbix enterprise level distributed monitoring system source document Cloud native basic introduction manual 10G large factory interview questions