SkillSprout
Chapter 4 · Lesson 2
Intermediate
8 min read

REST vs RPC (gRPC)

Overview

Two services have to talk. One option is a REST API over HTTP with JSON, which every browser and developer understands. The other is RPC, where a client calls a function on a remote server, and gRPC is the most popular modern version. This lesson explains what each is good at, the numbers that decide between them, and the common answer: REST at the edge for the outside world, gRPC between internal services.

Detailed Notes

What Problem Are We Solving?

Services need an agreed way to ask each other for things. That agreement is the API contract: which operations exist, what data goes in, what comes out and how errors are reported. Two styles compete.

  • REST (from the clients-servers-APIs lesson) models the world as resources named by URLs, such as /orders/42, and uses HTTP methods (GET, POST, PUT, DELETE) and status codes. Payloads are usually JSON, a human-readable text format.
  • RPC (remote procedure call) models the world as functions. The client calls CreateOrder(request) and the framework hides the network. gRPC is Google's open RPC framework. You describe operations and messages in a file, and tools generate client and server code for many languages. Messages use protobuf (Protocol Buffers), a compact binary format, and travel over HTTP/2, a version of HTTP that sends many requests at once over a single connection.

The question is not which is more modern. It is who calls you, how often and how much you care about bytes and speed.

A feel for the numbers:

text
The same order message
  JSON (text, field names repeated):  about 300 bytes
  Protobuf (binary, field numbers):   about 100 bytes

Internal traffic: 50,000 calls per second
  Saved per call: 200 bytes
  50,000 x 200 B = 10 MB/s less on the wire, plus less CPU
  spent turning text into objects and back

For one public endpoint at 50 requests per second that saving is irrelevant. For hundreds of internal calls per user request, it adds up.

REST vs gRPC
Comparison

REST + JSON

resources over HTTP

  1. 1

    Client builds a URL and JSON body

    POST /v1/orders

  2. 2

    HTTP request over TLS

    often one request per connection use

  3. 3

    Server parses JSON text

    about 300 bytes

  4. 4

    Status code and JSON reply

    201 Created

  • Works in every browser and tool
  • HTTP caching and CDNs
  • Easy to read and debug
  • Larger payloads, more parsing
  • Contract is informal

Best for public and partner APIs

gRPC + protobuf

functions over HTTP/2

  1. 1

    Call generated client method

    payment.Charge(req)

  2. 2

    Serialise to protobuf

    about 100 bytes

  3. 3

    Send on shared HTTP/2 connection

    multiplexed, kept open

  4. 4

    Typed reply or status

    deadline carried along

  • Compact and fast
  • Compile-time contract
  • Streaming and deadlines
  • Needs a proxy for browsers
  • Binary, harder to debug

Best for internal service calls

Key point. REST optimises for reach. gRPC optimises for efficiency between systems you control.

Same operation, two wire formats.

Option 1: REST (HTTP + JSON)

How it works

The client sends a normal HTTP request and receives a status code and a JSON body.

http
POST /v1/orders
Content-Type: application/json

{ "customer_id": "c_77", "items": [ { "sku": "p_1", "qty": 2 } ] }
http
HTTP/1.1 201 Created
Location: /v1/orders/o_9181

The server is stateless (each request carries everything it needs), so it scales behind a load balancer. GET responses can be cached by browsers, CDNs and proxies because the URL identifies the data. Anyone can try the API with curl or a browser.

Pros

  • Universal. Every language, browser and tool speaks HTTP and JSON. Partners can integrate without installing anything.
  • Easy to debug. You can read requests and responses by eye.
  • Cache-friendly. HTTP caching, ETags and CDNs work out of the box.
  • Loose coupling. A client only needs the documentation, not generated code.

Cons

  • Bigger and slower. Text payloads with repeated field names and parsing cost.
  • Contract is informal. An OpenAPI file helps, but nothing forces client and server to agree, so mismatches show up at runtime.
  • Awkward for actions. Not everything is a resource: "retry this payment" or "recalculate prices" does not map cleanly to GET/POST on a noun.
  • Chatty. Fetching a screen's data may need several round trips, or a custom endpoint per screen.

When to use it

Choose REST when the caller is outside your control (browsers, mobile apps, partners), the API is public and long-lived, or cacheability and easy debugging matter more than the last millisecond.

Realistic scenario. A food delivery company exposes menus and orders to restaurant partners. Hundreds of small integrators write code in PHP, Python and Java. REST with JSON and a published OpenAPI document lets each of them integrate in an afternoon, and GET /restaurants/12/menu is cached by a CDN.

