There are basically two styles of communication: synchronous and asynchronous. These two styles applied properly are the foundation for request-reply and event-driven patterns. In the case of the request-reply pattern, a client initiates a request and typically waits synchronously for the reply. However, there are cases where the client could decide not to wait and register a callback with the other party, which is an example of the request-reply pattern in an asynchronous fashion.

In this article, I am showcasing the approach of asynchronous request-reply by having two services communicate with each other over Advanced Message Queuing Protocol (AMQP). AMQP is an open standard for passing business messages between applications or organizations. Although this article focuses on the request-reply pattern, the same code can be used to develop additional scenarios like event sourcing. Communicating using an asynchronous model can be very beneficial for implementing the aggregator pattern.

Solution components

This demo has three components:

frontend: This is a service written in Java and provides an HTTP endpoint to receive calls from clients. Upon receiving a request, the frontend service sends the call to the QPid router and registers a reply handler. The reply handler will be invoked by the Vert.x AMQP bridge when the response is available. The frontend folder in the codebase hosts this project.

QPid router: The frontend process takes the call and posts a message to the QPid queue. Vert.x automatically takes care of adding a correlationId as the message property to identify a response to the original request.

backend: The backend component listens for the message in the call from the QPid router, process it (e.g. doing a calculation or persisting in a database), and sends the response back to the QPid router. The QPid router will then notify the frontend component with the response. The backend folder in the codebase hosts this project.

Verifying the number of messages and latency

QPid provides an ultra-fast backbone as an asynchronous hub for communication between services. Once you finish testing your application, you can log into the QPid router’s Docker container using its IMAGE ID and run qdstat to see various metrics.

Conclusion

Apache QPid provides an ultra-fast backbone for communication between microservices. Since AMQP is a wire-level protocol, services written in other stacks (like .NET) can also use the same communication channel. Java developers can easily adapt to the AMQP-based asynchronous inter-services communication pattern using the Vert.x AMQP bridge.