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
Phone appAPI server
  • Request
  • Response
  • Failed request
Step 1/10

Step 1 of 10: A client asks, a server answers

Read every step as text
  1. 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.

  2. Step 2: REST: nouns in the URL, verbs in the method

    In REST every thing is a resource with its own URL: /users/42 is one user, /users/42/orders is that user's orders. The HTTP method says what to do: GET reads, POST creates, PUT replaces, DELETE removes.

    Every request carries everything the server needs (it is stateless), and a GET changes 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.

  3. Step 3: The status code says how it went

    Each response starts with a status code. 2xx means success, 4xx means the client made a mistake, 5xx means the server failed.

    Clients and tools read the code first: 404 means "no such resource", 429 means "slow down", and a 5xx is often safe to retry later.

    Do not return 200 with an error hidden in the body. Monitoring and caches look at the status code.

  4. Step 4: Too much data, and a second trip

    With plain REST the app calls GET /users/42 first: it returns the whole user record (3 KB) including the order ids. Only then can it call GET /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.

  5. 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.

  6. Step 6: gRPC: small binary messages and streams

    gRPC is a popular RPC system. You describe methods and messages in a .proto file, 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.

  7. 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.

  8. 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 ms on 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.

  9. 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.

  10. 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.