Versioning, Webhooks and Alternatives · Lesson 25 of 30
REST vs GraphQL vs gRPC
REST is the default, but not the only option. GraphQL lets clients ask for exactly the data they need in one request; gRPC offers fast, strongly typed calls between services. Each solves real problems – and adds its own. Senior engineers are expected to explain the trade-offs, not just name the technologies.
In this lesson
- The same feature in REST, GraphQL and gRPC
- Over-fetching and under-fetching
- A detailed comparison
- Choosing – and combining – them
One screen, three styles
A course page needs the course title, its instructor's name and the latest three reviews.
# REST – resources; may take several requests, may return fields you don't need
GET /api/courses/rest-api → { id, title, description, price, instructorId, … }
GET /api/instructors/u7 → { id, name, bio, avatarUrl, … }
GET /api/courses/rest-api/reviews?limit=3 → [ … ]
# GraphQL – one endpoint; the client describes the exact shape it wants
POST /graphql
query {
course(id: "rest-api") {
title
instructor { name }
reviews(last: 3) { rating comment }
}
}
# gRPC – a typed contract in a .proto file; binary messages over HTTP/2
service CourseService {
rpc GetCoursePage (GetCoursePageRequest) returns (CoursePage);
}
message GetCoursePageRequest { string course_id = 1; int32 review_count = 2; }
Over-fetching and under-fetching
- Over-fetching: REST's
/courses/rest-apireturns the description and price even when the screen only needs the title. - Under-fetching: the screen needed three round trips. On a slow mobile network, that's noticeable.
REST has answers too – field selection (?fields=title,instructor.name), embedding (?include=instructor), or a purpose-built endpoint for a screen (the "backend for frontend" pattern). GraphQL makes this flexibility the core of the design.
Comparison
| REST | GraphQL | gRPC | |
|---|---|---|---|
| Transport | HTTP/1.1+, any method | HTTP, usually one POST endpoint | HTTP/2 (HTTP/3 emerging) |
| Data format | JSON (any media type) | JSON | Protocol Buffers (binary) |
| Contract | optional (OpenAPI) | required schema, introspectable | required .proto, code generated |
| Fetching | fixed shapes per endpoint | client chooses fields | fixed messages per method |
| HTTP caching | excellent (GET, ETag, CDNs) | difficult (POST, varied queries) | none built in |
| Streaming | SSE, chunked responses | subscriptions | first-class (client, server, bidirectional) |
| Browser support | native | native | needs gRPC-Web or a proxy |
| Debugging | curl and a browser | GraphiQL, good tooling | special tools (grpcurl) |
| Main risks | chatty clients, inconsistent design | expensive queries (need depth/cost limits), N+1 database calls | harder to expose publicly |
Choosing
- REST – public APIs, partner integrations, CRUD-style services, anything that benefits from HTTP caching and universal tooling. The safe default.
- GraphQL – a rich frontend (or several) aggregating data from many sources, where screens need different shapes and round trips matter.
- gRPC – internal service-to-service calls with high volume and low latency, polyglot microservices, streaming.
- They combine well: gRPC between internal services, a GraphQL or REST gateway for frontends, REST for public partners.
Practice
Try it yourself
Pick a style for: (a) a public payments API for merchants, (b) twenty internal microservices exchanging thousands of calls per second, (c) a mobile app showing dashboards from five backend systems.
Show answer
(a) REST – universal, cacheable, easy for merchants in any language, with OpenAPI docs and webhooks. (b) gRPC – compact binary messages, HTTP/2 multiplexing, generated typed clients. (c) GraphQL (or a REST backend-for-frontend) – one request per screen with exactly the fields needed.
Quick check
What is over-fetching?
Receiving fields the client doesn't need.
Why is HTTP caching harder with GraphQL?
Queries usually go via POST to one URL, with varied shapes.
What format does gRPC use?
Protocol Buffers, a compact binary format defined in .proto files.
Summary
- REST: resources and HTTP semantics – the universal default with great caching.
- GraphQL: client-shaped queries – fewer round trips, needs query cost controls.
- gRPC: typed, fast binary RPC – ideal between internal services.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.