LavinMQ Remote procedure call

In an RPC (Remote Procedure Call), a client sends a request to run a function and any related arguments to a program located on another server. The remote server runs the task and sends back the response. The compiled code includes a stub that acts as a representative of the remote procedure code. The stub receives the request while the program is running and the procedure call has been issued. It forwards it to a client program in the local computer, while the initiating program waits until it receives a response.

The RPC process works somewhat differently in LavinMQ. There is no stub available. The program will not stall if you use a non-blocking consumer. Instead, your client sends a message to the broker containing the reply-to header. The target server processes the request and sends data back to the server. Then, in regular pub/sub fashion, your consumer receives the response.

What is LavinMQ RPC?

The reply-to header in LavinMQ lets you create applications that communicate with one another over a special form of RPC.

How does RPC work in LavinMQ?

After a client sends the request to a server, the server gives a response message in return. The client sends a callback queue address with its request in order to receive a response. One queue receives the response while another consumer handles the reply.

Another way to handle RPC in LavinMQ is by using direct reply-to, which passes a response directly to the client, avoiding having to create a response queue to wait on.

When to Use RPC in LavinMQ?

RPC using the LavinMQ direct reply-to header allows you to create responsive applications since you can perform tasks remotely from your mobile and desktop applications.

LavinMQ RPC

There are different RPC options in LavinMQ. One is to let the client send a request message and let a server reply with a response message, sent into a specified queue. This queue is called a callback or response queue. The name of this queue must be sent as an address in the request message sent from the client:

channel.basic_publish(exchange='',
routing_key='rpc_queue',
properties=pika.BasicProperties(
reply_to = response_queue,
),
body=request)

In the code shown above is a callback queue added for every RPC request, which might be confusing since it’s not clear to which request the response belongs. A correlation_id (a unique value) can therefore be sent with every request.

channel.basic_publish(exchange='',
routing_key='rpc_queue',
properties=pika.BasicProperties(
reply_to = response_queue,
correlation_id = correlation_id),
body=request)

Workflow of RPC callback queues in LavinMQ:

  • The client creates an exclusive callback_queue.
  • The client sends a message containing the reply queue, the callback queue along with the correlation_id. The value of the correlation_id is set to a unique value at every request.
  • Next, the request is sent to rpc_queue.
  • The RPC worker/consumer, i.e. the remote server, is waiting for requests on that queue (rpc_queue) and handles the request once received.
  • The result is sent back to the broker by the remote server using the reply_to field to know which queue it belongs in. The response goes to the response_queue.
  • The callback queue is where the client/consumer waits for data.
callback_queue = channel.queue_declare(queue='', exclusive=True)
channel.basic_publish(exchange='',
routing_key='rpc_queue',
properties=pika.BasicProperties(
reply_to = callback_queue,
correlation_id = correlation_id
),
body=request)