1. Definition
RPC (Remote Procedure Call): A protocol or concept that allows a program to execute a procedure (function) on another address space (commonly on another computer on a shared network) as if it were a local call. Examples include gRPC, JSON-RPC, XML-RPC.
REST (Representational State Transfer): An architectural style for designing networked applications. RESTful APIs use HTTP requests to perform CRUD (Create, Read, Update, Delete) operations on resources, which are identified by URLs. Most modern web APIs are RESTful.
2. Main Differences
Aspect | RPC | REST |
---|---|---|
Philosophy | Procedure-oriented (call methods/functions) | Resource-oriented (manipulate resources) |
Interface | Methods/functions (e.g., getUser() ) | Resources/URLs (e.g., /users/123 ) |
Protocol | Can use various (HTTP, TCP, etc.) | Always uses HTTP |
Data Format | Often binary (gRPC/Protobuf), or JSON/XML | Usually JSON, XML, or plain text |
Verb Usage | Method name defines action | HTTP verbs (GET, POST, PUT, DELETE, etc.) |
Flexibility | Tightly coupled, often auto-generated code | Loosely coupled, human-readable |
Use Case | Microservices, internal APIs, high perf. | Public APIs, web services |
Name Differentiation & Formatting
-
RPC:
- Endpoints are typically named after actions or procedures, often in camelCase or PascalCase.
- Example:
getUser
,CreateOrder
,deleteProduct
- The method name itself conveys the operation.
-
REST:
- Endpoints are named after resources, usually in lowercase and plural form, using URL paths.
- Example:
/users/123
,/orders
,/products/456
- The HTTP verb (GET, POST, PUT, DELETE) combined with the resource path conveys the operation.
3. Example
RPC Example
Go (gRPC)
// service.proto
type UserService interface {
GetUser(ctx context.Context, req *GetUserRequest) (*User, error)
}
// Client call
resp, err := client.GetUser(ctx, &GetUserRequest{Id: 123})
Python (gRPC)
# user_service.proto
definition:
service UserService {
rpc GetUser (GetUserRequest) returns (User);
}
# Python client usage (using grpcio)
import grpc
from user_pb2 import GetUserRequest
from user_pb2_grpc import UserServiceStub
channel = grpc.insecure_channel('localhost:50051')
stub = UserServiceStub(channel)
response = stub.GetUser(GetUserRequest(id=123))
print(response)
REST Example
Go (net/http)
import (
"encoding/json"
"net/http"
"strconv"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
func getUserHandler(w http.ResponseWriter, r *http.Request) {
idStr := r.URL.Path[len("/users/") :]
id, _ := strconv.Atoi(idStr)
user := User{ID: id, Name: "Alice"}
json.NewEncoder(w).Encode(user)
}
func main() {
http.HandleFunc("/users/", getUserHandler)
http.ListenAndServe(":8080", nil)
}
Python (Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
# Fetch user from database
return jsonify({'id': user_id, 'name': 'Alice'})
if __name__ == '__main__':
app.run(port=8080)
4. Use Cases
When to use RPC:
- Internal microservices communication
- High performance and low latency required
- Strongly-typed contracts (e.g., gRPC with Protobuf)
- Need for streaming or bi-directional communication
When to use REST:
- Public APIs or web services
- Simplicity and human-readability are important
- Broad client compatibility (browsers, mobile, etc.)
- Stateless operations and resource-oriented design