skip to content
Alvin Lucillo

Sample RPC transactions

/ 2 min read

In real world, this is done by programs, but for now, let’s just simulate this using rabbitmqadmin commands.

# Server declares a request queue
➜ docker exec -it rabbitmq rabbitmqadmin declare queue --name=rpc_queue --durable=false --auto-delete=false
# Client declares a reply queue
➜ docker exec -it rabbitmq rabbitmqadmin declare queue --name=rpc_reply_demo --durable=false --auto-delete=true
# Client sends a request. Notice that there is a correlation ID. Later, when a message is received in the reply queue, we should check the correlation ID of the message.
➜ docker exec -it rabbitmq rabbitmqadmin publish message \
  --exchange=amq.default \
  --routing-key=rpc_queue \
  --payload="8" \
  --properties='{"content_type":"text/plain","correlation_id":"corr-001","reply_to":"rpc_reply_demo"}'

Message published and routed successfully

# Server consumes the request
➜ docker exec -it rabbitmq rabbitmqadmin get messages --queue=rpc_queue --count=1 --ack-mode=ack_requeue_false
┌───────────────┬─────────────┬──────────┬─────────────┬───────────────┬────────────────────────────┬─────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties                 │ payload │ payload_encoding │
├───────────────┼─────────────┼──────────┼─────────────┼───────────────┼────────────────────────────┼─────────┼──────────────────┤
│ 1             │ false       │          │ rpc_queue   │ 0             │ content_type: "text/plain" │ 8       │ string           │
│               │             │          │             │               │ correlation_id: "corr-001" │         │                  │
│               │             │          │             │               │ reply_to: "rpc_reply_demo" │         │                  │
│               │             │          │             │               │                            │         │                  │
└───────────────┴─────────────┴──────────┴─────────────┴───────────────┴────────────────────────────┴─────────┴──────────────────┘

# Server publishes a response to the message it received by using the reply_to value, which is the reply queue. The message must contain the same correlation ID so the client knows it's a reply to the prior message.
➜ docker exec -it rabbitmq rabbitmqadmin publish message \
  --exchange=amq.default \
  --routing-key=rpc_reply_demo \
  --payload="21" \
  --properties='{"content_type":"text/plain","correlation_id":"corr-001"}'
Message published and routed successfully

# Client receives the message with the same correlation ID and knows it's a reply from the prior message.
➜ docker exec -it rabbitmq rabbitmqadmin get messages --queue=rpc_reply_demo --count=10 --ack-mode=ack_requeue_false
┌───────────────┬─────────────┬──────────┬────────────────┬───────────────┬────────────────────────────┬─────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key    │ message_count │ properties                 │ payload │ payload_encoding │
├───────────────┼─────────────┼──────────┼────────────────┼───────────────┼────────────────────────────┼─────────┼──────────────────┤
│ 2             │ false       │          │ rpc_reply_demo │ 0             │ content_type: "text/plain" │ 21      │ string           │
│               │             │          │                │               │ correlation_id: "corr-001" │         │                  │
│               │             │          │                │               │                            │         │                  │
└───────────────┴─────────────┴──────────┴────────────────┴───────────────┴────────────────────────────┴─────────┴──────────────────┘