API Protocols — HTTP, gRPC, and GraphQL Compared
In this tutorial, you will learn about API Protocols. We cover key concepts, practical examples, and best practices to help you master this topic.
API protocols are the rules and formats that govern communication between services, with HTTP powering REST and SOAP, gRPC enabling high-performance RPC, and GraphQL providing flexible client-driven queries.
What You'll Learn
- The strengths and weaknesses of each major API protocol
- When to choose HTTP/REST vs gRPC vs GraphQL
- How protocol choice affects performance and developer experience
Why It Matters
Choosing the wrong protocol leads to over-engineered systems or performance bottlenecks. DodaTech's Durga Antivirus Pro uses REST for public APIs, gRPC for internal Microservices, and plans GraphQL for the dashboard.
flowchart TD
A["API Protocols"] --> B["HTTP/REST"]
A --> C["gRPC"]
A --> D["GraphQL"]
B --> E["Readable, cacheable,\nstandard status codes"]
C --> F["High performance,\nstrongly typed, streaming"]
D --> G["Flexible queries,\nsingle endpoint, real-time"]
style A fill:#dbeafe,stroke:#2563eb
Code Examples
# HTTP/REST
import requests
response = requests.get("https://api.example.com/v1/users")
print(response.json())
// GraphQL
const query = `
query {
users {
id
name
email
}
}
`;
fetch('https://api.example.com/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({query})
})
.then(res => res.json())
.then(data => console.log(data));
# gRPC using grpcurl
grpcurl -d '{"user_id": 1}' \
-H "Authorization: Bearer token" \
api.example.com:443 UserService/GetUser
Common Mistakes
1. Using REST for High-Throughput Internal Calls
gRPC is 5-10x faster for internal microservice communication.
2. Using GraphQL Without Caching
GraphQL responses are hard to cache because queries vary per client.
3. Choosing gRPC for Public APIs
gRPC requires HTTP/2 and protobuf, which browser support is limited.
4. Assuming REST is Always the Best Choice
Each protocol has trade-offs. Pick based on your use case.
5. Ignoring Protocol Limitations
Websocket doesn't do request-response; gRPC doesn't do browser clients easily.
Practice Questions
- What protocol does REST typically use?
- Why is gRPC faster than REST for internal services?
- What problem does GraphQL solve that REST doesn't?
- Can you use HTTP/2 with REST?
- Which protocol is best for real-time bidirectional communication?
Answers:
- HTTP/HTTPS.
- gRPC uses HTTP/2, binary encoding (protobuf), and persistent connections.
- GraphQL lets clients request only the fields they need, preventing over-fetching.
- Yes, REST works over any HTTP version, but HTTP/2 improves performance.
- WebSocket for real-time; gRPC streaming for high-throughput server-to-server.
Challenge: Design a system where REST handles public CRUD endpoints, gRPC handles internal microservice communication, and GraphQL powers a dashboard UI. Explain the data flow.
FAQ
What's Next
Dive into REST Introduction to learn the most popular API protocol in depth, or explore SOAP Protocol for enterprise patterns.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro