REST vs RPC vs GraphQL
Three ways for programs to talk over a network: REST (resources over HTTP), RPC such as gRPC (calling remote functions), and GraphQL (asking for exactly the data you need). What each costs, and where each fits.
Overview
Every system design has boundaries where one program calls another: a phone app calling the backend, a browser calling an API, one service calling another. At each boundary you pick a style for the conversation. The three you will be asked about are REST, RPC and GraphQL.
- REST models the API as resources (orders, users) addressed by URLs and changed with standard HTTP methods.
- RPC (remote procedure call) models the API as functions you call on another machine, such as GetUser or PlaceOrder. gRPC is the most common modern RPC framework.
- GraphQL exposes a typed graph of data and lets the client send a query that names exactly the fields it wants.
No style is best everywhere. REST fits the web and HTTP caching; gRPC fits fast, typed calls between services; GraphQL fits screens that need data from many places in one round trip. Many companies use all three, each at a different boundary.
What you will be able to do
- Explain the model behind each style, and what it makes easy and hard.
- Read a gRPC service definition and name its four kinds of method.
- Evolve a protobuf, REST or GraphQL contract without breaking clients.
- Spot GraphQL's N+1 and query-cost problems and fix them.
- Choose a style for each boundary in a design and defend it.
Key concepts
- Resources vs procedures
- REST constraints
- HTTP caching
- gRPC and Protocol Buffers
- Streaming RPCs
- Schema evolution
- GraphQL queries
- N+1 and DataLoader
- Query cost limits
Interview questions
- When do you choose REST, gRPC or GraphQL?
- Why is gRPC often faster than REST with JSON?
- How do you change a protobuf message without breaking anyone?
- What is the N+1 problem in GraphQL, and how do you fix it?
- How do you stop a GraphQL API from being overloaded by expensive queries?
- Can a browser call a gRPC service?
- What does REST actually require, and does it matter that most APIs are not fully RESTful?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio