REST vs GraphQL vs gRPC
While REST is the most popular API architecture, it's not the only option. Let's compare REST with two major alternatives: GraphQL and gRPC.
Overview Comparison
| Feature | REST | GraphQL | gRPC |
|---|---|---|---|
| Protocol | HTTP | HTTP | HTTP/2 |
| Data Format | JSON/XML | JSON | Protocol Buffers |
| Typing | Optional | Strongly Typed | Strongly Typed |
| Best For | Simple CRUD | Complex queries | Microservices |
REST
REST uses standard HTTP methods and multiple endpoints to expose resources.
Example Request:
GET /api/users/123
Example Response:
{
"id": 123,
"name": "John Doe",
"email": "john@example.com",
"posts": [
{ "id": 1, "title": "First Post" },
{ "id": 2, "title": "Second Post" }
]
}
Pros:
- Simple and well-understood
- Excellent caching support
- Works with any HTTP client
- Stateless by design
Cons:
- Over-fetching (getting more data than needed)
- Under-fetching (needing multiple requests)
- No built-in type system
GraphQL
GraphQL uses a single endpoint and lets clients specify exactly what data they need.
Example Request:
query {
user(id: 123) {
name
email
posts(limit: 5) {
title
}
}
}
Example Response:
{
"data": {
"user": {
"name": "John Doe",
"email": "john@example.com",
"posts": [
{ "title": "First Post" }
]
}
}
}
Pros:
- Fetch exactly what you need
- Single request for related data
- Strongly typed schema
- Self-documenting
Cons:
- Caching is more complex
- Can be overkill for simple APIs
- Potential for expensive queries
- Learning curve
gRPC
gRPC uses Protocol Buffers for efficient binary serialization and HTTP/2 for transport.
Example Definition (.proto file):
service UserService {
rpc GetUser(GetUserRequest) returns (User) {}
}
message GetUserRequest {
int32 id = 1;
}
message User {
int32 id = 1;
string name = 2;
string email = 3;
}
Pros:
- Very fast (binary serialization)
- Strongly typed contracts
- Bi-directional streaming
- Code generation for multiple languages
Cons:
- Not browser-friendly
- Less human-readable
- More complex setup
- Harder to debug
When to Use Each
Choose REST When:
- Building public APIs
- You need wide client compatibility
- Caching is important
- You have simple CRUD operations
Choose GraphQL When:
- Clients need flexible data fetching
- You have complex, interconnected data
- Mobile apps need to minimize bandwidth
- You have multiple clients with different needs
Choose gRPC When:
- Building internal microservices
- Performance is critical
- You need bi-directional streaming
- You're in a polyglot environment
Exercise: Choose the Right API Style
Hybrid Approaches
Many organizations use multiple API styles:
- REST for public-facing APIs
- GraphQL for mobile/web applications
- gRPC for internal service communication
┌─────────────────────────────────────────────┐
│ API Gateway │
├─────────┬─────────────┬─────────────────────┤
│ REST │ GraphQL │ (gRPC internal) │
│ Public │ Mobile/Web │ Microservices │
└─────────┴─────────────┴─────────────────────┘
Summary
Each API style has its strengths:
- REST: Simple, cacheable, universally understood
- GraphQL: Flexible queries, reduced over-fetching
- gRPC: High performance, streaming, strong typing
For this course, we'll focus on REST because it's the foundation of web APIs and the most widely used approach. The principles you learn here will also help you understand when and why you might choose an alternative.

