Skip to main content

REST, gRPC, and GraphQL

1. Executive Summary​

REST (Representational State Transfer over HTTP) models resources with verbs and status codes—ubiquitous, cache-friendly, and human-debuggable. gRPC uses Protocol Buffers over HTTP/2 for strongly typed, bidirectional streaming RPC with efficient binary serialization—dominant for internal service-to-service calls. GraphQL provides a schema-driven query language where clients request exactly the fields they need—excellent for aggregating multiple backends behind a single API for diverse clients.

No single style wins everywhere. Principal architects choose based on client diversity, latency budgets, evolution constraints, caching needs, and team skill. Hybrid architectures are normal: REST/GraphQL at the edge, gRPC east-west, events for async integration.

This chapter compares mechanisms, guarantees, failure modes, performance, security, versioning implications, and interview-level API design reasoning.

2. Why This Topic Matters​

API style questions separate architects who understand tradeoffs from those who advocate one technology:

  • When is GraphQL's flexibility a performance liability (N+1 queries)?
  • How do gRPC deadlines propagate vs REST timeouts?
  • REST idempotency with PUT vs POST vs PATCH.
  • Protobuf backward compatibility rules for rolling deploys.
  • BFF pattern vs GraphQL vs REST aggregation.

Interviewers probe system design with "design the public API" and "how do mobile and web share backends."

3. Problems Being Solved​

ProblemAPI style fit
Public HTTP integrationREST (OpenAPI), widest tooling
Low-latency internal RPCgRPC + protobuf
Mobile clients with varied data needsGraphQL or BFF
Streaming / long-lived connectionsgRPC streaming, WebSockets
Strong contracts + codegengRPC, GraphQL schema
CDN/HTTP caching of readsREST with cache headers
Polyglot microservicesgRPC with generated stubs

None eliminates need for authentication, rate limiting, versioning, and observability.

4. Assumptions and System Model​

AssumptionImplication
Clients have different needsOne API rarely fits all without aggregation layer
Network is unreliableDeadlines, timeouts, idempotency required
Schemas evolveBackward-compatible changes only without coordination
Browser clientsgRPC requires grpc-web proxy; REST/GraphQL native
Not all operations are CRUDRPC models commands explicitly

Integration topology: External clients → API gateway / GraphQL gateway → REST or gRPC microservices → databases/events.

5. Essential Terminology​

TermDefinition
RESTArchitectural style: resources, HTTP methods, stateless
OpenAPIMachine-readable REST API specification
gRPCRPC framework using protobuf over HTTP/2
ProtobufBinary serialization with schema evolution rules
GraphQLQuery language + runtime executing against schema
ResolverGraphQL function fetching field data
BFFBackend-for-Frontend tailored API per client type
HTTP/2Multiplexed streams; header compression
DeadlinegRPC propagated timeout budget
N+1 problemGraphQL resolvers causing one query per child entity

6. Core Mechanism​

Style comparison architecture​

Figure 1: Hybrid topology—GraphQL for flexible clients, REST for partners, gRPC internally.

REST request lifecycle​

Figure 2: REST leverages HTTP semantics—caching, status codes, standard verbs.

gRPC call with deadline​

Figure 3: gRPC propagates cancellation—child calls must respect parent deadline.

GraphQL query execution​

Client sends single query; server resolves fields—risk of N+1 without DataLoader batching:

query {
user(id: "1") {
name
orders { id total }
}
}

Resolver for orders may fan out to order-service per user—batch loaders collapse to one RPC.

7. Step-by-Step Walkthrough​

Scenario: Product page needs user, recommendations, and inventory.

ApproachFlowTradeoff
REST (multiple calls)Client calls 3 endpointsSimple; chatty on mobile
BFFOne REST /product-page aggregatesPer-client duplication
GraphQLOne query, gateway resolves fieldsN+1 risk; query complexity limits
gRPC (internal only)BFF calls 3 gRPC services in parallelNot browser-native

Recommended hybrid: GraphQL or BFF at edge; gRPC between services with parallel calls and shared deadline.

Protobuf evolution example:

ChangeSafe?
Add optional fieldYes (backward compatible)
Remove field (reserve number)Yes with reservation
Change field typeNo
Rename fieldNo (use new field)

API gateway responsibilities vs service responsibilities:

