tadata
Back to home

API Design: REST, GraphQL, gRPC, and Strategic Choices

#api#architecture#rest#graphql#grpc

APIs are the contracts between systems. The choice of API style, versioning strategy, and governance model shapes how teams collaborate and how systems evolve. This is an architectural decision, not just a technical one.

API Styles Compared

AspectRESTGraphQLgRPC
ProtocolHTTP/1.1 or HTTP/2HTTP (single endpoint)HTTP/2
Data formatJSON (typically)JSONProtobuf (binary)
SchemaOpenAPI/Swagger (optional)SDL (mandatory)Proto files (mandatory)
DiscoverabilityHATEOAS, docsIntrospection built-inProto file sharing
Over-fetchingCommon problemSolved by designField masks (optional)
Under-fetchingMultiple round tripsSingle queryDesigned per RPC
StreamingLimited (SSE, WebSocket)SubscriptionsBidirectional streaming
Browser supportNativeNativeRequires proxy (gRPC-web)
CachingHTTP caching built-inComplex (POST requests)Custom implementation
Learning curveLowMediumMedium-High

When to Use What

  • REST -- public APIs, simple CRUD, when HTTP caching matters, broad ecosystem compatibility
  • GraphQL -- client-driven queries, mobile apps with bandwidth constraints, aggregating multiple backends
  • gRPC -- internal service-to-service communication, high-performance requirements, streaming use cases

API Versioning Strategies

StrategyExampleProsCons
URL path/api/v2/usersSimple, explicitURL pollution, hard to sunset
Query parameter/api/users?version=2Easy to defaultEasy to miss
HeaderAccept: application/vnd.api.v2+jsonClean URLsHidden, harder to test
Content negotiationAccept: application/vnd.company.v2+jsonRESTfulComplex implementation
No versioning (evolution)Add fields, never removeNo version managementRequires strict discipline

Recommended approach: URL path versioning for external APIs (clarity over purity), evolution-based for internal APIs (less overhead).

API Gateway Patterns

An API gateway sits between clients and backend services:

  • Routing -- direct requests to the correct service
  • Authentication -- validate tokens before requests reach backends
  • Rate limiting -- protect backends from abuse
  • Response transformation -- aggregate or reshape responses
  • Caching -- reduce backend load for repeated queries
  • Monitoring -- centralized logging and metrics

Popular options: Kong, AWS API Gateway, GCP API Gateway, Traefik, Envoy.

API-First Design

Design the API before writing implementation code:

  1. Define the contract (OpenAPI spec, GraphQL schema, or proto file)
  2. Generate mock servers for frontend teams to start immediately
  3. Generate client SDKs automatically
  4. Implement the backend against the contract
  5. Contract tests validate compliance

Benefits: parallel development, clear contracts, auto-generated documentation.

Internal vs External APIs

AspectInternal APIsExternal APIs
AudienceYour own services/teamsThird-party developers
VersioningCan be more aggressiveMust be conservative
Breaking changesCoordinate across teamsDeprecation periods required
AuthenticationService mesh, mTLSAPI keys, OAuth 2.0
Rate limitingTrust-based, higher limitsStrict, tiered by plan
DocumentationMay be informalMust be comprehensive
SLAInternal agreementContractual obligation

Rate Limiting Strategies

  • Fixed window -- N requests per time window (simple, but allows bursts at window boundaries)
  • Sliding window -- smooths out the burst problem
  • Token bucket -- allows controlled bursts while maintaining average rate
  • Leaky bucket -- processes requests at a constant rate

Resources