• RPC
  • What is RPC (Remote Procedure Call)?

What is RPC (Remote Procedure Call)?

🔹 What is RPC (Remote Procedure Call)?

Remote Procedure Call (RPC) is a protocol that allows a computer program to execute a function or procedure on another computer (remote server) as if it were a local function.

✅ Key Idea: RPC enables communication between different processes across a network or within the same system.
✅ Used In: Microservices, Distributed Systems, API Communication, Cloud Computing.


🔹 How Does RPC Work?

1️⃣ Client sends a request → Calls a function on a remote server.
2️⃣ Server processes the request → Executes the function.
3️⃣ Server sends a response → Returns the output to the client.

📌 Example of RPC Flow:

Client App  ── Request (RPC Call) ──> Remote Server  
Remote Server  ── Process & Execute ──> Send Response  
Client App  ── Receive & Process Response ──> Done ✅

🔹 Types of RPC

1️⃣ Synchronous RPC

  • Client waits for the server to respond before continuing.
  • Example: Traditional API calls (like REST).

2️⃣ Asynchronous RPC

  • Client does not wait for a response, allowing parallel execution.
  • Example: Event-driven microservices.

🔹 RPC vs. REST API

| Feature | RPC | REST API |
|---------|-----|----------|
| Protocol | Can use HTTP, TCP, UDP, gRPC, etc. | Uses HTTP |
| Data Format | Binary (Protocol Buffers, Thrift, Avro) or Text (JSON, XML) | JSON/XML |
| Performance | Faster (Binary format + Compression) | Slower due to HTTP overhead |
| Streaming Support | Yes (gRPC, Thrift, WebSockets) | Limited (Needs WebSockets) |
| Use Case | Microservices, High-Performance APIs | Web & Mobile APIs |


🔹 Popular RPC Frameworks

| RPC Framework | Used For | Protocol |
|------------------|-------------|-------------|
| gRPC | Microservices, Cloud APIs | HTTP/2 + Protocol Buffers |
| Apache Thrift | High-speed RPCs | Binary Protocol, TCP |
| JSON-RPC | Web APIs | HTTP + JSON |
| XML-RPC | Legacy systems | HTTP + XML |


🔹 Example of RPC Call

Using gRPC (Google RPC) in Python:

import grpc
import example_pb2
import example_pb2_grpc

channel = grpc.insecure_channel('localhost:50051')
stub = example_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(example_pb2.HelloRequest(name='Alice'))
print(response.message)

✅ This sends an RPC request to a server and gets a response.


🎯 When to Use RPC?

✅ When high-speed communication is needed (Microservices, Cloud).
✅ When APIs must support streaming & multiplexing (gRPC).
✅ When services need to communicate in different programming languages.

Would you like a step-by-step guide on how to implement gRPC in Python, Go, or Java? 🚀