ConcernAPI GatewayIndividual Service
Authentication (OAuth/OIDC)Validate JWT, pass claimsAuthorize by claim
Rate limitingPer-client/tenantPer-resource fine-grained
TLS terminationNorth-southOptional east-west (mesh)
Request routingPath-based to serviceBusiness logic routing
Protocol translationREST ↔ gRPCNative protocol
CachingPublic GET responsesDomain-specific cache

Duplicating auth logic in every service is an anti-pattern—centralize at gateway, enforce authorization locally.

gRPC streaming patterns:

PatternDirectionUse case
UnarySingle req/resStandard RPC
Server streamingOne req, many resLarge result sets, live feed
Client streamingMany req, one resBatch upload
BidirectionalBothReal-time collaboration

Streaming requires flow control and deadline discipline—unbounded streams exhaust memory.

8. Invariants and Guarantees​

PropertyRESTgRPCGraphQL
ContractOpenAPI (optional).proto requiredSchema required
CachingHTTP nativeLimitedQuery-level complexity
StreamingSSE, chunkedFirst-classSubscriptions
Type safetyLoose (JSON)StrongStrong (schema)
Browser supportNativegrpc-web neededNative POST

9. Failure Scenarios​

Scenario 1: GraphQL N+1​

Setup: Resolver per order line hits DB each time.

Effect: Latency explosion; DB overload.

Mitigation: DataLoader batching; field-level complexity limits; persisted queries.

Scenario 2: gRPC without deadline​

Setup: Slow downstream holds threads.

Effect: Cascading latency (see resilience patterns).

Mitigation: Default deadlines; context propagation.

Scenario 3: REST over-fetching​

Setup: Mobile downloads full order object with 50 fields.

Effect: Bandwidth and battery waste.

Mitigation: Sparse fieldsets, GraphQL, or dedicated mobile endpoints.

Scenario 4: Breaking protobuf change​

Setup: Field renumbering deployed before consumers update.

Effect: Deserialization errors; production outage.

Mitigation: Buf breaking change detection in CI; field reservation.

Scenario 5: GraphQL query bomb​

Setup: Deep nested query user { friends { friends { ... }}}.

Effect: DoS via expensive resolution.

Mitigation: Depth/complexity limits; query cost analysis; persisted queries for production.

Scenario 6: gRPC message size limit exceeded​

Setup: Service returns 50MB protobuf payload; default 4MB limit.

Effect: RESOURCE_EXHAUSTED errors; client retries amplify load.

Mitigation: Pagination; streaming for large datasets; raise limits coherently client and server; reference storage for blobs.

10. Performance Characteristics​

StyleSerializationTypical use
REST JSONHuman-readable; largerPublic APIs, caching
gRPC protobufCompact; fastInternal high-QPS
GraphQLJSON response; variableFlexible client queries

HTTP/2 multiplexing benefits gRPC and modern REST servers. Verify with benchmarks—JSON with compression narrows gap for some payloads.

11. Scalability Limits​

  • GraphQL gateway CPU scales with query complexity—need horizontal scaling and caching.
  • gRPC connection pools per client instance—watch file descriptors.
  • REST caching reduces origin load but complicates invalidation.
  • OpenAPI spec size and codegen time grow with API surface.

GraphQL gateways at scale often require federation sharding—no single node can hold the entire composed schema in memory for very large organizations. Plan horizontal gateway scaling and subgraph ownership boundaries early.

gRPC load balancing note: Client-side load balancing with DNS requires headless Services or xDS-based resolution—naive DNS round-robin breaks on connection reuse with HTTP/2.

Principal interviews reward naming specific failure modes—N+1, message size limits, connection reuse—not just API style preferences.

12. Operational Considerations​

  • Contract testing for all styles (Pact, Buf breaking checks).
  • API gateways for auth, rate limit, transcoding REST↔gRPC.
  • Schema registry for protobuf and GraphQL federation governance.
  • Documentation: OpenAPI UI, gRPC reflection, GraphQL playground (dev only).
  • Deprecation headers and sunset dates for REST; package versioning for gRPC.

API platform operational checklist:

