All ArticlesAPI & Backend

GraphQL vs REST: Architectural Tradeoffs, Over-fetching, and Performance

Vijay Aravindh
· Last updated:
5 min read

TL;DR

Compare GraphQL and RESTful APIs across caching, network efficiency, schema evolution, client tooling, and query complexity management.

Try the Interactive Tool

Supply a schema to GraphQL Validator and try a query for a field that does not exist on its return type.

Open GraphQL Formatter →

When building distributed web applications and mobile clients, developers face the foundational architectural choice between REST (Representational State Transfer) and GraphQL. Both protocols transport data over HTTP, but their consumption patterns, caching semantics, and schema contracts differ radically.


1. The Over-Fetching and Under-Fetching Dilemma

REST: Resource-Centric Endpoints

In a traditional RESTful architecture, the server defines fixed endpoint representations:

  • GET /api/v1/users/42 returns a fixed user object with 30+ fields.
  • If a mobile view only needs name and avatar_url, the remaining 28 fields represent over-fetching (wasted network bandwidth).
  • If the view also requires the user's latest 3 orders, the client must trigger a second request to GET /api/v1/users/42/orders (under-fetching and the N+1 request problem).

GraphQL: Client-Driven Queries

GraphQL solves this by allowing the client to request precisely the shape of data needed:

Worked example

query GetUserProfile($userId: ID!) {
  user(id: $userId) {
    name
    avatarUrl
    orders(limit: 3) {
      id
      totalAmount
      status
    }
  }
}

2. HTTP Caching vs Application-Level Caching

One of the largest architectural differences lies in caching:

  • REST Caching: Built directly on HTTP semantics. Cache-Control, ETag, and 304 Not Modified headers allow CDN edges (Cloudflare, Fastly) and browser caches to store responses effortlessly.
  • GraphQL Caching: When GraphQL operations use POST /graphql, shared HTTP caches typically do not cache them by default. A client cache is one option. Persisted queries can also be used with GET for cacheable reads, with appropriate cache controls.

3. Schema Validation & Tooling

GraphQL enforces strict type definitions through its Schema Definition Language (SDL). You can validate queries and format schemas instantly using our GraphQL Validator and GraphQL Formatter.

Compare the same account card in two APIs

A page needs only a user's ID and name. Suppose an existing REST endpoint returns the complete user representation, including the email and locale used by other screens. An example exchange might look like this. These are fixtures for an API you control, so the URL describes the interface rather than an endpoint to call on DevStackTools.

GET /users/42 HTTP/1.1
Host: api.example.com
Accept: application/json

HTTP/1.1 200 OK
Content-Type: application/json

{"id":"42","name":"Ada","email":"ada@example.com","locale":"en"}

Here is a GraphQL schema and operation for the same account card. Paste the first block into Schema (optional) in GraphQL Validator and the second into Input GraphQL. The operation asks for two fields even though the schema exposes four.

type Query { user(id: ID!): User }
type User { id: ID!, name: String!, email: String!, locale: String! }
{ user(id: "42") { id name } }

Running that operation with graphql-js against a resolver returning the REST fixture produces this response body.

{"data":{"user":{"id":"42","name":"Ada"}}}

This is an example of over-fetching from a fixed representation. It does not establish that REST always over-fetches. A REST API can define a compact account-card endpoint or offer a documented field-selection parameter. GraphQL is especially convenient when several clients have changing selection needs and the service can give them a consistent schema. It still needs field-level authorization. The client being able to name email in an operation does not grant permission to read it.

Selection also differs from database projection. A GraphQL resolver might fetch the entire row to return two fields. If that is expensive or includes sensitive data the resolver does not need, inspect the database query as well as the response. For a remote service, the costly part may be the number of calls rather than the size of the JSON. Trace the data access layer before estimating savings from the selection set.

Fix one lookup per order

Consider an order list that selects the customer name on every order. A resolver that loads each customer independently can send one query for the list and then one additional query per order. If orders repeat a customer, the same record may be fetched several times. A small test with a single order can conceal the problem. Reproduce it with a list that includes both repeated and distinct customer IDs, and count backend calls rather than assuming the endpoint's response time reveals why it slowed down.

// Pseudocode: each call can issue its own database query.
const Order = {
  customer: (order, _args, ctx) => ctx.db.customerById(order.customerId),
};

Batch the loads within the request. A loader collects IDs for a batch, calls the backend once for that group, then returns one result per requested key in the same order.

// Pseudocode: create the loader when creating this request's context.
const customers = new DataLoader(async (ids) => {
  const rows = await db.customersVisibleTo(viewer, ids);
  const byId = new Map(rows.map((row) => [row.id, row]));
  return ids.map((id) => byId.get(id) ?? null);
});

const Order = {
  customer: (order, _args, ctx) => ctx.customers.load(order.customerId),
};

The database may return rows in a different order or omit a record the viewer cannot see. Returning its rows directly would associate the wrong customer with some orders. The map fixes the ordering and defines a missing-record result. Keep the loader request-scoped so cached values cannot leak between callers with different permissions. This design reduces repeated queries within compatible batches. Nested resolver work and multiple batches can still issue additional queries, so verify the effect in traces for the actual operation.

Decide what can be cached

A REST GET can fit HTTP caching naturally when the URL identifies a representation and the server sets appropriate cache controls, validators, and variation rules. Authenticated responses need an explicit policy that accounts for user identity. A response with account-specific fields should not end up in a shared cache under a key that represents only the route.

A persisted GraphQL query identifies a known operation by an ID or hash. That helps control which operation a client sends, but it does not make its result cacheable by itself. Variables, authorization, locale, and data freshness may all affect the response. If you support GET for read operations, confirm that the HTTP cache key distinguishes these inputs and that the response is safe to share. If you use a client-side normalized cache, choose stable entity IDs and decide how mutations update cached selections.

For a new interface, start with the consumers. If they have stable resource representations and benefit from straightforward HTTP cache behavior, model those representations directly. If they need changing slices across related entities, test a schema and a few actual selections. In either case, budget query complexity, verify authorization at the data boundary, and measure backend calls and response bytes separately. Those checks reveal whether the interface solves the real cost in your system.

Found this guide helpful?

Explore our 50+ privacy-first developer utility tools.

Explore Tools