visit
Proper choice of the communication protocol over Microservices makes the application more responsive and efficient. Various aspects need to be
considered regarding communication before breaking down into services.
There are 2.5 quintillion bytes of data generated every day. Software built
using monolithic architecture may face problems like difficulties in scaling, longer time to ship, spaghetti code, etc.
As the services get deployed independently, it requires an efficient protocol to communicate between the services. Improper use of the
communication protocol may lead to poor performance in applications.
Synchronous Protocol
The client requests service and waits for the response. The synchronous protocol is blocking, i-e CPU waits for the response. HTTP is a type of synchronous communication. Modern web applications follow the RESTful
architectural style to build APIs for inter-process communication. REST treats every object or entity as a service resource that is accessed by using the HTTP verbs.
Asynchronous Protocol
Synchronous communication between the microservices lags in performance. Microservices may form a chain of requests between services to respond to a single client. Asynchronous communication is recommended over the synchronous to be a resilient microservices. The asynchronous protocol is non-blocking in nature. If services form the chain of request over microservices it increases the communication overhead which leads to poor performance. It follows the event-driven architecture, there may be one to one and one to many mapping between event producer and consumer.Single Receiver
Each request has one sender and one receiver. In the case of multiple requests, they get queued because of the single receiver which can consume one at a time.Multiple Receiver
Each request can be processed by zero to multiple receivers. This is
based upon the event-bus interface or message broker while exchanging data between the microservices through events.
Publisher
The publisher doesn’t need the subscriber to be up and available. It sends a message to the message queue. Nor does the publisher need to know about the subscriber application that needs to receive the message. Once the message is published in the specific topic the message gets broadcast in the channel to notify the subscribers. Subscribers consume the message in the queue.Subscriber
The subscriber subscribes to a particular topic to get notified once the message is published in the respective topic. There may be many subscribers as client request grows.Input Channel
Channel used by the publisher to send message to the subscriber.Output Channel
Channel used by the subscriber to consume the message.Message Broker
Message from the publisher is copied from the input channel to the output channel of interested subscribers, this mechanism is handled by the message broker. All these events occur asynchronously. There are many message broker and third-party services to facilitate the publish/subscribe protocol.Among them, RabbitMQ, Amazon SQS, Redis, Apache Kafka, Amazon SQS, Google Cloud Pub/Sub are used widely.
RabbitMQ
It provides the flexibility to send messages up-to 50k per second. Message
brokers can be configurable to both transient and persistent. The subscriber can be both one or many as per need. It follows the Advanced message queuing protocols. RabbitMQ is best suited for complex routing.
General programming languages are supported.
Kafka
Kafka is an open-source stream-processing software platform developed by LinkedIn. It has high-throughput, low latency for handling real-time data feeds. It provides the data persistency as well and supports one to many subscribers only. It is best to fit to play with large amounts of data.Redis
Redis is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability. It can send up to a million messages per second. It is best suited for the use case of short message retention and where persistence doesn’t matter. It supports one to many subscribers and provides extremely fast service.ORCHESTRATION
In orchestration, there is a single orchestrator that acts as a coordinator to send responses for clients. Orchestrator synchronously manages all services to serve clients. In this pattern, services are tightly coupled to each other. Once the coordinator goes down there will be a single point of failure. As all services communicate synchronously, the time to send response grows as the interaction between services increases.CHOREOGRAPHY
Central orchestrator creates the dependencies within the microservices. In microservice, each service should be able to stand on its own. Choreography solves some of the problems of orchestrators. It enables faster processing as it can execute parallel/asynchronously. Services can be plugged as the user grows because the publisher doesn’t have to be aware of the subscriber.Direct client to microservice communication approaches each microservice
has a public endpoint. The client directly consumes the endpoint of services in the production environment. Most of the time these services have communication by means of Advanced messaging queue, gRPC,unlike HTTP protocol. Clients may not be able to have communication with services in that scenario.
API gateway is a hybrid approach to both orchestrator and choreography.
While designing the large or complex microservice-based application having multiple client apps like Android, iOS, and Single Page Applications, etc, API Gateway is a better approach to deal with. It is similar to the Facade pattern from object-oriented design. It provides the abstraction of the internal communication of microservices as it is tailored to each client. It might have other responsibilities such as authentication, monitoring, load balancing, caching, request shaping, and static response handling.
Conclusion
The synchronous communication protocol may form a chain of requests
while sending a response to the client, which may lead to bad user experience and a single point of failure. Asynchronous communication is
preferred over the synchronous which will be more responsive and fault-tolerant. While building applications, proper architecture plays an important role to make applications more robust, fault-tolerant, and scalable.
References
Previously published at h