gRPC and GraphQL vs REST
Overview
REST is a great default, but it is not the only option. gRPC and GraphQL solve two specific pain points: speed between internal services, and flexible data fetching for clients. This lesson explains each and when to pick which.
Detailed Notes
What is gRPC and GraphQL
gRPC is a way for one service to call a function on another service as if it were local. You describe the functions and messages in a .proto file, and tools generate client and server code. Data travels as compact Protocol Buffers (a binary format) over HTTP/2.
GraphQL is a query language for APIs. There is a single endpoint, and the client sends a query describing exactly which fields it wants. The server returns just those fields in one response.
The problem it solves
GraphQL fixes two REST annoyances:
- Over-fetching:
GET /users/42returns 30 fields when the screen needs two. - Under-fetching: a screen needs a user, their orders and each order's items, so the client makes several REST calls one after another.
gRPC fixes speed and consistency between services. JSON is text and bigger than binary, and hand-written REST clients drift out of sync with the server. A shared .proto contract and generated code keep both sides aligned and fast.
How it works
gRPC
service OrderService {
rpc GetOrder (OrderRequest) returns (Order);
}
- The client calls
GetOrder(...)through generated code. - The request is packed into small binary messages and sent over HTTP/2, which can run many calls over one connection.
- It supports streaming in either direction, useful for live feeds.
- Browsers cannot call gRPC directly without a helper, so it is mostly used between backend services.
GraphQL
query {
user(id: 42) {
name
orders { id total }
}
}
- The server publishes a schema describing every type and field.
- The client asks for exactly the fields it needs, across related data, in one request.
- Watch out: clients can write expensive queries, so servers limit query depth and cost. Caching is also harder because everything goes to one URL, unlike REST where each URL can be cached.
Comparison
| REST | gRPC | GraphQL | |
|---|---|---|---|
| Format | JSON over HTTP | Binary Protocol Buffers over HTTP/2 | JSON, one query endpoint |
| Contract | Optional (docs) | Required .proto file | Required schema |
| Strength | Simple, cacheable, universal | Fast, streaming, typed | Client picks exact data |
| Weakness | Over/under-fetching | Not browser friendly | Complex caching and query cost |
| Best for | Public APIs | Internal service calls | Varied clients, rich screens |
A simple example
A phone app home screen needs a user's name, their last three orders and the item names. With REST that could be three or four calls returning lots of unused data. With GraphQL it is one query returning only those fields. Meanwhile, behind the scenes, the order service asks the payment service for a charge using gRPC, because speed and a strict contract matter more than human readability there.
System design example
A common setup uses all three:
- Public API for partners: REST, because it is simple, easy to cache and every language supports it.
- Mobile and web apps: GraphQL in front, so each screen fetches exactly what it needs in one trip.
- Between internal services: gRPC for low latency, small payloads and generated clients.
Rule of thumb: start with REST. Choose GraphQL when many different clients need different shapes of data. Choose gRPC when services talk to each other at high volume.
Summary
- gRPC: binary, contract-first, fast and streaming; best between internal services.
- GraphQL: client chooses fields in one request; fixes over- and under-fetching.
- REST: simplest, cacheable and universal; the safe default and best for public APIs.
- It is normal to mix them in one system.
Architecture Diagram
Each style sits where it fits best.
- 1Clients call the public API with REST or GraphQL.
- 2The API forwards work to internal services.
- 3Internal services call each other with gRPC.
- 4Results are combined and returned to the client.
Common Interview Questions
Chapter quiz
API Design: REST, gRPC and GraphQL · 5 questions
1.Which HTTP method is NOT idempotent?
2.A client is not logged in and calls a protected endpoint. Which status code fits best?
3.Why is cursor pagination preferred for large, fast-changing lists?
4.A screen needs data from several related resources and REST needs many calls. Which style helps most?
5.Which style is the best fit for high-volume calls between internal backend services?
0 of 5 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

