REST vs GraphQL vs gRPC

Three common API styles, what each optimizes for, and when each fits.

What is it?

An API style is the set of conventions a client and server agree on for asking for data and sending changes. Three styles dominate today:

- REST models data as resources with URLs (/users/42) and uses HTTP methods (GET, POST, PUT, DELETE) to act on them. Responses are usually JSON. - GraphQL exposes one endpoint and a typed schema. The client sends a query describing exactly which fields it wants, and gets back exactly that shape. - gRPC is a remote procedure call framework. You define services and messages in a .proto file, generate client and server code, and call remote methods as if they were local functions. Messages are encoded with Protocol Buffers (a compact binary format) and sent over HTTP/2.

None of them is "the best". REST is the simple default for public web APIs, GraphQL shines when many different clients need different slices of data, and gRPC is a strong fit for fast, typed service-to-service calls.

Explain like I'm 10

REST is a restaurant with a fixed menu: each dish (endpoint) comes as the kitchen plates it. GraphQL is a build-your-own bowl counter: you list exactly the ingredients you want and get one bowl. gRPC is a walkie-talkie between two kitchens that share a codebook: messages are short, coded and fast, but you need the codebook to understand them.

Examples

The same request in three styles

# REST: two round trips, and you get every field the server chooses
GET /users/42
GET /users/42/orders?limit=3

# GraphQL: one round trip, exactly the fields you ask for
POST /graphql
{
  user(id: 42) {
    name
    orders(last: 3) { id total }
  }
}

// gRPC: a typed method defined in a .proto file
service UserService {
  rpc GetUser (GetUserRequest) returns (User);
  rpc WatchOrders (WatchOrdersRequest) returns (stream Order); // server streaming
}

message GetUserRequest { int64 id = 1; }
message User {
  int64 id = 1;
  string name = 2;
}

REST spreads data across resource URLs. GraphQL lets the client shape one response. gRPC defines methods and messages up front; the numbers (= 1, = 2) are field tags used in the binary encoding.

How it works

REST leans on HTTP itself: methods carry meaning (GET is safe, PUT and DELETE are idempotent), status codes report outcomes, and GET responses can be cached by browsers, CDNs and proxies using standard headers.

GraphQL servers parse each query, validate it against the schema, and run a resolver function per field. Because a query can nest (users, then each user's orders, then each order's items), naive resolvers cause the N+1 query problem; batching tools like DataLoader collect lookups into one query. Most queries are sent as POST to a single URL, so standard HTTP caching doesn't apply without extra work (persisted queries, client-side normalized caches).

gRPC compiles a .proto contract into stubs in many languages. Requests are Protocol Buffers: binary, smaller and faster to parse than JSON, with fields identified by number so old and new versions can coexist if you never reuse a field number. HTTP/2 multiplexes many calls over one connection and enables four call types: unary, server streaming, client streaming and bidirectional streaming. Browsers can't speak native gRPC directly; they need gRPC-Web plus a proxy, which is why gRPC is mostly used between backend services.

REST               GraphQL               gRPC
Transport     HTTP/1.1 or 2      HTTP (usually POST)   HTTP/2
Payload       JSON (usually)     JSON                  Protobuf (binary)
Contract      OpenAPI (optional) Schema (required)     .proto (required)
Endpoints     many URLs          one URL               methods on services
Caching       HTTP caches work   needs extra work      app-level only
Streaming     no (use SSE/WS)    subscriptions         4 built-in modes
Browser use   native             native                needs gRPC-Web proxy
Best fit      public APIs        many varied clients   internal services

Why does it exist?

REST became popular because it reuses the web's existing machinery: URLs, methods, status codes and caches. As apps grew, mobile clients suffered from over-fetching (getting fields they don't need) and under-fetching (needing several round trips), which GraphQL was built to fix. Inside large backends, thousands of services calling each other needed lower overhead, strict contracts and streaming, which is what gRPC provides.

When to use it