ItemRESTgRPCGraphQL
Contract in CIopenapi-diffBuf breakingschema check
Rate limit configGateway per routePer methodPer operation + depth
DashboardLatency by routeLatency by methodComplexity score
Error budgetPer API productPer servicePer gateway
On-call runbook4xx vs 5xx triageStatus code mappingQuery timeout kills

Correlation ID propagation: All styles must forward traceparent or X-Request-ID across sync and async boundaries—API gateway generates if missing; reject internal calls without correlation in staging to build habit.

13. Security Considerations​

  • OAuth2/OIDC at edge for all public styles.
  • gRPC requires TLS in production; consider mTLS internal.
  • GraphQL introspection disabled in production.
  • Input validation at boundary—GraphQL types are not sufficient alone.
  • Rate limiting per client and per operation (GraphQL operation name).

14. Cost Considerations​

  • GraphQL gateway compute for complex queries—may exceed simple REST LB cost.
  • gRPC reduces egress bytes east-west—savings at high volume.
  • Multiple API styles increase platform team maintenance—consolidate where possible.
  • API gateway managed services (Apigee, Kong Cloud) add OpEx.

15. Production Implementations​

Google​

Internal Stubby/gRPC origin; public APIs mix REST (Google Cloud JSON) and gRPC.

Netflix​

Falcor (GraphQL-like) historically; extensive REST and custom protocols—evolution over time.

Shopify​

Storefront API GraphQL for merchant flexibility; admin REST legacy coexistence.

Uber​

Protobuf/gRPC extensively internal; edge APIs documented for partners.

Industry adoption patterns (architectural observations):

Company patternEdge APIInternalAsync
Large SaaSREST OpenAPI + OAuthgRPCKafka events
Social/mobileGraphQLgRPC/ThriftCustom pipelines
FinancialREST with strict versioninggRPC mTLSMQ with schema registry
StartupsREST or BFFREST (early) → gRPC (scale)Webhooks

Connect-RPC (Buf) deserves evaluation in 2025+ architectures: gRPC-compatible over HTTP/1.1 and HTTP/2 with simpler load balancer compatibility than native gRPC—useful when infrastructure teams resist gRPC ingress complexity. Verify current Buf/Connect maturity for your language targets.

GraphQL federation at scale: Apollo Federation and GraphQL Mesh enable team-owned subgraphs composed at gateway—aligns with bounded context ownership. Operational cost includes schema composition CI, subgraph deployment coordination, and query plan monitoring.

16. Alternatives and Tradeoffs​

When to chooseStyle
Public partners, caching, simplicityREST + OpenAPI
Internal microservices, streaminggRPC
Many client types, field flexibilityGraphQL + federation
Single mobile appBFF may suffice over GraphQL
Event-driven readsAsync APIs + materialized views

Anti-pattern: GraphQL everywhere including simple CRUD internal services—unnecessary complexity.

17. Common Misconceptions​

MisconceptionReality
"gRPC replaces REST entirely"Edge often stays REST/HTTP for compatibility
"GraphQL = no over-fetching"Server still fetches; N+1 can worsen
"REST cannot perform"HTTP/2 + good design scales widely
"Protobuf = versioning solved"Discipline still required
"One API style for whole company"Hybrid is normal and healthy

18. Principal Architect Perspective​

  1. Edge vs internal separation—optimize each layer independently.
  2. Schema-first development with CI breaking checks for gRPC and GraphQL.
  3. Idempotency keys on REST POST and gRPC idempotent RPCs.
  4. BFF when client count is small; GraphQL when many diverse clients share gateway.
  5. Document decision criteria in ADR—avoid religious wars between teams.

Interview API design checklist (principal-level):

When designing APIs in system design interviews, explicitly address:

  1. Client types (mobile, partner, internal) → style per layer
  2. Read vs write ratio → caching, CQRS consideration
  3. Consistency requirements → sync vs async integration
  4. Versioning strategy → additive default, major version triggers
  5. Auth model → OAuth scopes, mTLS internal
  6. Rate limits and quotas → per tenant fairness
  7. Idempotency → keys on mutations
  8. Observability → correlation IDs, OpenTelemetry propagation

Skipping any of these signals incomplete principal thinking.

Whiteboard tip: Draw three boxes—Client, Edge (REST/GraphQL), Internal (gRPC)—before debating technology religion. Interviewers reward layered thinking over single-style advocacy.

19. Architecture Review Exercise​

