FullStack AI Hub
Studio
System Design · 04. Communication
10 min
10 steps
Beginner

REST, GraphQL & gRPC

Balance HTTP edge cacheability, client-driven query graphs, and binary Protobuf speed across public and internal APIs.

System Design04. CommunicationLesson 1 of 4
10 minBeginner10 steps

REST, GraphQL & gRPC

Balance HTTP edge cacheability, client-driven query graphs, and binary Protobuf speed across public and internal APIs.

CDN HIT RATE
82%
P95 LATENCY
34 ms
ORIGIN LOAD
24%
AVG PAYLOAD
4.2 KB
GET /products/4218% miss12k clientsHTTP CDN28%82% GET hitREST API24%JSONCatalog DB20%CACHE-CONTROL: MAX-AGE=60Cache KeyHTTP GET URLOrigin CPU0% on edge hits
REST / GraphQL call
CDN / batch hit
large JSON / N+1 burst
gRPC binary frame
timeout / overload
In This LessonStep 1 of 10
What You Will Master
  • ✓Leverage REST resource URLs and HTTP GET semantics for effortless CDN caching
  • ✓Eliminate mobile waterfall round-trips with GraphQL while guarding against resolver N+1 storms
  • ✓Cut internal microservice latency and CPU serialization overhead using gRPC, Protobuf, and HTTP/2
  • ✓Combine REST/GraphQL at the public edge with gRPC inside the private service mesh

Architectural Trade-Off Comparison Matrix

PatternBest ForKey Trade-Off
REST (HTTP/JSON)Public developer APIs, CRUD resources, and read-heavy endpoints that benefit from CDN HTTP GET cachingFixed response shapes cause overfetching or multi-request waterfalls on mobile screens
GraphQL (Query Graph + DataLoader)Mobile & web BFF layers stitching multiple domain services into one tailored client queryPOST-by-default breaks CDN caching unless using Persisted Queries; naïve resolvers trigger N+1 DB storms
gRPC (HTTP/2 + Protobuf)Low-latency internal service-to-service RPC, strict typed contracts, and bidirectional streamingBinary payloads are not human-readable in curl and browsers need a gRPC-Web proxy
Edge-to-Core Protocol StackProduction architectures: REST/GraphQL facing browsers & mobile, gRPC between internal microservicesRequires schema governance and gateway translation between external JSON and internal Protobuf

Step-by-Step Architectural Breakdown (10 Steps)

Full architectural walkthrough transcript and state transitions for REST, GraphQL & gRPC.

STEP 01 · SETUPJump to step →

Start with REST: URLs that caches understand

In **REST**, every entity has a predictable resource URL like `GET /v1/products/42` and standard HTTP verbs (`GET`, `POST`, `PUT`, `DELETE`).

Because `GET` requests are safe and idempotent, CDNs and browser caches understand them out of the box—absorbing 82% of catalog reads before traffic ever touches your origin.

idea:REST’s superpower isn’t JSON—it’s native alignment with decades of HTTP caching, status codes, and tooling.

REST ResourcesHTTP GET CachingIdempotency
STEP 02 · PROBLEMJump to step →

The mobile screen problem: overfetching & waterfalls

Now open a single mobile Product Detail screen. It needs the **product title**, **seller rating**, and **top 3 reviews**—three different REST resources.

The phone fires **4 sequential round-trips** over a 70 ms cellular link and downloads **84 KB** of unused JSON fields (shipping tables, audit history, internal flags) just to render 1.5 KB of text.

watch:On high-latency mobile networks, 4 chained REST calls multiply RTT into a 460 ms blank screen—and burn user data on fields the UI discards.

OverfetchingUnderfetchingMobile Waterfall
STEP 03 · SOLUTIONJump to step →

GraphQL BFF: one round-trip, exact fields

Place a **GraphQL Backend-for-Frontend (BFF)** inside the data center. The mobile app sends **one typed query** listing only the 6 fields it actually renders.

The GraphQL server fans out in parallel over sub-millisecond datacenter LAN links, strips unused attributes, and returns a crisp **1.8 KB** response in a single cellular round-trip.

idea:GraphQL moves the multi-service fan-out from a slow 70 ms cellular link onto a 0.5 ms internal datacenter network.

GraphQL SchemaDeclarative FetchingBFF Pattern
STEP 04 · TRADE-OFFJump to step →

GraphQL’s hidden trap: N+1 resolvers & lost CDN cache

A week later, a frontend engineer fetches a list of **50 products** with each product’s `seller`. Without batching, the `seller` resolver fires **50 separate SQL queries**—turning 1 request into **51 database round-trips**.

