Tools for synchronous communication
The most common form of direct communication between microservices is using the HTTP protocol with Rest and passing JavaScript Object Notation, the famous JSON. Communication works great for APIs, providing endpoints for external consumption. However, communication using HTTP with JSON has a high cost in relation to performance.
First, this is because in the case of communication between microservices, it would be more appropriate to optimize than the HTTP protocol creating some sort of pipeline or keeping the connection alive. The problem is the control of the connection timeout, which shouldn't be very strict, and in addition could start to close doors, threads, or processes with a simple silent error. The second problem with this approach is the serialization time of JSON sent. Normally this is not an inexpensive process. Finally, we have the packet size sent to the HTTP protocol. In addition to JSON, there are a number of HTTP headers that should be interpreted further which may be discarded. Look closer; there's no need to elaborate on protocols between microservices, the only concern should be to maintain a single layer for sending and receiving messages. Therefore, the HTTP protocol with JSON to communicate between microservices can be a serious slow point within the project and, despite the practicality of implementation of the protocol, the optimization is complex to understand yet not very significant.
Many would propose the implementation of communication sockets or WebSockets but, in the end, the customization process of these communication layers is very similar to the classic HTTP.
Synchronous communication layers between microservices must complete three basic tasks:
- Report practice and direct the desired messages
- Send simple, lightweight packages and fast serialization
- Be practical to maintain the signature of communication endpoints
A proposal that meets the aforementioned requirements communicates using binary or small-size packages.
Something important to point out when it comes to working with this type of protocol is that, usually, they are incompatible with each other. This means that the option chosen as the tool for serialization and submission of these small-sized packages should be compatible with the stack of all microservices.
Some of the most popular options on the market are:
- MessagePack (http://msgpack.org/)
- gRPC (https://grpc.io/)
- Apache Avro (https://avro.apache.org/)
- Apache Thrift (https://thrift.apache.org/)
Let us understand how each of these options works to see what best fits on our news portal.