0
0
RabbitMQdevops~10 mins

Implementing RPC client and server in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, one program needs to ask another program to do a task and wait for the answer. This is called Remote Procedure Call (RPC). RabbitMQ helps programs talk this way by sending messages back and forth safely and quickly.
When a web app needs to ask a backend service to process data and wait for the result before continuing.
When a microservice needs to request information from another microservice and use the response immediately.
When you want to split a big task into smaller parts handled by different programs and collect their answers.
When you want to build a system where clients send requests and servers reply asynchronously but reliably.
When you want to avoid blocking your program while waiting by using message queues for communication.
Config File - rpc_server.py
rpc_server.py
import pika
import time

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='rpc_queue')

def on_request(ch, method, props, body):
    n = int(body)
    print(f" [.] fib({n})")
    response = fib(n)

    ch.basic_publish(exchange='',
                     routing_key=props.reply_to,
                     properties=pika.BasicProperties(correlation_id=props.correlation_id),
                     body=str(response))
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='rpc_queue', on_message_callback=on_request)

print(" [x] Awaiting RPC requests")
channel.start_consuming()

This Python script sets up the RPC server using RabbitMQ.

  • queue_declare creates a queue named rpc_queue where requests arrive.
  • on_request is the function called when a request message arrives; it calculates the Fibonacci number.
  • The server sends the result back to the client using the reply_to queue and correlation_id to match requests and responses.
  • basic_qos limits the server to process one request at a time for fairness.
Commands
Start the RPC server. It waits for requests and processes them one by one.
Terminal
python rpc_server.py
Expected OutputExpected
[x] Awaiting RPC requests
Run the RPC client script that sends a request to calculate a Fibonacci number and waits for the answer.
Terminal
python rpc_client.py
Expected OutputExpected
[x] Requesting fib(7) [.] Got fib(7) = 13
Key Concept

RPC with RabbitMQ uses a request queue for clients to send tasks and reply queues with correlation IDs to receive matching responses asynchronously.

Common Mistakes
Not setting the reply_to property in the request message
The server won't know where to send the response, so the client never receives the answer.
Always set the reply_to property to a unique callback queue name in the client request.
Not using correlation_id to match responses
If multiple requests are sent, the client can't tell which response belongs to which request.
Use a unique correlation_id for each request and check it when receiving responses.
Not acknowledging messages on the server
Messages may be redelivered or lost, causing duplicate processing or message loss.
Call basic_ack after processing each request to confirm successful handling.
Summary
Start the RPC server to listen on a queue for incoming requests.
Send requests from the client with reply_to and correlation_id properties.
The server processes requests and sends responses back to the client's callback queue.
Clients wait for responses and match them using correlation_id to handle multiple requests safely.