- REST: public APIs, simple CRUD, anything that benefits from HTTP caching and from being easy to call with curl. - GraphQL: a product with several clients (web, iOS, Android) that each need different views of the same data, or a gateway that stitches together many backend services. - gRPC: internal service-to-service calls where latency, payload size, strict typing and streaming matter, especially across many languages.

When not to use it

Don't adopt GraphQL for a small API with one client; you take on schema design, resolver performance and query-cost limits for little gain. Don't expose gRPC directly to browsers or third parties who expect JSON over HTTP. Don't force REST onto workflows that are really actions or streams (for example, live updates), where RPC or a realtime channel fits better.

Common mistakes

  • Adopting GraphQL without batching (DataLoader), so one nested query fires hundreds of database queries.

  • Leaving a public GraphQL endpoint without depth or cost limits, so one deeply nested query can overload the server.

  • Reusing or renumbering protobuf field numbers, which silently corrupts data for clients on the old schema.

  • Assuming gRPC works in the browser like fetch does, without planning for gRPC-Web and a proxy.

  • Treating REST as 'JSON over HTTP' and ignoring status codes, idempotent methods and cache headers.

Practice exercises

  1. Easy:

    For a mobile profile screen that shows a user's name, avatar and last three orders, count the REST calls you'd need versus GraphQL queries.

  2. Medium:

    Write a .proto file for a chat service with a unary SendMessage method and a server-streaming Subscribe method. Explain how you'd add a new field later without breaking old clients.

  3. Hard:

    Design the API layer for a company with a public developer API, a web app, two mobile apps and 40 internal microservices. Which style do you use where, and why?

Interview questions

What problem does GraphQL solve that REST struggles with?

Over-fetching and under-fetching. A REST endpoint returns a fixed shape, so clients either get unused fields or need several round trips. GraphQL lets the client ask for exactly the fields it needs, across related objects, in one request.

What makes gRPC faster than typical REST + JSON?

Protocol Buffers are a compact binary encoding that is smaller and cheaper to parse than JSON, and HTTP/2 multiplexes many concurrent calls over one long-lived connection with header compression. Generated stubs also remove hand-written serialization code.

What are gRPC's four call types?

Unary (one request, one response), server streaming (one request, a stream of responses), client streaming (a stream of requests, one response) and bidirectional streaming (both sides stream independently over one call).

Why is caching harder with GraphQL than with REST?

REST GET requests have stable URLs, so browsers and CDNs can cache them with standard headers. GraphQL usually sends varied queries as POST to one URL, so HTTP caches can't key on them. Teams use persisted queries (sent as GET with a hash) or client-side normalized caches instead.

What is the N+1 problem in GraphQL and how is it fixed?

Resolving a list of N items, then running one more query per item for a nested field: 1 + N queries. The usual fix is a batching loader (like DataLoader) that collects all the keys requested during one tick and loads them in a single query.

How do you evolve a protobuf schema without breaking clients?

Add new fields with new field numbers; old readers ignore unknown fields. Never change a field's number or type, and when removing a field, mark its number (and name) as reserved so it can't be reused.

Can a browser call a gRPC service directly?

Not with native gRPC, because browsers don't expose the low-level HTTP/2 control (such as trailers) it needs. You use gRPC-Web, which needs a proxy (such as Envoy) or server support to translate, or expose a REST/JSON gateway instead.

How would you protect a public GraphQL API from expensive queries?

Limit query depth and compute a cost score per query, reject anything above a budget, rate-limit by cost rather than request count, add timeouts, and for first-party clients allow only persisted (pre-registered) queries.

How does versioning typically differ across the three styles?

REST often versions the URL or a header (/v2/users). GraphQL usually avoids versions: you add fields and deprecate old ones in the schema. gRPC relies on protobuf's backward-compatible field rules, with a new package or service name only for breaking changes.

A team wants to use gRPC everywhere, including the public API. What would you advise?

Use gRPC internally where it shines, but expose REST/JSON (or GraphQL) publicly. Third parties expect plain HTTP tools, browsers need gRPC-Web, and debugging binary payloads is harder. A gateway can translate REST to gRPC (for example, gRPC-Gateway or Envoy transcoding) so you keep one contract.