Scenario: GraphQL gateway calls 15 microservices synchronously per homepage query; no complexity limits; p99 4s.

Review prompts:

  1. N+1 and fan-out analysis?
  2. Caching strategy?
  3. Alternative architectures?

Expected findings: DataLoader, persisted queries, complexity limits, CDN for static fields, parallel gRPC with deadline, consider BFF caching layer.

20. Whiteboard Explanation​

90-second version:

"REST models resources over HTTP—great for public APIs, caching, and tooling. gRPC uses protobuf on HTTP/2—typed contracts, efficient, streaming, ideal internal east-west with deadlines. GraphQL lets clients specify fields in one query—powerful for mobile/web diversity but risks N+1 without batching and query cost limits. I typically expose REST or GraphQL at the edge, gRPC inside. Protobuf evolves with additive fields only. Auth and rate limiting sit at gateway. Choose REST for partner simplicity, gRPC for service performance, GraphQL when client data needs diverge sharply—not one size fits all."

Extended principal addendum: Always discuss operational ownership—who maintains the GraphQL gateway schema, who owns OpenAPI specs, who runs Buf breaking checks. API style without governance becomes integration debt within 18 months at scale.

21. Interview Questions​

  1. REST vs RPC philosophical difference?

    • Signals: Resources/verbs vs procedures; hypermedia optional.
  2. gRPC advantages internal?

    • Signals: Protobuf, HTTP/2, streaming, codegen, deadlines.
  3. GraphQL N+1 mitigation?

    • Signals: DataLoader, batching, complexity limits.
  4. Protobuf safe evolution?

    • Signals: Add optional fields; reserve numbers; no type change.
  5. When REST over gRPC public?

    • Signals: Browser, caching, ecosystem, debuggability.
  6. BFF vs GraphQL?

    • Signals: BFF per client type; GraphQL shared schema flexibility.
  7. gRPC in browser?

    • Signals: grpc-web + proxy; not native.
  8. Idempotent HTTP methods?

    • Signals: GET, PUT, DELETE; POST not by default.
  9. GraphQL security concerns?

    • Signals: Depth limits, introspection off, auth per field.
  10. HTTP/2 benefit for APIs?

    • Signals: Multiplexing, header compression, single connection.
  11. Design order API—styles?

    • Signals: REST public, gRPC internal, events for notifications.
  12. Streaming use case gRPC vs REST?

    • Signals: Bidirectional streams, live updates; SSE for simpler one-way.
  13. HTTP/3 consideration?

    • Signals: QUIC over UDP; reduced head-of-line blocking; adoption maturity.
  14. API composition vs GraphQL?

    • Signals: BFF aggregates known views; GraphQL flexible client queries.
  15. Protobuf unknown field handling?

    • Signals: Preserved for forward compatibility; parsers must not reject.

Scoring rubric:

DimensionStrongWeak
TradeoffsPer-layer style choiceOne style evangelism
PerformanceN+1, protobuf, caching"gRPC is faster" only
EvolutionSchema compatibilityIgnore versioning

22. Interview Follow-Ups​

  1. GraphQL federation vs monolith schema?

    • Signals: Subgraphs per team; gateway composition.
  2. REST HATEOAS in practice?

    • Signals: Rare full HATEOAS; pragmatic REST common.
  3. Connect-RPC / Twirp alternatives?

    • Signals: gRPC-compatible HTTP variants; simpler gateways.
  4. API gateway rate limit strategy?

    • Signals: Per API key, per tenant, sliding window; 429 with Retry-After.
  5. Design idempotency for distributed checkout?

    • Signals: Idempotency-Key header; server store with TTL; return same response on duplicate.

22A. Production Low-Level Design (Interview + Game Day)​

Hybrid API platform: REST for partners, gRPC east-west, GraphQL for mobile BFF. Production LLD and Game Day Standard.

Control vs data plane​

PlaneComponents
ControlAPI gateway (auth, rate limits, transcoding), Buf/schema CI, OpenAPI registry
DatagRPC services (protobuf), REST edge, GraphQL gateway + resolvers

Step-by-step flow:

