Back to blog

REST, GraphQL or gRPC: how should the app talk to the server?

Thắng Đoàn
Thắng Đoàn

FotoFlow's app has one endpoint per screen, and the newest phone screen needs the user's name, their last three photos and one notification count. At 200,000 users the API team ships a new version of that endpoint every week, each a little heavier than the last. The phone waits.

The phone pays for every byte

A phone on a mediocre network pays about 100 ms per round trip, and every field in a response costs bytes. The profile endpoint returns 48 KB of user data because it was built for the web dashboard; the phone uses 3 KB of it. Every extra byte the phone downloads and throws away was paid for twice: in wait time and in data cost.

Under-fetching is the mirror problem: the screen needs data the response does not have, so the phone makes another call, then another. Round trips are the budget, not bandwidth.

REST: resources, not screens

REST models things, not screens: /users/42, /photos, one URL per resource, verbs that mean the same thing everywhere. GET is safe to repeat, PUT and DELETE are idempotent, so a phone that retries after a dropped connection does not accidentally like a photo twice.

The cost is rigidity. Each screen still gets whatever the endpoint returns, and the pagination that worked at 10,000 rows walks 40,000 rows to reach page 400. A cursor fixes that: "after photo 9f3c" jumps down the index instead of skipping, and a row inserted mid-scroll cannot make the phone skip or repeat it.

GraphQL: the client asks

GraphQL flips the contract. One endpoint, and each screen asks for exactly its fields: name, three photos, count. No second call, no 45 KB of dead weight. GraphQL lets the client ask for exactly the shape it needs, but a flexible query is a query you did not test.

A client can also ask for things that are expensive: followers of followers of followers is one innocent-looking query that walks the whole graph. Production GraphQL needs depth limits, cost limits per field, and persisted queries so only reviewed queries run.

gRPC: services talking to services

Inside the backend, services call each other in microseconds, thousands of times a second. gRPC sends compact protobuf messages over HTTP/2, with streaming both ways and deadlines that flow automatically from the caller to everything downstream. Browsers do not speak it natively, so it shines service to service, not app to server.

The same protobuf contract also gives you typed clients in every language for free, which is why teams with many internal services reach for it.

What this does not solve

No style survives a breaking change. Renaming name to display_name in the response crashes every app version already installed on phones that do not auto-update. Never rename a field: add the new one, migrate, remove the old one years later. Old apps keep calling old URLs for years, so API versions are additive: new fields, new endpoints, /v2/ only when the old shape truly cannot bend.

Your most-used endpoint returns 48 KB and the average screen uses 3 KB. What is that gap made of?
A field you renamed two years ago is still on thousands of phones. What would removing it break?

The API now speaks efficiently to phones and services. Next in the series: Photo resizes pile up: what takes the load off your API?

Sources

REST: architectural style

GraphQL

gRPC

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.