REST vs GraphQL vs RPC
Three ways to shape the contract between clients and services — each optimizing for a different kind of pain.
REST vs GraphQL vs RPC in 30 seconds
- It's a contract: how a client asks a service for data or an action. Three popular shapes, each removing one pain.
- REST: resources + HTTP verbs. Simple, cacheable, everywhere — but over- and under-fetching mean lots of round trips.
- GraphQL: one endpoint, ask for exactly the fields you want. Kills over/under-fetch — but caching is hard and queries can get expensive.
- RPC / gRPC: call a remote function. Fast, typed, action-shaped — the go-to for internal service-to-service traffic.
- Mix freely: REST or GraphQL at the public edge, gRPC between services. It's a per-boundary choice, not a religion.
- Watch for: GraphQL abuse — add query depth and complexity limits so one query can't DoS you.
Three ways to order at a restaurant
Imagine three restaurants. At the first, every dish has its own fixed plate you point at on a menu — simple, and the kitchen can prep popular plates ahead of time, but if you want a custom combo you order several plates and assemble it yourself. At the second, you hand the waiter one slip listing exactly what you want on your plate, and it comes back built to order in a single trip. At the third, you don't browse a menu at all — you just tell the chef 'make me the usual,' naming an action the kitchen knows how to perform.
Those are REST, GraphQL, and RPC. The food (your data) is the same; what differs is how you ask for it. Each style makes one thing easy and one thing annoying — and the whole skill is matching the style to the pain you actually have.
The problem it solves
An API is a contract: how does a client ask a service to do something or return data? The three dominant styles answer that differently. REST models everything as resources you act on with standard verbs. GraphQL gives clients one endpoint and lets them ask for exactly the fields they want. RPC makes a remote call look like calling a local function. None is universally best — each removes a specific friction and adds another.
Why 'just return the data' isn't a plan
It's tempting to think the API shape doesn't matter — the server has the data, just send it over. But how you ask shapes real, user-visible costs, and ignoring that bites you fast:
- Round trips cost latency. On mobile, each extra request to build one screen can add hundreds of milliseconds. The contract decides how many you make.
- Over-fetching wastes bandwidth. An endpoint that returns a whole user object when you needed only the name burns data and battery.
- Rigid shapes slow evolution. If every field change needs a new endpoint, your API ossifies and clients break.
- No typing means runtime surprises. Loosely-shaped payloads push bugs to production instead of to the compiler.
Each style is essentially a different answer to 'which of these pains do I least want to feel?' So let's take them one at a time.
REST — resources and verbs
REST organizes the API around resources (/users/42, /orders) acted on with HTTP verbs (GET, POST, PUT, DELETE). It's simple, cacheable (GETs cache naturally), and universally understood. Its weaknesses: over-fetching (an endpoint returns more than you need) and under-fetching (you must call several endpoints to assemble one screen), which pushes mobile clients to make many round trips.
GET /users/42 # read one user
POST /users # create a user (body has the fields)
PUT /users/42 # replace user 42
DELETE /users/42 # remove user 42
# Building a profile screen often means several calls:
GET /users/42 # the user
GET /users/42/orders # ...their orders (under-fetch: another round trip)
GET /users/42/photos # ...and their photos (and another)GraphQL — ask for exactly what you need
GraphQL exposes a single endpoint and a type schema; the client sends a query describing exactly the fields it wants, across multiple resources, in one request. That kills over- and under-fetching and is great for complex, evolving front ends. The costs: caching is harder (it's not simple URL-based GETs), a complex query can be expensive on the server, and you need query-depth/complexity limits to prevent abuse.
POST /graphql
query {
user(id: 42) {
name # just the fields this screen needs
orders(last: 3) { id total } # ...pulled in the SAME round trip
photos(first: 6) { url }
}
}
# One call returns a response shaped exactly like the query —
# no over-fetch (only these fields), no under-fetch (all of it at once).The gap GraphQL opens — abuse and caching
GraphQL's flexibility is also its danger. Because a client can request anything the schema allows, a single cleverly-nested query can ask for enormous, deeply-linked data and melt your backend — a built-in denial-of-service vector you must defend against:
# A malicious client nests relationships to explode the work:
query {
user(id: 42) { friends { friends { friends { posts { comments } } } } }
}
# Defenses you add at the GraphQL layer:
max_query_depth = 6 # reject queries nested deeper than this
max_complexity = 1000 # each field costs points; cap the total
require_persisted_queries() # only allow pre-approved query shapes in prodThe caching gap is the other cost. A REST GET /users/42 caches on its URL; a GraphQL request is a POST with a query body, so edge caches can't key on it naturally. Teams claw some of that back with persisted queries and field-level caches, but it's real work REST gets for free.
RPC (and gRPC) — call a remote function
RPC frames the API as actions: createShipment(order), chargeCard(amount). It fits well when the interface is a set of operations rather than resources. gRPC is the common modern form — binary, fast, strongly typed via schemas, with streaming support — which makes it a favorite for internal service-to-service communication. The trade-off: it's less human-readable and not as browser-friendly as REST.
// user.proto defines the contract both sides compile against:
// rpc GetUser(GetUserRequest) returns (User);
// The caller invokes it as if it were local — types checked at compile time:
User user = userStub.getUser(
GetUserRequest.newBuilder().setId(42).build()
);
System.out.println(user.getName()); // "Ada" — no JSON parsing, no guessing
// Binary on the wire, generated types on both ends -> fast and safe.# REST: a resource addressed by URL, fetched with a verb
GET /users/42 -> { "id": 42, "name": "Ada", "email": "..." }
# GraphQL: one endpoint, ask for exactly the fields you want
POST /graphql
{ user(id: 42) { name email } } -> { "user": { "name": "Ada", "email": "..." } }
# gRPC: call a typed remote function defined in a .proto schema
stub.GetUser(GetUserRequest(id=42)) -> User(name="Ada", email="...")Choosing a style — a quick comparison
| Style | Best at | Weak at | Use when |
|---|---|---|---|
| REST | Simplicity, caching, ubiquity. | Over/under-fetching; many round trips. | Public APIs, CRUD-shaped resources. |
| GraphQL | Flexible reads; one call per screen. | Caching; query-cost control. | Rich, varied front ends over many entities. |
| RPC / gRPC | Fast, typed, action-oriented calls. | Browser-friendliness; readability. | Internal microservice-to-service traffic. |
Choosing an API style looks like a syntax decision, but it's really about where you put the friction between clients and services — and that shapes performance, developer speed, and how your system evolves.
Why it matters
- The contract shape decides how many round trips a screen costs — a huge deal for mobile and slow networks.
- It sets how easy it is to evolve: GraphQL schemas and gRPC proto files make typed, versioned change manageable.
- It draws the line between your public edge (readable, cacheable) and your internal mesh (fast, typed) — two different jobs.
If ignored
- Chatty REST between internal services burns latency and CPU where gRPC would've been fast and typed.
- A wide-open GraphQL endpoint becomes a denial-of-service vector when one deep query melts the backend.
- Over- and under-fetching pile up into sluggish clients and a graveyard of single-purpose endpoints.
Where it fits
- Sits at the request path — the boundary a client crosses to reach your services.
- Pairs naturally with an API gateway that handles auth, routing, and rate limits across whatever style you pick.
- Reads lean on caching and a CDN; writes lean on idempotency to stay safe under retries.
Related topics
- API gateway: the front door that fronts these styles and owns cross-cutting concerns
- Communication protocols: REST/GraphQL ride HTTP; gRPC rides HTTP/2 — the transport underneath
- Idempotency: why POST and RPC actions need keys to survive retries safely
- Caching: why REST GETs cache naturally and GraphQL makes it harder
Interview lens
Questions it unlocks
- REST or GraphQL for this client — why?
- What API would you use between internal microservices?
- How do you stop a GraphQL endpoint from being abused?
60-second answer
I pick by the pain I'm removing. REST for public, CRUD-shaped, cache-friendly APIs; GraphQL when varied front ends suffer from over- and under-fetching and want one call per screen; gRPC for fast, typed, internal service-to-service traffic. I'd happily mix them — GraphQL or REST at the edge, gRPC inside. For GraphQL specifically I'd add query depth and complexity limits so a single query can't become a denial-of-service, since its flexibility is also its risk.
Common traps
- Claiming one style is universally superior.
- Forgetting GraphQL's caching and query-cost challenges.
- Using chatty REST for high-performance internal calls where gRPC fits better.