StepActionFailure / idempotency
1Partner POST with Idempotency-KeyGateway dedupes; 409 if payload mismatch
2REST→gRPC transcoding at gatewayProtobuf validation rejects bad types
3Order service persists + emits eventgRPC ALREADY_EXISTS mapped to 409
4Retry returns cached 201 bodyTTL 24h on idempotency store
5GraphQL complexity checkReject depth > 10; 429 on abuse
6DataLoader batches N+1 resolversSingle gRPC BatchGetLines

API contract (illustrative)​

RPC / RESTMethodError codes
CreateOrder / POST /v1/ordersPOST201, 400, 409, 429, 503
GetOrder / GET /v1/orders/{id}GET200, 404, 429
GraphQL order(id)query200 + errors[]; 400 complexity

Protobuf rule: add optional fields only; Buf breaking check in CI.

One SLO​

Partner REST availability: 99.95% successful non-4xx responses; p99 gateway latency < 150ms excluding backend; alert on gRPC deadline exceeded > 1%.


22B. Game Day Playbooks​

#DrillInjectDetectMitigateVerify
GD-1gRPC backend overloadLatency injection on Order servicep99 > 500ms; deadline exceeded metricGateway circuit breaker; shed GraphQLPartner 503 with Retry-After; mobile degraded gracefully
GD-2Duplicate checkout stormReplay same Idempotency-Key 1k/secIdempotency store hot partitionRedis shard idempotency keys; coalesceSingle order created; 201 stable
GD-3GraphQL complexity attackDeep nested queryComplexity score > limitBlock query; rate limit clientLegitimate queries unaffected

GD-1 — Backend overload cascades to clients​

23. Strong Answer Example​

Question: "Public mobile app + partner integrations—API strategy?"

Answer this as a 45-minute greenfield design using the PRACTICE framework. Below is how a strong student sounds aloud—right style per client, not one-size-fits-all GraphQL.


Student interview script (PRACTICE)​

P — Problem framing​

"We have three client classes: mobile app (dynamic UI), partner integrations (stable contracts, onboarding), and internal microservices (high throughput). The mistake is picking one API style globally—I'll design a hybrid platform with a gateway as the policy edge."

R — Requirements​

TypeRequirement
FRPartners create/read orders via REST; mobile aggregates order + lines; internal inventory is gRPC
NFRPartner p99 < 200ms; mobile GraphQL complexity bounded; internal protobuf backward compatible
SafetyOAuth for partners; mTLS east-west; Idempotency-Key on partner POSTs
OpsOpenAPI published; Buf CI; versioned deprecation headers

A — Assumptions​

AssumptionImplication
Partners need OpenAPI + API keysREST at edge, not raw gRPC
Mobile team owns query shapesGraphQL BFF with persisted queries in prod
5 internal services consume inventorygRPC + protobuf stubs; deadlines propagated
Checkout must not double-chargeIdempotency store at gateway

C — Capacity​

Partner peak POST → 500 RPS
Mobile GraphQL → 2K RPS reads; DataLoader batch size ~50
Internal gRPC → 10K RPS; protobuf ~10× smaller than JSON
Idempotency store TTL → 24h; ~500 × 86400 keys/day order-of-magnitude

T — Topology​

"API gateway: REST for partners with REST→gRPC transcoding to Order service. Mobile hits GraphQL gateway with complexity limit 200 and DataLoader for order lines. Internal east-west pure gRPC with Buf breaking checks. Events via Kafka for order status push to mobile WebSocket—not polling GraphQL."

I — Invariants​

"Idempotency-Key required on partner POSTs. Protobuf additive-only in CI. GraphQL depth and complexity caps enforced before execution. gRPC deadlines propagated on every hop."

C — Constraints​

ConstraintResponse
Browser can't native gRPCgrpc-web or REST transcoding only at edge
GraphQL N+1DataLoader + batch RPCs
Partner long tail on old RESTSunset headers + metrics before removal

E — Evolution​

PhaseDeliverable
M1Partner REST + internal gRPC Order service
M2GraphQL BFF + complexity limits
M3Idempotency store + contract tests in CI
M4Event-driven mobile push; deprecate polling

Closing line​

"Partners get versioned REST + OpenAPI; mobile gets bounded GraphQL; internal gets gRPC + protobuf. Gateway owns auth, idempotency, transcoding, and rate limits—one style per layer, not one style for everything."

Scoring signals​

