Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 39 ยท Getting Started

gRPC vs REST and GraphQL

Compare gRPC with REST and GraphQL: contracts, payload size, streaming, browser support, caching and tooling, and learn when each style is the right choice.

gRPC and REST both let one program talk to another over a network, but they make different trade-offs. REST models your system as resources reached through URLs and HTTP verbs. gRPC models it as methods on services, with a typed contract. Neither is better in every situation; the right choice depends on who your callers are and what they need.

This lesson compares them side by side, shows the same operation in both styles, and ends with a decision guide. If you need the basics first, start with what gRPC is.

The same operation, two styles

Imagine fetching a book by its id.

HTTP
GET /v1/books/42 HTTP/1.1
Accept: application/json
JSON
{ "id": 42, "title": "Dune", "author": "Frank Herbert" }

The gRPC version is a method call defined in a contract (read-only real code):

PROTO
service Library {
  rpc GetBook(GetBookRequest) returns (Book);
}

message GetBookRequest { int64 id = 1; }
message Book {
  int64 id = 1;
  string title = 2;
  string author = 3;
}

On the wire the gRPC request is an HTTP/2 POST to /library.Library/GetBook whose body is a compact binary message. The URL is always /package.Service/Method, and the verb is always POST.

Side-by-side comparison

Topic REST (JSON over HTTP) gRPC
Model Resources and verbs Services and methods
Contract Optional (OpenAPI) Required (.proto)
Payload Text JSON Binary protobuf
Transport HTTP/1.1 or HTTP/2 HTTP/2 only
Streaming Awkward (SSE, WebSockets) Built in, four call types
Code generation Optional Standard
Browser Native Needs gRPC-Web or a gateway
Caching HTTP caches, CDNs Little built-in caching
Debugging curl, browser grpcurl, special tools

Stand-in: measuring a payload difference

This stand-in does not use real protobuf. It encodes a list of items as JSON and as a simple length-prefixed binary layout so you can see why binary formats win on list-heavy responses. The exact ratio for real protobuf depends on the data.

Go Stand-in: JSON versus binary for a list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

JSON repeats every field name for every item. A schema-based binary format knows the names from the contract, so it only sends values.

Typed contracts versus loose documents

In REST, nothing stops a server from renaming author to writer. Old clients keep running and quietly receive null. With gRPC, the field has a number and a type in a shared file. Renaming is safe because the wire uses the number, and changing a type is rejected by tooling. We cover safe evolution in a later chapter listed on the track page.

Streaming

REST over plain HTTP is request-response. To push updates you add Server-Sent Events or WebSockets, each with its own conventions. gRPC defines four call types in the contract itself:

  1. Unary: one request, one response.
  2. Server streaming: one request, many responses.
  3. Client streaming: many requests, one response.
  4. Bidirectional streaming: both sides send independently.

You will meet each in the streaming chapter.

GraphQL in one paragraph

GraphQL lets the client choose exactly which fields it wants in one request, which suits mobile and front-end apps that aggregate many backends. gRPC lets the server define fixed methods, which suits efficient backend-to-backend calls. Many companies use GraphQL or REST at the edge and gRPC inside the data center.

Stand-in: a gateway that translates REST to RPC

Browsers and third parties usually speak REST, while internal services speak gRPC. A gateway sits in between, translating. This stand-in routes HTTP-style requests to RPC-style method names, using net/http/httptest so that no port is opened.

Go Stand-in: REST-to-RPC gateway

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The real tools for this job are grpc-gateway and Envoy's gRPC-JSON transcoder; see the gateways chapter listed on the track page.

A decision guide

  • Choose gRPC for internal microservices, polyglot teams, low latency, streaming, and strict contracts.
  • Choose REST for public APIs, browser-first products, cacheable content and when simple tooling matters most.
  • Choose both: expose REST or GraphQL to the outside and gRPC inside.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which HTTP method does a gRPC call use on the wire?

    gRPC maps every call to an HTTP/2 POST to /package.Service/Method.
  2. 2. Which is a genuine advantage of REST over gRPC?

    Browsers and CDNs understand URLs and verbs directly. gRPC needs gRPC-Web or a gateway.
  3. 3. Why are protobuf payloads usually smaller than JSON?

    JSON repeats every key as text. Protobuf sends compact field numbers.
  4. 4. A common architecture is:

    Edge clients want browser-friendly APIs; internal services benefit from gRPC's speed and contracts.

Summary

  • REST is resource-oriented and browser-friendly; gRPC is method-oriented, typed and efficient.
  • gRPC contracts make breaking changes visible at build time.
  • gRPC has four call types, including built-in streaming.
  • Gateways translate between REST/JSON and gRPC.
  • Pick by audience: public and browser-heavy favors REST, internal and performance-critical favors gRPC.

Next: How gRPC Uses HTTP/2