Service to Service Communication
Something else that needs to be considered is how services will communicate with one another. The checkout service might need information from the catalog service, or the checkout service might need to send some information to a notification service. Quite often this is in the form of a REST-based API over HTTP/S using JSON serialization, but this is not always the best choice for certain interactions. There are a number of different protocols, serialization formats, and trade-offs to consider. A quick online search will find a lot of great information available covering the performance and management trade-offs with the various serialization options.
When designing our microservices architecture, we need to consider how our services will communicate. We want to make sure we can avoid unnecessary communication across services and make the communication as efficient as we possibly can.
Whatever we select for our interface must be a well-defined interface that hopefully does not introduce breaking changes for consumers of a specific version. It’s not that we can’t change the interface; the key difference is breaking changes. Two different communications styles used between services are synchronous and asynchronous request/response. This means the services implemented will need to use some kind of versioning strategy for the eventual breaking change to an interface.
Many developers are very familiar with the synchronous messaging approach. A client sends a request to a back-end and then waits for a response. This style makes a lot of sense: it’s easy to reason about and it’s been around for a long time. The implementation details might have evolved over time.
One of the challenges with synchronous messaging at scale is that we can tie up resources waiting for a response. If a thread makes a request, quite often it sits and waits for a response, utilizing precious resources and doing nothing while it waits for the response. Many clients can use asynchronous event-based implementations that enable the thread to continue doing work, and then execute a callback when a response is returned. If we need to make synchronous requests to a service, this is the way to do it. The client does, however, expect a timely response from the service. See Appendix A in this book for best practices when implementing and consuming APIs with ASP.NET. Many of the concepts discussed are transferrable to other languages and frameworks.
Another useful approach for inter-service communications in distributed systems is asynchronous messaging. Using an asynchronous messaging approach, the client makes a request by sending a message. The client can then continue doing other things, and if the service is expected to send a response it does this by sending another message back. The client will generally wait for acknowledgment that the message was received and queued, but will not have to wait for the request to be processed with a response. In some cases the client does not need a response or will check back with a service at a later time on the status of the request.
There are a number of benefits to asynchronous messaging, but also some trade-offs and challenges that need to be considered. A great introduction to asynchronous messaging is available here from the Microsoft Patterns and Practices team at https://msdn.microsoft.com/en-us/library/dn589781.aspx. This material is a great primer to help those new to asynchronous messaging concepts.
As we mentioned previously, when designing an application using a microservices approach, there are a number of things to consider with regard to communication style, protocols, and serialization formats used.
Considerations with inter-service communications:
Select a communication approach that is best suited for the interaction, and if possible use asynchronous messaging.
Consider protocol and serialization formats and the various trade-offs. Performance is important, but so is interoperability, flexibility, and ease of use.
Always consider how coupling is affected by any decision, because as our services become coupled we tend to lose many of the benefits of a microservices architecture.
Always consider caching when making requests on external systems.
Always implement the resiliency patterns covered in Chapter 1 in the Best Practices subsection when calling other services.
When one service takes a dependency on another service, it should not require the dependent service to be available at deployment or impact its ability to start. There should never be a need to deploy and start services in a specific order.
Consider alternate approaches to reduce overhead. For example, if one service uses information received in a request to call another with additional information there might be no need to reserialize the original message. The original message can be passed with the additional information in the header.
There’s a place for both synchronous request/response and asynchronous messaging in today’s systems. We really just touched the surface on some inter-service communications concepts in this section. For more information on routing requests to services see the Service Discovery section in Chapter 5. Inter-service communication is an important consideration when designing an application based on microservices architecture.