Worse, because GraphQL sends queries inside `POST /graphql` bodies by default, standard HTTP CDNs cannot cache reads—slamming the origin database on every page load.

watch:GraphQL makes it effortless for clients to accidentally author exponential N+1 queries and bypass edge HTTP caches.

N+1 Query ProblemPOST Cache BypassQuery Complexity
STEP 05 · TECHNIQUEJump to step →

Fix GraphQL: DataLoader batching + Persisted GETs

Add **DataLoader** inside the GraphQL layer: it collects all 50 `sellerId` lookups during a single event-loop tick, deduplicates them, and runs **one** `WHERE id IN (...)` query.

Pair it with **Automatic Persisted Queries (APQ)** so clients send a short SHA-256 query hash over `GET /graphql?hash=9f8a…`—restoring full CDN edge caching.

idea:Production GraphQL always ships with three guardrails: DataLoader batching, Persisted GET queries for CDN caching, and max query depth limits.

DataLoader BatchingPersisted Queries (APQ)Depth Limiting
STEP 06 · PROBLEMJump to step →

Inside the mesh: the hidden tax of JSON over HTTP/1.1

Zoom behind the public gateway into your internal microservices. Checkout fans out to **Pricing**, **Inventory**, and **Fraud** thousands of times per second.

Speaking text-based **JSON over HTTP/1.1** repeats long key names (`"unit_price_cents":`) in every message, burns **35–45% of CPU** on string parsing, and drifts whenever a team renames a field silently.

watch:JSON is great for human debugging at the edge—but across 15,000 internal RPCs/sec, string parsing becomes your #1 compute bill.

Serialization OverheadHead-of-Line BlockingSchema Drift
STEP 07 · SOLUTIONJump to step →

Switch internal calls to gRPC: Protobuf over HTTP/2

Replace internal JSON calls with **gRPC**. Services share a strongly typed `.proto` contract that generates Go, Java, Python, and TypeScript clients automatically.

On the wire, **Protocol Buffers** encode fields as compact numeric tags (`1=1999`)—shrinking payloads **5–7×**—while **HTTP/2 multiplexing** streams hundreds of concurrent RPCs over a single persistent TCP connection.

idea:Because `.proto` numbers field tags instead of repeating string keys, Protobuf deserializes up to 8× faster than JSON with strict compile-time safety.

Protocol BuffersHTTP/2 MultiplexingCode Generation
STEP 08 · TECHNIQUEJump to step →

Beyond request-response: gRPC streaming & deadlines

Because gRPC runs on HTTP/2 frames, it natively supports **four communication modes**: Unary (1→1), Server Streaming (1→many), Client Streaming (many→1), and **Bidirectional Streaming** (many↔many).

It also propagates a **Deadline** header across every hop—so if the caller gave up after 200 ms, downstream services automatically cancel wasted work instead of piling up zombie queries.

idea:Deadline propagation prevents cascading microservice pileups: when an upstream request times out, the entire downstream call tree cancels immediately.

Bidirectional StreamingDeadline PropagationFlow Control
STEP 09 · SOLUTIONJump to step →

Production blueprint: REST/GraphQL at the edge, gRPC inside

Top engineering teams don’t pick one protocol religion—they put each protocol where it excels.

**Public developers** get cacheable **REST** endpoints, **mobile/web UIs** talk to a **GraphQL BFF** with DataLoader & APQ, and every hop behind the gateway speaks **gRPC + Protobuf** inside the service mesh.

idea:Rule of thumb: External APIs optimize for compatibility and CDN caching (REST/GraphQL); internal APIs optimize for speed and typed contracts (gRPC).

API Gateway TranslationEdge vs Core ProtocolsContract Governance
STEP 10 · RECAPJump to step →

Recap: choose the protocol that fits the boundary

There is no single "best" API protocol—each optimizes a different boundary of your system.

Use **REST** for public APIs and CDN-cached resources, **GraphQL** (with DataLoader + APQ) when mobile/web screens aggregate many entities, and **gRPC** for high-throughput, typed internal service-to-service meshes.

idea:Interview cheat code: say "REST or GraphQL at the client boundary for compatibility and aggregation, translated to gRPC over HTTP/2 inside the VPC."

RESTGraphQLgRPCL7 Load Balancing

Related Production Deep-Dives & Simulators

Related Systems Course
Free · Verifiable Credential

FS-301Agentic Systems Engineer

Engineer bounded tool-using systems with explicit workflows state protocols evaluation observability security and human control.

27 concepts~29h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.