04. Communication / Lesson 1 of 4
REST, RPC & GraphQL
Three ways for a client and a server to talk, and what each one costs.
- 10 min
- Beginner
- 10 steps
- Round trips—
- Data received—
- Screen time80 ms
- Request
- Response
- Failed request
Step 1 of 10: A client asks, a server answers
Read every step as text
Step 1: A client asks, a server answers
A phone app and a server talk over HTTP: the client sends a request, the server sends back a response. Each pair is one round trip.
We will fetch one screen: the user's name and the titles of their last 20 orders. On a mobile network a round trip costs about 80 ms, so the number of trips matters. The question is how to shape the requests.
REST, RPC and GraphQL are three styles for shaping that request and response. All three usually travel over HTTP.
Step 2: REST: nouns in the URL, verbs in the method
In REST every thing is a resource with its own URL:
/users/42is one user,/users/42/ordersis that user's orders. The HTTP method says what to do:GETreads,POSTcreates,PUTreplaces,DELETEremoves.Every request carries everything the server needs (it is stateless), and a
GETchanges nothing, so caches, browsers and CDNs can store the answer safely.REST is easy to guess, easy to test with a browser or curl, and easy to cache. That is why most public APIs use it.
Step 3: The status code says how it went
Each response starts with a status code.
2xxmeans success,4xxmeans the client made a mistake,5xxmeans the server failed.Clients and tools read the code first:
404means "no such resource",429means "slow down", and a5xxis often safe to retry later.Do not return 200 with an error hidden in the body. Monitoring and caches look at the status code.
Step 4: Too much data, and a second trip
With plain REST the app calls
GET /users/42first: it returns the whole user record (3 KB) including the order ids. Only then can it callGET /users/42/orders(9 KB for 20 full orders).That is 2 round trips one after the other,
2 × 80 = 160 ms, and 12 KB downloaded when the screen needs about 1 KB. Too much data is over-fetching; needing a second call is under-fetching.Fixed endpoints return a fixed shape. Every screen that wants a different shape either wastes data or makes extra calls.
Step 5: RPC: call a named action
RPC (remote procedure call) makes a network call look like a function call:
GetProfileScreen(user_id). The name says the action, not a resource.The server can build exactly the answer one screen needs. Now it is 1 round trip, 80 ms, and about 1 KB of data.
The price: one method per screen. When the screen changes, the server method changes too.
Step 6: gRPC: small binary messages and streams
gRPC is a popular RPC system. You describe methods and messages in a
.protofile, and it makes code for both sides. Messages are sent in a compact binary format (Protocol Buffers) over HTTP/2.The message
{"id":42,"age":31}is 18 bytes as JSON text but about 4 bytes in Protobuf. gRPC can also stream: one call can carry many messages in either direction.gRPC is a good fit between your own services: fast, typed, and streams. Browsers cannot call it directly without a helper.
Step 7: GraphQL: the client chooses the fields
GraphQL has one endpoint. The client sends a query that names exactly the fields it wants, even across related things, and gets back a JSON answer with the same shape.
The same screen is again 1 round trip and about 1 KB, but this time the server did not need a new method. A different screen just sends a different query.
GraphQL suits many different clients (phone, web, watch) that each need a different slice of the same data.
Step 8: The hidden cost: N+1 queries
GraphQL fills each field with a small function called a resolver. If the order titles live in a products table, a naive resolver runs 1 query for the 20 orders and then 20 more, one per order: 21 queries.
At 2 ms each that is
21 × 2 = 42 mson the server, so the screen takes 122 ms. Batching the lookups (a DataLoader) asks for all 20 products at once: 2 queries, 84 ms in total.The client sees one fast request, but the database pays for the hidden loop. Watch query counts, and limit how deep a query may go.
Step 9: Which one should you pick?
REST is the default for public APIs: simple, cacheable, understood by every tool. gRPC fits fast calls between your own services. GraphQL fits apps with many clients that need different data shapes.
They mix well: a common design is GraphQL or REST at the edge for apps, and gRPC between the services behind it.
Pick by who calls you. Strangers and browsers: REST. Your own services: gRPC. Many different screens: GraphQL.
Step 10: Three styles in one picture
The same screen cost 2 trips and 12 KB with fixed REST endpoints, and 1 trip and about 1 KB with a tailored RPC call or a GraphQL query.
REST names resources and uses status codes. RPC and gRPC call actions with compact messages. GraphQL lets the client pick fields, but needs care with N+1 queries.