Three popular API styles, and how to choose between them.
REST, GraphQL and gRPC are three popular ways to build an API. Each one is best at something different.
REST (Representational State Transfer) treats everything as a resource with a URL. You use the standard HTTP method to say what to do, and the status code tells you how it went. The server keeps no memory of you between calls (stateless).
There is one endpoint. The client sends a query that lists the exact fields it wants, even across related things. The server returns exactly that shape. A schema defines what can be asked.
You write a .proto file that lists services and methods. Tools make client and server code in many languages. Messages are Protobuf (binary) and travel over HTTP/2, which also allows streaming.
Different clients and different jobs need different things. There are three big problems that these styles try to solve:
| Style | Used a lot for | Examples (general idea) |
|---|---|---|
| REST | Public APIs, CRUD apps, simple back-ends | Payment, maps and cloud provider APIs |
| GraphQL | Apps with many screens and mobile clients, aggregating many back-ends | Social and e-commerce front-ends; GitHub offers a GraphQL API |
| gRPC | Service-to-service calls, streaming, low latency | Microservices inside a company; Kubernetes and etcd use it |
GET /users/7 β read user 7 200 OK GET /users/7/posts?limit=3 β read his latest 3 posts 200 OK POST /users β create a user 201 Created PUT /users/7 β replace user 7 200 OK PATCH /users/7 β change part of user 7 200 OK DELETE /users/7 β remove user 7 204 No Content
/orders), verbs in HTTP methods.?limit=20&cursor=abc (cursor-based scales better than page numbers on big data)./v1/β¦ or a header, so you can change without breaking clients.POST /graphql
{
user(id: 7) {
name
avatar
posts(first: 3) { title }
}
}
β { "data": { "user": { "name": "Asha", "avatar": "β¦", "posts": [ {"title":"Hi"}, β¦ ] } } }200 OK, with an errors list inside the body when something fails.service ProfileService {
rpc GetProfile (ProfileRequest) returns (ProfileReply); // unary
rpc WatchPosts (ProfileRequest) returns (stream Post); // server stream
rpc UploadPhotos (stream Photo) returns (UploadSummary); // client stream
rpc Chat (stream Msg) returns (stream Msg); // two-way
}| REST | GraphQL | gRPC | |
|---|---|---|---|
| Shape | Resources + HTTP methods | One endpoint + a query | Methods (RPC) |
| Data format | JSON (mostly) | JSON | Protobuf (binary) |
| Transport | HTTP/1.1 or 2 | HTTP (usually POST) | HTTP/2 |
| Contract | OpenAPI (optional) | Schema (required) | .proto (required) |
| Over/under-fetching | Common | Solved by the client query | You design each method |
| Caching | Easy (HTTP, CDN) | Harder | Custom |
| Streaming | No (use SSE / WebSockets) | Subscriptions | Yes, built in |
| Browser support | Perfect | Perfect | Needs gRPC-Web |
| Learning curve | Low | Medium | Medium |
| Best for | Public APIs, simple CRUD | Varied clients and screens | Internal, fast, typed calls |
The screen needs: name, avatar, and for each of 3 posts: title
Numbers are rounded examples to show the idea. Real sizes depend on your data and on compression.
/users, /users/7/orders.| Term | Meaning in easy words |
|---|---|
| Resource | A βthingβ in REST with its own URL, like a user or an order |
| CRUD | Create, Read, Update, Delete: the four basic actions on data |
| Stateless | The server keeps no memory of the client between requests |
| Over-fetching | Getting more data than the screen needs |
| Under-fetching | Needing more than one call to get what the screen needs |
| Query / Mutation | GraphQL ways to read / change data |
| Schema | The list of types and fields an API supports |
| Resolver | The GraphQL function that fetches the data for one field |
| N+1 problem | One query for a list plus one extra query per item: too many database calls |
| RPC | Remote Procedure Call: calling a function on another machine as if it were local |
| Protobuf | A compact binary format used by gRPC, defined in .proto files |
| HTTP/2 | A newer HTTP version that sends many requests on one connection and supports streams |
| OpenAPI | A standard way to describe REST APIs in a file |
| Style | Pros | Cons |
|---|---|---|
| REST | Simple, universal, easy to cache, easy to debug, huge tooling | Over/under-fetching, many round trips, no built-in streaming |
| GraphQL | One request, exact data, strong schema, fast front-end work, good for aggregating many back-ends | Harder caching, N+1 risk, costly queries need limits, more server work |
| gRPC | Fast, small, typed, streaming, code generation, deadlines | Not human readable, browsers need a gateway, harder to debug by hand, less suited to public APIs |
/getUser, /deleteOrder) instead of nouns and methods.