Microservices with Clojure
上QQ阅读APP看书,第一时间看更新

Direct messaging

In direct messaging, each request is sent directly to the microservice on its API endpoint. Such requests may be initiated by users, applications, or by other microservices that integrate with the target microservice to complete a particular task. Mostly the endpoints to handle such requests are implemented using REST (https://en.wikipedia.org/wiki/Representational_state_transfer), which allows resource identifiers to be addressed directly via HTTP based APIs with a simple messaging style.

REST is an architectural style with predefined operations based on HTTP request methods, such as GET, PUT, POST, and DELETE. The stateless nature of REST makes it fast, reliable, and scalable with multiple components. 

For example, in a typical microservices-based e-commerce application, an administrative user may wish to create, update, or manage users via the User Service's REST endpoint. In that case, the user can directly call the REST API of the User Service, as shown in the following diagram:

Similarly, a mobile application may want to query user interests via Interest Service's REST endpoint of the e-commerce application. Since the Interest Service depends on both the User Service and the Orders Service, it may further initiate two separate calls to the REST endpoint of these two services to get the desired response which it can merge with the user interests data and generate the response for the requesting application. Mostly, all these kinds of request-response are synchronous in nature as the sender expects a response with the results in the same call. Asynchronous requests are mostly used to send alerts or messages to record an operation, and in those cases, the sender doesn't wait for the response and expects the action to be taken eventually.

Avoid building long chains of synchronous calls as that may lead to high latency and an increased possibility of failure due to multiple hops of intermediate requests and network round-trips among the participating services.

Message formats used with REST endpoints are mostly text-based message formats such as JSON, XML, or HTML, as supported by the endpoint implementation. Binary message formats such as Thrift (https://en.wikipedia.org/wiki/Apache_Thrift)ProtoBuf (https://en.wikipedia.org/wiki/Protocol_Buffers), and Avro (https://avro.apache.org/) are also popular due to their wide support across multiple programming languages.

Use direct messaging only for smaller microservices-based deployments. For larger deployments, it is advisable to go with API gateways that act as the main entry point for all clients. Such API gateways help monitor requests for microservices and also assist in the maintenance and upgrade operations.