Learning on Web Dev Open is free for all.

Backend Engineering > Designing an API someone else has to useREST, RPC and GraphQL, on constraints
Phase 05Designing an API someone else has to use247 of 434

REST, RPC and GraphQL, on constraints

Three shapes, three sets of tradeoffs, and a decision that should come from your clients and your data rather than from what is fashionable this year.

Concept16 minAI adversary

REST is resource-shaped. It suits domains where nouns are stable and clients want the same things repeatedly, and it gets you HTTP caching, conditional requests and a URL space people can explore for free. Its cost is round trips and over-fetching: a screen that needs a user, their orders and each order status is three or four calls that nobody can collapse.

RPC, including tRPC and gRPC, is verb-shaped. You call a function, typed end to end, and there is no debate about whether cancelling an order is a DELETE or a POST. It suits an internal service or a single-team full-stack app where client and server ship together. The cost is that you lose the free HTTP layer and you have coupled a client build to a server build.

GraphQL lets the client specify the shape of the response, which is genuinely the right answer when you have many clients with different screens and a deeply connected graph. The cost is real and often underestimated: caching becomes yours to solve rather than the CDN's, arbitrary queries make performance unpredictable, and you will implement dataloader batching to fight the N+1 that the flexibility creates. Choose it when you have the problem it solves, and not before.

You should now be able to

  • State what problem each style is good at
  • Choose a style from stated constraints and defend it
  • Name the specific cost of each choice
Ask the community

Loading…