Module 8 - Networking and APIs

REST, GraphQL, gRPC

Three approaches to building APIs-each with its own strengths.

1The Restaurant Analogy

Simple Analogy
REST: A fixed menu. Order dish #5, get dish #5. Simple, predictable.
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 123
Pros
  • 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

AspectRESTGraphQLgRPC
FormatJSONJSONProtobuf (binary)
ProtocolHTTP/1.1+HTTPHTTP/2
CachingBuilt-inComplexNone
StreamingNoSubscriptionsBi-directional
Best forPublic APIsMobile appsMicroservices

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

1REST: Simple, cacheable, widely adopted. Best for public APIs
2GraphQL: Request exactly what you need. Best for mobile/complex UIs
3gRPC: Fast binary protocol with streaming. Best for internal services
4Use REST when caching matters, GraphQL when flexibility matters, gRPC when speed matters
5Many companies use multiple protocols for different use cases

?Quiz

1. Mobile app needs user + posts + comments in one call. Best choice?

2. Internal microservices need maximum performance. Best choice?