REST vs RPC - API Design Paradigms
What is RPC?
Section titled “What is RPC?”RPC (Remote Procedure Call) is a programming paradigm that allows a program to execute a procedure (function) on another computer as if it were a local function call. The goal is to make distributed computing feel like local programming.
RPC History and Evolution
Section titled “RPC History and Evolution”1970s-1980s: RPC was first developed at Xerox PARC and later standardized by Sun Microsystems. The concept was revolutionary; it allowed programmers to call functions on remote machines without worrying about network details.
Key milestones:
- 1984: Sun RPC (ONC RPC) became widely adopted
- 1990s: CORBA (Common Object Request Broker Architecture) for object-oriented RPC
- 1990s: Microsoft DCOM (Distributed Component Object Model)
- 2000s: XML-RPC and SOAP brought RPC to the web
- 2010s: JSON-RPC and gRPC modernized the approach
How Traditional RPC Works
Section titled “How Traditional RPC Works”Client Code:result = calculateTax(income, country)
Network Layer:1. Serialize parameters (income, country)2. Send to remote server3. Server executes calculateTax() function4. Return serialized result5. Client deserializes result
The programmer writes code as if calling a local function, but the RPC framework handles all the network communication behind the scenes.
What is the Difference?
Section titled “What is the Difference?”REST and RPC represent two fundamentally different approaches to designing web APIs:
- REST (Representational State Transfer): Resource-oriented architecture
- RPC (Remote Procedure Call): Action-oriented architecture
Understanding these paradigms helps explain why REST uses different HTTP verbs instead of just POST for everything.
Core Philosophy Differences
Section titled “Core Philosophy Differences”REST: Resource-Oriented Thinking
Section titled “REST: Resource-Oriented Thinking”- Focus: What things (resources) does your system manage?
- Approach: Expose resources and let HTTP verbs define operations
- Example: “I have planets, and I want to manipulate them”
RPC: Action-Oriented Thinking
Section titled “RPC: Action-Oriented Thinking”- Focus: What actions can your system perform?
- Approach: Expose functions/procedures that can be called remotely
- Example: “I have functions that do things with planets”
URL Structure Comparison
Section titled “URL Structure Comparison”REST Approach
Section titled “REST Approach”GET /api/planets # Get all planetsGET /api/planets/123 # Get specific planetPOST /api/planets # Create new planetPUT /api/planets/123 # Update planet 123DELETE /api/planets/123 # Delete planet 123
Key characteristics:
- URLs represent resources (nouns)
- HTTP verbs indicate operations
- Same URL, different operations based on HTTP method
RPC Approach
Section titled “RPC Approach”POST /api/getAllPlanetsPOST /api/getPlanetByIdPOST /api/createPlanetPOST /api/updatePlanetPOST /api/deletePlanet
Key characteristics:
- URLs represent actions (verbs)
- All operations use POST
- Different URL for each function
Detailed API Comparison
Section titled “Detailed API Comparison”Let’s compare how both approaches handle planet management:
REST API Design
Section titled “REST API Design”Get All Planets
Section titled “Get All Planets”GET /api/planetsAccept: application/json
Response: 200 OK[ {"id": 1, "name": "Mercury", "diameter": 4879}, {"id": 2, "name": "Venus", "diameter": 12104}]
Get Single Planet
Section titled “Get Single Planet”GET /api/planets/123Accept: application/json
Response: 200 OK{"id": 123, "name": "Earth", "diameter": 12742}
Create New Planet
Section titled “Create New Planet”POST /api/planetsContent-Type: application/json
{ "name": "Kepler-442b", "diameter": 13800, "distanceFromStar": 1.2}
Response: 201 CreatedLocation: /api/planets/456
Update Planet
Section titled “Update Planet”PUT /api/planets/123Content-Type: application/json
{ "name": "Earth", "diameter": 12742, "moons": 1, "atmosphere": "nitrogen-oxygen"}
Response: 200 OK
Delete Planet
Section titled “Delete Planet”DELETE /api/planets/123
Response: 204 No Content
RPC API Design
Section titled “RPC API Design”Get All Planets
Section titled “Get All Planets”POST /api/getAllPlanetsContent-Type: application/json
{}
Response: 200 OK{ "result": [ {"id": 1, "name": "Mercury", "diameter": 4879}, {"id": 2, "name": "Venus", "diameter": 12104} ]}
Get Single Planet
Section titled “Get Single Planet”POST /api/getPlanetByIdContent-Type: application/json
{ "planetId": 123}
Response: 200 OK{ "result": {"id": 123, "name": "Earth", "diameter": 12742}}
Create New Planet
Section titled “Create New Planet”POST /api/createPlanetContent-Type: application/json
{ "name": "Kepler-442b", "diameter": 13800, "distanceFromStar": 1.2}
Response: 200 OK{ "result": {"id": 456, "message": "Planet created successfully"}}
Update Planet
Section titled “Update Planet”POST /api/updatePlanetContent-Type: application/json
{ "planetId": 123, "name": "Earth", "diameter": 12742, "moons": 1, "atmosphere": "nitrogen-oxygen"}
Response: 200 OK{ "result": {"message": "Planet updated successfully"}}
Delete Planet
Section titled “Delete Planet”POST /api/deletePlanetContent-Type: application/json
{ "planetId": 123}
Response: 200 OK{ "result": {"message": "Planet deleted successfully"}}
Key Advantages of REST
Section titled “Key Advantages of REST”1. HTTP Semantics and Caching
Section titled “1. HTTP Semantics and Caching”# REST - GET requests can be cached by browsers/CDNsGET /api/planets/123Cache-Control: max-age=3600
# RPC - POST requests are never cachedPOST /api/getPlanetById# No caching possible
2. Idempotency Guarantees
Section titled “2. Idempotency Guarantees”# REST - Safe to retry GET, PUT, DELETEGET /api/planets/123 # Always safe to callPUT /api/planets/123 # Same result if called multiple timesDELETE /api/planets/123 # Safe to retry if network fails
# RPC - No guarantees about safety of retryingPOST /api/getPlanetById # What if this has side effects?POST /api/updatePlanet # What happens if called twice?
3. Clear Resource Relationships
Section titled “3. Clear Resource Relationships”# REST - Hierarchical and intuitiveGET /api/planets/123/moons # Get moons of planet 123GET /api/planets/123/moons/456 # Get specific moonPOST /api/planets/123/moons # Add moon to planet
# RPC - Relationships less clearPOST /api/getMoonsOfPlanetPOST /api/getMoonByIdPOST /api/addMoonToPlanet
4. Standard HTTP Status Codes
Section titled “4. Standard HTTP Status Codes”# REST - Uses HTTP status codes meaningfullyGET /api/planets/999404 Not Found # Planet doesn't exist
PUT /api/planets/123422 Unprocessable Entity # Validation failed
# RPC - Everything returns 200 OKPOST /api/getPlanetById200 OK{"error": "Planet not found", "code": 404}
5. Uniform Interface
Section titled “5. Uniform Interface”# REST - Same patterns across all resourcesGET /api/planets GET /api/stars GET /api/galaxiesPOST /api/planets POST /api/stars POST /api/galaxiesPUT /api/planets/123 PUT /api/stars/456 PUT /api/galaxies/789
# RPC - Each resource may have different function namesPOST /api/getAllPlanets POST /api/listStars POST /api/fetchGalaxiesPOST /api/createPlanet POST /api/addStar POST /api/makeGalaxyPOST /api/updatePlanet POST /api/modifyStar POST /api/editGalaxy
When to Use Each Approach
Section titled “When to Use Each Approach”Choose REST When:
Section titled “Choose REST When:”- Building CRUD-heavy applications
- Need caching benefits (GET requests)
- Want standard HTTP semantics
- Building public APIs for multiple clients
- Need discoverable resources
- Want stateless communication
Examples:
- E-commerce platforms
- Content management systems
- Social media APIs
- Mobile app backends
Choose RPC When:
Section titled “Choose RPC When:”- Complex business operations that don’t map to CRUD
- Internal microservices communication
- Need high performance (binary protocols like gRPC)
- Action-oriented workflows
- Real-time operations
Examples:
calculateShippingCost()
processPayment()
sendNotification()
generateReport()
Hybrid Approaches
Section titled “Hybrid Approaches”Many real-world APIs combine both approaches:
# RESTful resource operationsGET /api/planets/123PUT /api/planets/123DELETE /api/planets/123
# RPC-style actions when CRUD doesn't fitPOST /api/planets/123/actions/calculate-orbitPOST /api/planets/123/actions/simulate-climatePOST /api/planets/123/actions/send-probe
Migration Considerations
Section titled “Migration Considerations”From RPC to REST
Section titled “From RPC to REST”Common RPC anti-patterns to avoid:
# ❌ RPC-style URLs in RESTPOST /api/createPlanetGET /api/getPlanet/123POST /api/deletePlanet
# ✅ RESTful approachPOST /api/planetsGET /api/planets/123DELETE /api/planets/123
Summary
Section titled “Summary”Aspect | REST | RPC |
---|---|---|
Philosophy | Resource-oriented | Action-oriented |
URLs | Nouns (resources) | Verbs (actions) |
HTTP Methods | Full spectrum (GET, POST, PUT, DELETE) | Mostly POST |
Caching | Excellent (GET requests) | Limited |
Idempotency | Built-in guarantees | No guarantees |
Status Codes | Semantic HTTP codes | Usually 200 OK |
Discoverability | High | Low |
Learning Curve | Medium | Low |
Best For | CRUD operations, public APIs | Complex actions, internal services |