The benefit of RPC Calls

Azure Nov 26, 2023

In the ever-evolving landscape of microservices architecture, communication between services is a critical aspect that demands attention. Remote Procedure Call (RPC) is a paradigm that allows one service to invoke a function or method on another service, creating a seamless flow of data and functionality. This will decouple the dependency between (for example) an API and the data layer. In this blog post, we will explore the implementation of RPC calls using Azure Service Bus and MassTransit, two powerful tools that streamline communication in distributed systems.

Understanding the Problem:

In a microservices environment, services often need to communicate with each other to fulfill specific business requirements. Let's consider a scenario where Service A needs to retrieve information from Service B to complete a user request. This is where RPC comes into the game, this pattern is a solution to let the two components interact together.

Using Azure Service Bus

Azure Service Bus is a fully managed message broker that facilitates communication between applications and services. It supports various messaging patterns, including RPC. By leveraging Azure Service Bus, we can ensure reliable and asynchronous communication between microservices.

Sure you can use any other bus system like rabbitmq, but this article will focus on Azure service bus.

The Implementation with MassTransit:

MassTransit is a powerful, open-source message bus framework for .NET that simplifies building distributed systems. It abstracts the complexities of messaging and allows developers to focus on business logic. Combining MassTransit with Azure Service Bus enables us to implement RPC calls seamlessly.

Let's delve into the code to understand how to achieve RPC calls using Azure Service Bus and MassTransit. First of all the Sender Request implementation

public async Task<TResponse> MakeRpcCall<TRequest, TResponse>(TRequest request)
    var requestClient = _busControl.CreateRequestClient<TRequest, TResponse>(new Uri("queue:serviceBQueue"));

    var response = await requestClient.Request(request);

    return response.Message;

Now the Receiver Implementation

public void ConfigureBus(IServiceBusBusFactoryConfigurator configurator, IServiceProvider provider)
    configurator.ReceiveEndpoint("serviceBQueue", endpoint =>
        endpoint.Handler<YourRequestType>(async context =>
            // Process the request and generate the response
            var response = ProcessRequest(context.Message);

            // Send the response back to the requester
            await context.RespondAsync(response);

Mass transit will create the desired queues for you so that the request response system will work. In this example, Service A makes a call against Serice B (via the Bus). Service B will then send the response answer back to Service A.

How is it working?

Normally when you are using a Bus for communication you will use it as a fire & forget system like this


You as Sender will send a small message onto the bus system and the desired receiver do with the message. But in some cases like the actual example, it will be neccessary to get a feedback. So in this example you will send a message, and within the message paylod you will define a "reply-queue". The receiver will now receiver the message as normal and know (because due to the reply-queue) the the result must be send via another queue defined in the reply-queue.


So this is not rocket science it's quite easy, but there is many configuration overhead that will be taken over by MassTransit (or other good frameworks).

Closing Words:

Implementing RPC calls in a microservices architecture is crucial for building scalable and responsive systems. Azure Service Bus, coupled with MassTransit, provides an elegant solution to address the complexities of inter-service communication. By adopting these tools, developers can focus on business logic and let the underlying infrastructure handle the intricacies of message-based communication. As microservices continue to gain prominence, mastering RPC calls with Azure Service Bus and MassTransit becomes a valuable skill for building robust and maintainable distributed systems. Of course, you can create a API that does a synchronous request against the system, but internally it will make an RPC call. But please pay attention, this all can get very complex over time and it must be well documented, to avoid problems and circular depdendencies.

What do you think about RPC?