SystemDesign.website
Track 5 ยท Module 5.6 ยท Communication Patterns & Asynchronous Systems

API Paradigms: REST vs GraphQL vs gRPC

How modern tech giants choose their communication protocols. Deep dive into REST resource contracts, GraphQL flexible query schemas, gRPC Protocol Buffers over HTTP/2, and API versioning.

โฑ๏ธ Reading Time: ~25 min
๐ŸŽฏ Protocols: REST (JSON/HTTP 1.1), GraphQL, gRPC (Protobuf/HTTP 2)
๐ŸŽฌ Format: Restaurant Analogy + Live Payload Benchmark Lab

๐Ÿ” 1. The Core Intuition: The Restaurant Ordering Models

๐Ÿฝ๏ธ The Analogy: Combo Meal vs Custom Salad Bowl vs Kitchen Pneumatic Tube

How you order food is identical to how client apps request data from servers:

1. REST (The Fixed Combo Meal)

You order "Combo #3". You get a burger, fries, and soda. If you only wanted the burger, you still get all 3 (Over-fetching). If you also want dessert, you must wait in line a 2nd time (Under-fetching).

๐ŸŽฏ Best for: Public third-party APIs, standard CRUD apps, simple web clients.
2. GraphQL (The Custom Salad Bowl)

You hand the waiter a checklist asking for exactly 2 tomatoes, 5 olives, and ranch. You get exactly what you asked for in 1 single roundtrip (Zero waste).

๐ŸŽฏ Best for: Mobile apps with slow 4G/5G connections, Frontend-For-Backend (BFF) layers.
3. gRPC (The High-Speed Pneumatic Tube)

The head chef and line cook communicate via pneumatic tube using compressed numerical codes (Binary Protocol Buffers over HTTP/2). Ultra fast, microscopic payload, zero human English words.

๐ŸŽฏ Best for: Internal microservice-to-microservice RPCs where microseconds matter.

โš–๏ธ 2. Architectural Comparison Matrix

FeatureREST (Representational State)GraphQLgRPC (Google RPC)
Data FormatJSON (Text / UTF-8)JSON (Text / UTF-8)Protocol Buffers (Binary)
Transport LayerHTTP/1.1 or HTTP/2HTTP/1.1 or HTTP/2 (POST)HTTP/2 (Multiplexed Streams)
Payload EfficiencyMedium (Repeated JSON keys)High (Exact requested fields)Ultra High (60-80% smaller than JSON)
Network OverheadHigh ($N+1$ request waterfall)Low (Single query aggregate)Minimal (Sub-millisecond latency)
Streaming SupportLimited (SSE, chunked)Subscriptions (via WebSocket)Native Bi-Directional Streaming
Type SafetyLoose (OpenAPI schema optional)Strict (GraphQL Schema Definition)Ultra Strict (Compiled .proto stubs)
Browser Support100% Native100% NativeRequires gRPC-Web proxy

๐Ÿ“ฆ 3. Why Protocol Buffers are 80% Smaller Than JSON

In JSON, every single HTTP response repeats key names as ASCII strings over the wire. In Protocol Buffers, field names are replaced with tiny 1-byte integer tags:

JSON Representation (48 Bytes on Wire)
{
  "user_id": 101,
  "user_name": "Alice",
  "is_active": true
}
Repeats characters "user_id", "user_name" on every request!
Protobuf Binary Representation (10 Bytes on Wire)
08 65 12 05 41 6c 69 63 65 18 01
[Tag 1: 101] [Tag 2: "Alice"] [Tag 3: true]
โšก 79% space savings: No repeated string keys, fast CPU bit-shift decoding!

๐Ÿ’ป 4. Protobuf Service Definition & Multi-Language Stubs

Define your strongly-typed API once in a user_service.proto contract, and compile client/server stubs in any language:

Select Language:
// user_service.proto (Protobuf Version 3)
syntax = "proto3";
package ecommerce.v1;

service UserService {
  // Unary RPC: Single request, single response
  rpc GetUserProfile(UserRequest) returns (UserResponse);
  
  // Server Streaming RPC: Stream live trade updates
  rpc StreamOrders(UserRequest) returns (stream OrderEvent);
}

message UserRequest {
  int64 user_id = 1; // Field tag 1 (NOT the value)
}

message UserResponse {
  int64 user_id = 1;
  string username = 2;
  string email = 3;
  bool is_premium = 4;
}

๐Ÿงช 5. Interactive Lab: Live Payload & Latency Benchmark Simulator

โšก Real-Time API Profiler

REST vs GraphQL vs gRPC Benchmark

Select an API query and compare roundtrips, payload size, and latency!
๐ŸŒ REST (HTTP/1.1 JSON)
Roundtrips: 3 HTTP Calls
Wire Payload: 4.8 KB
Total Latency: 142 ms
๐Ÿ“Š GraphQL (Single Query)
Roundtrips: 1 HTTP Call
Wire Payload: 1.1 KB
Total Latency: 48 ms
โšก gRPC (Protobuf / HTTP/2)
Roundtrips: 1 Stream
Wire Payload: 240 Bytes
Total Latency: 6 ms
๐Ÿš€ Benchmark Ready: Click any query above to compare real-time network waterfall and byte overhead!

๐ŸŽฏ 6. Test Your Understanding

Scenario 1: High-Frequency Payment Microservices

You are designing an internal communication layer between 50 microservices that process 200,000 requests/second. Latency must be under 2 milliseconds. Which API protocol is the optimal choice?

Scenario 2: Mobile App over Slow Cellular 3G

A mobile shopping app needs to fetch a user profile, recent orders, and cart items on the home screen. Over REST, this requires 3 separate roundtrips taking 800ms on 3G. What solves this?

โ—‹

Completed this module?

Mark as finished to update your curriculum progress tracker.