API Patterns
REST, GraphQL, gRPC comparison and best practices
Choosing the right API pattern impacts performance, developer experience, and system evolution. REST provides simplicity and caching, GraphQL offers flexibility and efficiency, while gRPC delivers speed and type safety. Each has its sweet spot in modern architectures.
The practical approach: use REST for public APIs, GraphQL for complex UIs, gRPC for microservices. REST's HTTP semantics work everywhere, GraphQL solves over/under-fetching, and gRPC's binary protocol and streaming excel in service-to-service communication.
Different API patterns serve different needs. Choose based on your use case, team expertise, and performance requirements.
REST
Resource-oriented HTTP APIs
- •Simple & widely understood
- •Stateless
- •Great tooling
- •Cacheable
- •Over-fetching
- •Multiple round trips
- •Versioning challenges
CRUD operations, public APIs, simple data models
GraphQL
Query language with single endpoint
- •Precise data fetching
- •Strong typing
- •Single request
- •Real-time subscriptions
- •Complexity
- •Caching challenges
- •Query depth attacks
Complex data relationships, mobile apps, rapid frontend development
gRPC
High-performance RPC framework
- •High performance
- •Strong contracts
- •Bi-directional streaming
- •Multi-language
- •Browser limitations
- •Less human-readable
- •Learning curve
Microservices, real-time systems, internal APIs
Security vs complexity trade-offs for different authentication approaches.
Header: Authorization: Bearer <api-key>
Authorization code flow with PKCE
Header: Authorization: Bearer <jwt-token>
RESTful Resource Design
Use HTTP verbs and resource-based URLs for clarity and consistency
- GET /users/123
- POST /users
- PUT /users/123
- DELETE /users/123
- GET /getUser?id=123
- POST /createUser
- POST /updateUser
- POST /deleteUser
Error Handling
Proper HTTP status codes and detailed errors improve developer experience
- HTTP 400 with error details
- Consistent error format
- Helpful error messages
- HTTP 200 with error in body
- Generic "error occurred"
- Inconsistent formats
Pagination
Efficient pagination prevents performance issues and improves UX
- Cursor-based pagination
- Limit/offset with metadata
- Link headers
- Return all results
- Page numbers only
- No total count
Stripe (REST)
Why: Payment APIs need to be simple, reliable, and widely compatible
GitHub (GraphQL + REST)
Why: Complex relationships between repos, users, issues need flexible querying
Google Services (gRPC)
Why: Internal microservices need high performance and strong contracts
Netflix (GraphQL Federation)
Why: 100+ microservices need unified API for mobile apps
📝 Test Your Knowledge
6 questions • Progress: 0/6