Compare

REST vs GraphQL

RESTA URL for each resource

Order #981

Home · Indiranagar, BLR-07

Your items

CDNat the edge
API gatewayroutes paths
  • Order
  • Rider
  • Catalog
  • Store
    GraphQLOne query for the whole screen

    Order #981

    Home · Indiranagar, BLR-07

    Your items

    CDNat the edge
    GraphQL serverone endpoint
    • Order
    • Rider
    • Catalog
    • Store

      One Zepto tracking screen, fetched two ways: a call per resource, or one query for the whole screen. Then the screen changes, and a thousand shoppers arrive. Illustrative sizes and times: a round trip over the mobile network is about 150 ms.

      REST or GraphQL?

      REST for simple resources and anything a CDN should cache: public APIs, files, webhooks. GraphQL when many screens need different slices of the same data — one round trip, exactly the fields. Plenty of companies run both.

      DifferenceRESTGraphQL
      One screenOne call per resource: 3 round tripsOne query: 1 round trip
      What comes backWhole resources, used or notExactly the fields asked for
      A new field on screenAnother call, or a new endpointOne more line in the query
      CDN cachingBuilt in: a GET has an addressNeeds persisted queries
      ErrorsHTTP status codesA 200, with an errors list
      Best forPublic APIs, files, webhooksApps with many screens and clients

      REST vs GraphQL, explained step by step

      1. Load the screen

      One Zepto shopper opens the tracking screen: the order, its items, and the rider bringing it. On the left, REST: the app calls one address for each thing it needs. On the right, GraphQL: the app sends one query that describes the whole screen. Let’s put both through three situations.

      First, loading the screen. With REST, the app asks for the order, and gets all of it back, including the rider’s ID.

      Only then can it ask for the rider, and then for the items.

      Three round trips over the mobile network, and thirteen kilobytes, for a screen that shows nine fields.

      With GraphQL, the app sends one query naming those nine fields.

      The GraphQL server fetches from the order, rider and catalog services, short hops inside the data centre, and sends back one answer with exactly those fields.

      One round trip, under a kilobyte.

      2. The screen changes

      Next, the screen changes: the team adds the store’s name.

      The order doesn’t include it, so the app makes a fourth call, to the store.

      Or the backend team builds a new endpoint for this screen, and the app waits for it.

      With GraphQL, the app adds one line to its query: the store’s name.

      The server already knows how to fetch a store, so nothing changes there, and it’s still one round trip.

      3. A thousand shoppers

      Finally, a thousand shoppers open the same deals list.

      A REST request is a plain GET with an address, so the CDN keeps the answer: the first request reaches the server, and the other nine hundred and ninety-nine are answered at the edge.

      GraphQL sends every query as a POST to the same address, with the question inside.

      The CDN can’t tell them apart, so all thousand reach the GraphQL server.

      Persisted queries fix this, by sending a short ID as a GET, but that’s extra work to set up.

      In short

      So which should you pick? REST for simple resources and anything a CDN should cache, like public APIs and files. GraphQL when many screens need different slices of the same data: one round trip, exactly the fields, and screens change without new endpoints. Plenty of companies run both.