Option 2: RPC with gRPC

How it works

You first write the contract in a .proto file:

text
service PaymentService {
  rpc Charge (ChargeRequest) returns (ChargeReply);
}
message ChargeRequest {
  string order_id = 1;
  int64  amount_cents = 2;
}

A compiler generates client and server code. Calling the service looks like a local function call: payment.Charge(request). Under the hood the request is serialised to protobuf and sent over a long-lived HTTP/2 connection, where many calls share one connection (multiplexing), so there is no new TCP and TLS handshake per call. gRPC also supports streaming: one request can return a stream of replies, or both sides can stream.

Field numbers (1, 2, ...) let you add fields later without breaking old clients, as long as you never reuse a number.

Pros

  • Fast and compact. Binary messages, reused connections, lower CPU and latency.
  • Strict contract. Generated code catches mismatches at compile time.
  • Streaming and deadlines built in. Each call can carry a deadline that is passed on to downstream calls.
  • Great for polyglot internal systems. One .proto yields Go, Java and Python clients.

Cons

  • Not browser-native. Browsers cannot speak plain gRPC, so you need a proxy layer (gRPC-Web) or a REST gateway.
  • Harder to debug. Binary payloads need special tools.
  • No free HTTP caching. CDNs and intermediaries do not understand gRPC calls.
  • Tighter coupling. Clients depend on generated code and the schema file, so changes need coordination.
  • Load balancing is trickier. A long-lived HTTP/2 connection can pin all calls to one server, so you need a load balancer that understands requests, not just connections (layer 7, from the load-balancing lesson).

When to use it

Choose gRPC when both sides are your own services, call volume is high, latency matters, or you need streaming. Typical fit: service-to-service calls inside a data centre.

Realistic scenario. Opening a product page triggers 40 internal calls (pricing, stock, reviews, recommendations). At 5 ms of JSON parsing and connection overhead each, serial calls would cost 200 ms. Moving those internal hops to gRPC, with persistent connections and binary messages, cuts per-call overhead to about 1 ms and the page stays inside its latency budget.

The hybrid: REST at the edge, gRPC inside

The common design exposes REST (or GraphQL, a query language where the client picks fields) through an API gateway, a front door that handles authentication and routing. The gateway translates one external call into several internal gRPC calls. The outside gets simplicity and caching, the inside gets speed and strict contracts.

REST at the Edge, gRPC Inside
Request flow
Mobile appAPI gatewayOrder servicePayment service1POST /v1/orders (JSON)REST over HTTPS2authenticate, rate limit3CreateOrder (gRPC)protobuf over HTTP/24Charge (gRPC)idempotency key, 200 ms deadline5ChargeReply6CreateOrderReply7201 Created (JSON)
Key point. The outside world sees one simple API while the inside uses the efficient protocol.

The gateway turns one public REST call into internal gRPC calls.

Interview Discussion

State who the callers are first, then decide.

  1. Who calls it? Public or mobile clients push you to REST. Internal services open the door to gRPC.
  2. How hot is the path? If one user action triggers dozens of internal calls, protocol overhead multiplies.
  3. Do you need streaming? Server push or long-lived streams favour gRPC (or WebSocket at the edge, see the real-time lesson).
  4. How will you evolve the API? Mention versioning (/v1/) for REST and never reusing field numbers for protobuf.

Interview tip. Do not claim gRPC is "faster so always use it". Its win is small when the work behind the call (a database query, 20 ms) dominates the protocol overhead (0.5 ms).

Interview discussion. "Clients and partners get a REST API behind a gateway so it is cacheable and easy to adopt. Between our own services I'd use gRPC for compact messages, strict contracts and deadlines, because one page load fans out to dozens of internal calls."

Trade-off thinking. REST trades speed for reach and simplicity. gRPC trades reach and debuggability for speed and strictness. Pick per boundary, not per company. Also mention retries: whichever you pick, writes need idempotency keys (the retries lesson) so a retried Charge does not charge twice.

Final Takeaway

Choose REST when the caller is external, the API is public, or caching and easy debugging matter. Choose gRPC when both ends are yours, volume is high and you want compact messages, strict contracts, deadlines or streaming. Combine them with an API gateway: REST outside, gRPC inside. Whichever you pick, version the contract and make writes idempotent.

Common Interview Questions

Finished this lesson?

0 of 44 lessons done · 0% of the course