Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path →
GraphQL vs REST: who picks the shape of the data?
REST hands you a menu of fixed dishes; GraphQL hands you the ingredients and lets each client cook exactly the plate it wants. That single difference — server-decided shape vs client-decided shape — cascades into everything else: over-fetching, caching, versioning, and the failure modes each one is prone to.
New to this? Start here
Both are ways for an app to ask a server for data. Your phone app needs a user's name, their photo and their last three orders. It has to ask a server. REST and GraphQL are two conventions for how that asking works.
REST gives you a set of fixed addresses, each returning a fixed shape: /users/42 returns the user, /users/42/orders returns the orders. The server decides what each address returns. Want three things? Usually three requests.
GraphQL gives you one address and a query language. You send a request describing exactly the fields you want, across all three things, and get back one response shaped like your request. The client decides what comes back.
That is the whole distinction — server-decided shape versus client-decided shape — and everything below is a consequence of it: how much data you over-fetch, how cacheable responses are, how you version the API, and how each one fails under load.
The one distinction that decides everything
REST models your API as a set of resources, each at its own URL (/users/42, /users/42/posts), and the server decides what fields each endpoint returns. GraphQL exposes a single endpoint (/graphql) and a type schema, and the client writes a query saying exactly which fields of which resources it wants — the server assembles precisely that shape in one response.
If the server should own the response shape and you want simple, cacheable, resource-oriented endpoints, REST fits. If many different clients each need a different slice of deeply-related data and you want them to ask for exactly that in one round trip, GraphQL fits.
Head to head
| Dimension | REST | GraphQL |
|---|---|---|
| Endpoints | Many, one per resource | One (/graphql), a typed schema behind it |
| Response shape | Fixed by the server per endpoint | Chosen by the client, per query |
| Over/under-fetching | Common — fixed payloads, multiple calls to assemble a view | Solved — ask for exactly the fields you need, across resources, at once |
| HTTP caching | Free — GET + stable URL caches in browsers, CDNs, proxies | Hard — single POST endpoint bypasses URL caching; needs app-layer caching |
| Versioning | Explicit — /v2/, breaking changes ship new URLs | Evolve the schema — deprecate fields instead of versioning |
| Typing & introspection | Convention (OpenAPI adds it on) | Built-in — a typed schema, introspectable, self-documenting |
| Error handling | HTTP status codes (404, 500…) | Usually HTTP 200 with an errors array; partial data is normal |
| Server complexity | Low — each route is a handler | Higher — resolvers, schema, and the N+1 problem to manage |
| Classic failure mode | The mobile app makes 6 calls and still over-fetches | One innocent query fans out into N+1 database hits |
When to use each
Reach for REST
- A public API many third parties consume (predictable, cacheable, familiar)
- Simple, resource-shaped CRUD without deep nesting
- You want HTTP caching, CDNs and standard status codes for free
- File uploads/downloads and streaming, which REST handles naturally
- A small team that doesn't want to run a schema + resolver layer
Reach for GraphQL
- Many clients (web, iOS, Android) each needing a different data shape
- Deeply nested, related data assembled per screen (feeds, dashboards)
- You're tired of shipping a new endpoint every time the UI changes
- A gateway aggregating several backend services behind one schema
- Fast-moving product teams that want the client to drive the query
The answer is often "REST underneath, GraphQL at the edge"
These aren't mutually exclusive. A very common production shape is REST (or gRPC) between internal services — where simple, cacheable, independently-deployed endpoints shine — with a GraphQL gateway in front, aggregating those services into one client-facing schema so the web and mobile apps get exactly-shaped data in a single round trip. The clients get GraphQL's ergonomics; the backend keeps REST's simplicity and caching. You don't have to pick one for the whole system.
Start with REST. It's simpler, its caching is free, and most APIs are resource-shaped enough that it's genuinely the right tool. Reach for GraphQL when you can point at a concrete pain — multiple clients over-fetching, or a screen that needs five REST calls to render — not because it's newer.
The caching and N+1 reality nobody mentions in the demo
GraphQL's flexibility has two costs the tutorials skip. First, caching: REST rides on HTTP, so a GET /users/42 is cached for free by the browser, the CDN and any proxy in between. GraphQL clients typically send queries as a POST to one URL, so that layer of free caching mostly evaporates (the spec allows queries over GET, and persisted queries sent by GET are CDN-cacheable) — you rebuild it in the application with persisted queries, a normalized client cache (Apollo, urql), and server-side response caching keyed on the query, not the URL.
Second, the N+1 problem: a query for 50 posts, each with its author, can trigger a naive resolver to run 1 query for the posts and then 50 more for the authors — 51 database hits for one request. The fix is DataLoader, which batches every author lookup in a single tick into one IN (...) query and caches results per request. It's a solved problem — but it's a problem you have to know exists, whereas REST's fixed endpoints rarely surprise you this way.
GraphQL moves work from the client (which no longer over-fetches or makes many round trips) onto the server (which now owns schema design, resolver batching and a caching strategy HTTP used to give you for free).
A worked scenario: a public weather API vs a social feed app
A public weather API — GET /forecast?city=London — is a textbook REST case: the resource is simple, the response shape is stable, thousands of clients want the same data, and CDN caching a popular city's forecast for a few minutes is a massive, free win. Wrapping that in GraphQL would add a schema, resolvers and a caching headache to solve a problem REST already handles perfectly.
A social app's home feed is the opposite. One screen needs the current user, their feed of posts, each post's author, like count, top comments and each commenter's avatar — five or six related resources, nested, and the iOS app wants slightly different fields than the web app. In REST that's a waterfall of calls or a bespoke /feed endpoint you re-cut every time the design changes. In GraphQL it's one query per client, each asking for exactly its fields — with DataLoader batching the author and comment lookups so "one query" doesn't quietly become N+1 behind the scenes.
Stable, cacheable, one-shape-fits-all data → REST. Many clients, deeply nested and varied per screen → GraphQL. Big systems usually have both shapes, which is why "REST services behind a GraphQL gateway" is so common.
The picture: one screen, two request paths
IN (…) query. That gives 2 queries, and one more per extra nesting level.Cost and trade-offs: round trips, caching and server load
Here Time is what the client waits for or the server spends. Space is bandwidth, cache or query load. Round-trip figures assume a slow mobile link at 200 ms per round trip, an illustrative number. Server time is ignored.
| Case | Time | Space | Why |
|---|---|---|---|
| Screen needs 5 resources, REST, one after another | 5 × 200 ms = 1,000 ms | Each response is the endpoint’s full shape, used or not | Each call waits for an id from the one before. This is the waterfall the page warns about. |
| Same screen, REST, independent calls in parallel | 2 dependency levels × 200 ms = 400 ms | Still over-fetches fields | Parallel calls help only when the next call does not need the previous answer. |
| Same screen, one GraphQL query | 1 × 200 ms = 200 ms | Only the requested fields cross the wire | The server now runs every resolver, so its latency is your latency. |
| Feed with comments, naive resolvers | 1 + 50 + 50 + 150 = 251 queries for 50 posts with 3 comments each | Database load grows with list sizes multiplied together | Posts, then an author per post, comments per post, and a commenter per comment. |
| Same feed with DataLoader | 4 queries, one per nesting level | A per-request cache of loaded ids | The number of queries now grows with the query’s depth, not with how many rows come back. |
| Popular public GET, cached 60 s at the edge | 1,000 req/s is 60,000 a minute, and the origin sees about 1 per edge location per minute | Edge storage, which costs you nothing to build | Needs Cache-Control headers. A shared cache will not reuse a response to a request carrying Authorization unless you explicitly mark it cacheable. |
| GraphQL caching | Persisted queries sent by GET restore URL caching for known queries | A normalized client cache, plus server caches keyed on query and variables | Ad-hoc POST queries still miss the CDN. You rebuild the layer REST got for free. |
| Breaking change | REST: ship /v2 and run both versions until clients move | GraphQL: mark the field @deprecated and keep serving it | You can only remove a GraphQL field once you can see that no client still asks for it, so log field usage. |
The table makes the page’s trade concrete. GraphQL turns a 1,000 ms client waterfall into one round trip. It does this by moving the joins into resolvers you must batch, and the caching into layers you must build. REST keeps the server simple and the CDN free, and pays for it in client round trips and over-fetching. A gateway in front of REST services, the page’s common production shape, takes some of each. The client gets the single round trip. The services behind it keep their simple, cacheable endpoints, and the batching problem moves into the gateway’s resolvers, where one team can solve it once.
Common mistakes
- No cost limit on a public GraphQL API
A client can nest
friends { friends { friends { … } } }until one request touches millions of rows. Set a depth limit and a cost budget per query, and paginate every list. For your own apps, accept only persisted queries, a fixed allowlist of known query hashes, so arbitrary queries never run at all. - Alerting on HTTP status codes
GraphQL usually returns
200with anerrorsarray, and partial data counts as success. A dashboard of 5xx rates stays green while half your resolvers fail. Count entries inerrorsper operation name, and alert on that. - One DataLoader shared across requests
DataLoader caches every id it loads. Create it once at server start and that cache is shared by every user and never refreshed, so one user can be served another’s stale or private record. Create a new loader per request, in the request context, so its cache lives exactly as long as the request and is thrown away with it.
- Schema that mirrors the database
Generating one type per table, with foreign keys as fields, exposes your storage layout to every client and freezes it there. Design types around what screens need, and keep table names and join keys behind the resolvers.
- Assuming REST caching is automatic
A GET is cacheable only if the response says so. With no
Cache-Control, or with per-user data behind anAuthorizationheader, the CDN passes every request through to the origin. Decide per endpoint what is public, for how long, and what varies by user.
Which would you pick?
Three situations. Pick the side you'd actually build — the explanation follows.
A mobile client renders a screen from five related resources and is on a slow connection.
Five round trips on a slow link is exactly the latency problem GraphQL was built for, and over-fetching wastes bandwidth the client is short of. Caching helps a returning user and does nothing for the first paint, which is where this hurts most.
A public API served to unknown third parties at high volume, where you must protect the backend from expensive queries.
GraphQL hands query construction to the caller, so an untrusted client can compose something expensive that no endpoint author anticipated. Depth limits and cost analysis exist precisely because this is hard, and they are a mitigation rather than a solution. REST endpoints have a shape you chose and a cost you can measure in advance.
Someone argues GraphQL is better because "you only fetch the fields you need."
Precise fetching is genuine, and it is one axis among several. GraphQL gives up cheap HTTP caching, makes per-request cost harder to bound, and adds schema and resolver machinery. The honest comparison weighs those together rather than settling it on the benefit that is easiest to demo.
Quick answers
GraphQL or REST — which should I use?
Decide by who needs to control the response shape. For a simple, public, cacheable API with stable resources, REST is simpler and its HTTP caching is free. For an app with many clients fetching deeply nested, varied data, GraphQL lets each client ask for exactly what it needs in one round trip. Many teams run both: REST for public/simple endpoints, GraphQL as a client-facing gateway.
What problem does GraphQL actually solve?
Over-fetching and under-fetching. A fixed REST endpoint returns a fixed shape, so a mobile screen often gets more fields than it needs, or has to call three endpoints to assemble one view. GraphQL lets the client request exactly the fields it wants, for multiple resources, in a single request.
Why is caching harder with GraphQL?
REST rides on HTTP — a GET to a stable URL is cached for free by browsers, CDNs and proxies. GraphQL usually POSTs every query to one endpoint, so URL-based caching doesn't apply. You recover it at the app layer: persisted queries, a normalized client cache, and DataLoader on the server.
What is the N+1 problem in GraphQL?
A query for a list plus a field on each item that needs its own lookup (50 posts, each with an author) can fire 1 + N queries. The standard fix is DataLoader, which batches all the lookups from one tick into a single query and caches them per request.
GraphQL vs REST: a fixed menu, or build-your-own?
Explore the topic
See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.
More Handbooks
- The System Design Fundamentals HandbookThe load-bearing ideas behind every distributed system — the CAP theorem and consistency models, concurrency and locking, partitioning and replication, and consensus and coordination — each tied to a real worked design you can study interactively.Read →
- CAP Theorem & Consistency ModelsWhat a distributed system can promise when the network splits — CP vs AP, why "CA" is a myth, PACELC, and the full spectrum from linearizable to eventual consistency. Part of System Design Fundamentals.Read →
- Partitioning, Sharding & ReplicationHow one dataset becomes many — range vs hash partitioning, consistent hashing and virtual nodes, hot partitions, replication topologies, replication lag, and quorums. Part of System Design Fundamentals.Read →
- Consensus, Transactions & CoordinationHow nodes that can crash still agree on one truth — majority quorums, Raft and Paxos, leader election, two-phase commit vs the saga pattern, and idempotency for exactly-once effects. Part of System Design Fundamentals.Read →
- The Kubernetes HandbookThe one idea under all the YAML — declare desired state, and a control loop makes reality match. Covers the orchestration problem, pods, deployments and replicasets, services and networking, the reconciliation loop and self-healing, the scheduler, config/secrets and health probes, autoscaling, and when you actually need Kubernetes.Read →
- The Observability HandbookSeeing inside production — monitoring vs observability, the three pillars (metrics, logs, traces) and what each answers, structured logging, metric types and the cardinality trap, distributed tracing, the golden signals and SLIs/SLOs/error budgets, alerting on symptoms not causes, and correlating all three during an incident.Read →