0
0
RabbitMQdevops~7 mins

RPC vs direct API calls in RabbitMQ - CLI Comparison

Choose your learning style9 modes available
Introduction
Sometimes, one program needs to ask another program to do something. RPC and direct API calls are two ways to do this. RPC uses messaging to send requests and get replies asynchronously, while direct API calls use HTTP requests directly between programs.
When you want to decouple services so they don't need to know each other's location or status.
When you want to handle requests asynchronously to improve system resilience.
When you need to call a service that might take time to respond without blocking the caller.
When you want simple, fast communication between services that are always available.
When you want to use HTTP methods like GET or POST directly for synchronous calls.
Config File - rpc_server.py
rpc_server.py
import pika
import time

def on_request(ch, method, props, body):
    n = int(body)
    print(f" [.] Received request for 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)

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(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='rpc_queue')
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 creates an RPC server using RabbitMQ.

  • queue_declare creates a queue named 'rpc_queue' where requests arrive.
  • basic_consume listens for messages and calls on_request when a request comes.
  • on_request calculates the Fibonacci number and sends the result back using the reply queue.
Commands
Start the RPC server that listens for requests and sends back responses asynchronously.
Terminal
python rpc_server.py
Expected OutputExpected
[x] Awaiting RPC requests
Make a direct API call to a web server that calculates Fibonacci synchronously and returns the result immediately.
Terminal
curl http://localhost:5000/api/fib?n=10
Expected OutputExpected
55
Run an RPC client that sends a request to the RPC server and waits for the response asynchronously.
Terminal
python rpc_client.py
Expected OutputExpected
[x] Requesting fib(10) [.] Got fib(10) = 55
Key Concept

RPC uses messaging to let programs talk asynchronously and decoupled, while direct API calls use synchronous HTTP requests for immediate responses.

Common Mistakes
Trying to use RPC without setting up reply queues and correlation IDs.
Without reply queues and correlation IDs, the client cannot match responses to requests, causing lost or mixed messages.
Always set a unique reply queue and correlation ID for each RPC request to track responses properly.
Using direct API calls for long-running tasks that block the client.
Direct API calls wait for the server to finish, which can cause timeouts or slow user experience.
Use RPC or asynchronous messaging for long tasks to avoid blocking the client.
Summary
RPC uses message queues to send requests and receive replies asynchronously.
Direct API calls use HTTP requests for synchronous communication.
Use RPC to decouple services and handle long tasks without blocking.