Ankit KanojiyaIntermediate
Evergreen guide by Ankit Kanojiya · Intermediate · Backend
REST vs GraphQL vs gRPC: When to Use Which
REST, GraphQL and gRPC solve different problems. Here is how each works, a worked example of the same API in all three, and the questions interviewers ask.
- backend
- api
- rest
- graphql
- grpc
- system design
Every backend interview eventually arrives at the same question: "Why did you choose REST here? Would GraphQL or gRPC have been better?" A list of features is the weak answer. The strong one starts with the problem each style was built to solve, because that tells you when to pick it.
The problem each one solves
REST came first, and it solved a simple problem: how do you expose data over HTTP in a way that every client, proxy and cache already understands? The answer was to treat your data as resources with URLs, and to reuse HTTP verbs (GET, POST, PUT, PATCH, DELETE) and status codes instead of inventing your own. A GET /users/42 is cacheable by any CDN on earth without configuration because the web already knows what GET means.
GraphQL, open-sourced by Facebook in 2015, solved a problem REST created. A mobile app screen often needs a user, their last five orders and the first image of each product. With REST that is one request for the user, one for the orders and five for the products, or a custom endpoint that returns exactly that shape and breaks the moment the designer changes the screen. GraphQL lets the client say what it wants in one query, and the server returns exactly that, nothing more.
gRPC, from Google, solved a different problem again: services talking to other services inside a data centre, thousands of times a second, where JSON parsing and HTTP/1.1 overhead actually show up in the bill. gRPC uses Protocol Buffers (a compact binary format with a schema) over HTTP/2, generates client and server code from that schema, and supports streaming in both directions.
In one line: REST for public and simple APIs, GraphQL for flexible client-driven reads, gRPC for fast internal service-to-service calls. The details below are what make that line true, and where it breaks.
How each one works
A REST API is a set of resources. Each has a URL, and the HTTP method says what you want to do to it. The response is usually JSON. There is no formal contract unless you add one (OpenAPI is the common choice). Errors are HTTP status codes: 404 if the resource is missing, 400 if your request is malformed, 422 if it is valid JSON but semantically wrong.
A GraphQL API has exactly one endpoint, almost always POST /graphql. The server publishes a schema that defines types and the fields on them. The client sends a query written against that schema. The server resolves each field, possibly from different databases or services, and returns a JSON object shaped like the query. Mutations change data; subscriptions push updates over a WebSocket. Because there is one endpoint and everything is a POST, HTTP caching mostly does not apply; caching happens in the client (Apollo, Relay) or at the resolver level.
A gRPC API is defined in a .proto file: you declare messages (the data types) and services (the RPC methods). A compiler generates typed client and server code in your language. Calls go over HTTP/2, which allows many requests on one connection, and the payload is binary Protobuf, which is smaller and faster to parse than JSON. Four call types exist: unary (one request, one response), server streaming, client streaming and bidirectional streaming.
The same API three ways
Say we are building a tiny orders service. We need to fetch an order and the name of the customer who placed it.
REST:
GET /orders/1001
→ 200 OK
{ "id": 1001, "status": "shipped", "customerId": 7, "total": 1499 }
GET /customers/7
→ 200 OK
{ "id": 7, "name": "Priya", "email": "priya@example.com", "phone": "..." }
Two round trips, and the second response includes fields we did not ask for. That is the over-fetching and the N+1 problem in miniature. You can fix it with GET /orders/1001?include=customer, but now you are designing a query language by hand.
GraphQL:
query {
order(id: 1001) {
id
status
total
customer {
name
}
}
}
One request, and the response contains exactly id, status, total and customer.name. The schema behind it looks like:
type Order {
id: ID!
status: String!
total: Int!
customer: Customer!
}
type Customer {
id: ID!
name: String!
email: String!
}
type Query {
order(id: ID!): Order
}
The cost is on the server side. The customer field has its own resolver, and if a client asks for 100 orders with customers, a naive implementation makes 100 customer queries. Every production GraphQL server uses a batching layer (DataLoader is the standard one) to collapse those into one.
gRPC:
syntax = "proto3";
message GetOrderRequest { int64 id = 1; }
message Order {
int64 id = 1;
string status = 2;
int64 customer_id = 3;
int32 total = 4;
}
service OrderService {
rpc GetOrder (GetOrderRequest) returns (Order);
}
Generate the code, and in Go or Java or Python the call is client.GetOrder(GetOrderRequest{Id: 1001}), fully typed. The browser cannot call this directly (browsers do not expose raw HTTP/2 frames), so you add grpc-web or a gateway in front for web clients. That is the main reason gRPC stays internal.
Choosing: the questions that actually decide it
Who are the clients? If they are third-party developers, or a browser, or anyone you do not control, REST wins on familiarity and tooling. If they are your own mobile and web apps with many different screens, GraphQL pays off. If they are your own services inside a cluster, gRPC.
How much does payload size and latency matter? For most CRUD apps, not much; JSON over HTTP/1.1 is fine. For a service called 10,000 times a second, Protobuf and connection multiplexing are a measurable saving.
Do you need caching at the HTTP layer? REST gets CDN and browser caching for free on GET. GraphQL and gRPC do not.
Do you need streaming? gRPC has it built in. GraphQL has subscriptions. REST has Server-Sent Events or a separate WebSocket, both bolted on.
Who owns the schema? In REST, the server decides the response shape. In GraphQL, the client decides and the server has to be ready for any combination, which makes performance harder to predict and rate limiting harder to design (you end up scoring query complexity instead of counting requests). In gRPC, both sides compile the same .proto, so the contract is enforced at build time.
Mixing is normal. A common layout in a mid-sized company is gRPC between internal services, a GraphQL layer (often called a BFF, backend for frontend) that aggregates them for the apps, and a plain REST API for partners. Nobody picks one for everything.
Questions interviewers ask
What is over-fetching and under-fetching, and which style fixes each? Over-fetching is getting fields you do not need; under-fetching is needing several requests for one screen. GraphQL addresses both; REST with sparse fieldsets and include parameters addresses them partially.
Why can't a browser call gRPC directly? Browsers do not give JavaScript control over HTTP/2 framing or trailers, which gRPC relies on. grpc-web translates in between.
How do you version a REST API versus a GraphQL API? REST typically versions in the URL or a header (/v2/orders). GraphQL avoids versions: you add fields and mark old ones @deprecated, because clients only get what they ask for, so adding fields never breaks them.
What is the N+1 problem and how does DataLoader solve it? A list resolver triggers one query per child item. DataLoader collects all the IDs requested in one tick of the event loop and issues a single batched query.
Is REST stateless, and why does that matter? Each request carries everything the server needs; no session on the server. That is what lets you add servers behind a load balancer without sticky sessions.
When would you not use gRPC even for internal services? When the team is small and debugging with curl matters more than throughput, when you need HTTP caching, or when your languages have weak gRPC tooling.
Go deeper
RFC 9110 (HTTP Semantics) for what the verbs and status codes actually promise. The GraphQL spec and the "Thinking in Graphs" guide at graphql.org. The gRPC "Core concepts" and "Introduction" pages at grpc.io, plus the Protocol Buffers language guide. The DataLoader README on GitHub for the batching pattern. Roy Fielding's dissertation, chapter 5, if you want to know what REST originally meant before it became "JSON over HTTP".