REST, GraphQL, gRPC
Three approaches to building APIs-each with its own strengths.
1The Restaurant Analogy
GraphQL: Custom orders. "I want chicken, no sauce, extra veggies." You specify exactly what you want.
gRPC: Kitchen-to-kitchen communication. Fast, efficient, but not for customers directly.
2REST
REST (Representational State Transfer) uses HTTP methods (GET, POST, PUT, DELETE) on resources identified by URLs. Stateless, cacheable, widely adopted.
Example
GET /users/123 → Get user 123
POST /users → Create new user
PUT /users/123 → Update user 123
DELETE /users/123 → Delete user 123
GET /users/123/orders → Get orders for user 123Pros
- ✓Simple, well-understood
- ✓Cacheable (GET requests)
- ✓Stateless, scalable
- ✓Browser-native
Cons
- ✗Over-fetching (get more than needed)
- ✗Under-fetching (need multiple calls)
- ✗Multiple round trips
- ✗Versioning complexity
3GraphQL
GraphQL is a query language for APIs. Clients request exactly the data they need in a single request. Developed by Facebook.
Query Example
query {
user(id: "123") {
name
email
orders(last: 5) {
id
total
items {
name
quantity
}
}
}
}Pros
- ✓No over/under-fetching
- ✓Single request for complex data
- ✓Strongly typed schema
- ✓Great for mobile apps
Cons
- ✗Complex to implement
- ✗No HTTP caching
- ✗N+1 query problem
- ✗Learning curve
4gRPC
gRPC is a high-performance RPC framework using Protocol Buffers for serialization. Ideal for microservice-to-microservice communication.
Proto Definition
service UserService {
rpc GetUser(GetUserRequest) returns (User);
rpc CreateUser(CreateUserRequest) returns (User);
rpc StreamOrders(UserRequest) returns (stream Order);
}
message User {
string id = 1;
string name = 2;
string email = 3;
}Pros
- ✓10x faster than JSON
- ✓Bi-directional streaming
- ✓Strongly typed
- ✓Auto-generated clients
Cons
- ✗Not browser-native
- ✗Binary format (not human-readable)
- ✗Requires HTTP/2
- ✗Steeper learning curve
5Comparison
| Aspect | REST | GraphQL | gRPC |
|---|---|---|---|
| Format | JSON | JSON | Protobuf (binary) |
| Protocol | HTTP/1.1+ | HTTP | HTTP/2 |
| Caching | Built-in | Complex | None |
| Streaming | No | Subscriptions | Bi-directional |
| Best for | Public APIs | Mobile apps | Microservices |
6When to Use What
REST
Public APIs, simple CRUD, browser-first apps, caching important
Examples: Stripe API, GitHub API, Twitter API
GraphQL
Mobile apps needing efficiency, complex nested data, multiple clients with different needs
Examples: Facebook, Shopify, GitHub v4 API
gRPC
Microservice communication, real-time streaming, performance-critical internal services
Examples: Netflix internal, Google Cloud, Dropbox
Many companies use all three: REST for public API, GraphQL for mobile, gRPC internally.
7Key Takeaways
?Quiz
1. Mobile app needs user + posts + comments in one call. Best choice?
2. Internal microservices need maximum performance. Best choice?