StrongWeak
Per-client style with gateway policy"GraphQL for everything"
Idempotency + Buf CIStyle evangelism only
DataLoader / N+1 awarenessIgnores operational limits

24. Weak Answer Example​

Question: "Public mobile app + partner integrations—API strategy?"

"Use GraphQL for everything because it's modern."

Why weak: Ignores partner needs, internal efficiency, security, and operational limits.

Additional strong answer​

Question: "Internal team debates REST vs gRPC for new inventory service—your recommendation?"

"Inventory is internal, high read volume, strong typing valuable—I'd default gRPC with protobuf. Generate stubs for Java and Go consumers. Buf enforces backward-compatible schema changes in CI. Expose REST transcoding at API gateway only if browser or partner tools need HTTP—otherwise pure gRPC east-west. Set default deadlines 500ms propagated via context. For bulk export, add server-streaming RPC instead of paginated REST loops. Document in ADR: rejected GraphQL because client set is 3 known services with stable queries—not enough diversity to justify gateway complexity. Observability via OpenTelemetry gRPC instrumentation."

25. Hands-On Exercise​

  1. Define protobuf service for Orders; generate Java and Go stubs.
  2. Implement same read in REST and gRPC; compare payload size and latency.
  3. Build minimal GraphQL schema with intentional N+1; fix with DataLoader.
  4. Add Buf breaking change check to CI.
  5. Configure grpc-web proxy for browser client.
  6. Write OpenAPI spec with deprecation policy for one endpoint.
  7. Benchmark same endpoint REST JSON vs gRPC protobuf at 1k RPS; document payload size and CPU delta.
  8. Implement GraphQL complexity limit and verify deep query rejection.
  9. Design hybrid API strategy document for fictional company with mobile, web, and partner clients.

26. Knowledge Check​

  1. gRPC transport? (HTTP/2 + protobuf.)
  2. GraphQL N+1 cause? (Per-item resolver without batching.)
  3. Safe protobuf change? (Add optional field.)
  4. REST idempotent methods? (GET, PUT, DELETE typically.)
  5. Browser-native gRPC? (No—needs grpc-web.)
  6. GraphQL complexity limit why? (Prevent DoS via expensive queries.)
  7. Buf breaking check purpose? (Block incompatible protobuf in CI.)
  8. BFF best when? (Few client types with known aggregation needs.)
  9. gRPC deadline propagates how? (Context metadata across calls.)
  10. REST cache header example? (Cache-Control, ETag.)

27. Flashcards​

#FrontBack
1RESTResource-oriented HTTP API style.
2gRPCRPC over HTTP/2 with protobuf.
3GraphQLSchema-driven client-specified queries.
4ProtobufBinary IDL with evolution rules.
5OpenAPIREST API specification format.
6BFFBackend-for-Frontend per client type.
7DeadlinegRPC propagated timeout budget.
8DataLoaderBatches GraphQL resolver fetches.
9grpc-webBrowser proxy protocol for gRPC.
10HTTP/2Multiplexed streams for connections.

28. Cheat Sheet​

CHOOSE REST
Public partners, HTTP caching, simplicity

CHOOSE gRPC
Internal high-QPS, streaming, strong contracts

CHOOSE GraphQL
Diverse clients, field flexibility, aggregation

HYBRID (COMMON)
Edge: REST or GraphQL
Internal: gRPC
Async: events

PROTOBUF RULES
Add fields, don't renumber
Reserve removed field numbers

GRAPHQL GUARDRAILS
Complexity/depth limits
Persisted queries in prod
DataLoader batching

API architecture sits at the boundary between external clients and internal distributed systems—choices here constrain decomposition, versioning, caching, and security for years. Review related chapters when designing any new public or partner-facing surface.

30. References​

Primary sources​

  • Fielding, R. (2000). "Architectural Styles and the Design of Network-based Software Architectures" — REST dissertation.
  • gRPC Documentation — grpc.io.
  • GraphQL Specification — graphql.org.

Engineering blogs​

  • Google API Design Guide — resource-oriented REST conventions.
  • Buf Documentation — protobuf breaking change detection.

Distinction​

Claim typeSource
REST constraintsFielding dissertation
gRPC/protobuf semanticsOfficial gRPC and protobuf docs
GraphQL execution modelGraphQL specification
Performance comparisonsBenchmark-dependent—verify per workload