bigonotes
System DesignAPI Design

RPC

How RPC frameworks like gRPC model APIs as remote function calls, and when to use them for internal services.

3 min readLast updated

RPC (Remote Procedure Call) makes a call to another service look like a call to a local function. Instead of thinking in resources and HTTP methods, you think in actions: checkAvailability(restaurantId, date, party), chargeCard(bookingId, amount). The framework handles turning the call into bytes, sending it over the network and turning the reply back into a return value. gRPC is the most common framework today, and it's the usual choice for calls between internal services.

How it works#

You define the service once, in an interface file, and generate client and server code from it in any language.

syntax = "proto3";

service Availability {
  rpc CheckAvailability (AvailabilityRequest) returns (AvailabilityReply);
}

message AvailabilityRequest {
  string restaurant_id = 1;
  string date = 2;
  int32 party = 3;
}

message AvailabilityReply {
  repeated string free_slots = 1;
}

The booking service then calls CheckAvailability on a generated client stub as if it were a local method. Under the hood:

  1. The client stub serializes the request into a compact binary format (Protocol Buffers).
  2. It sends the bytes over HTTP/2 to the availability service.
  3. The server stub decodes them and calls your handler.
  4. The reply travels back the same way and comes out as a typed object.

Why gRPC is fast#

Three choices make gRPC lighter than JSON over HTTP/1.1:

Choice Effect
Protocol Buffers Binary messages are several times smaller than JSON and faster to encode and decode
HTTP/2 Many calls share one long-lived connection, so there's no new handshake per request
Generated, typed stubs Both sides agree on the message shape at compile time, so mismatches show up as build errors, not runtime bugs

It also supports streaming in either or both directions on one connection, which suits feeds, uploads in chunks and live updates between services.

RPC vs REST#

REST RPC (gRPC)
Model Resources and HTTP methods Actions, as function calls
Format JSON text, human-readable Protocol Buffers binary, needs tools to read
Contract Optional (OpenAPI) Required .proto file, with generated code
Browser support Native Needs a proxy such as gRPC-Web
Caching HTTP caching works out of the box No HTTP caching; cache in the service
Best for Public APIs, web and mobile clients Internal service-to-service calls, low latency, many languages

Costs and pitfalls#

  • A network call is not a local call. It can be slow, time out or fail halfway. Always set a deadline on each call, retry only idempotent calls, and add an idempotency key to the ones that change data.
  • Tight coupling. Clients depend on the exact .proto contract. Evolve it safely: add new fields with new numbers, never reuse or renumber an old field.
  • Harder to debug. You can't read binary messages in a browser or with plain curl. You need tools like grpcurl and good logging.
  • Not for browsers. Public web and mobile clients usually talk REST or GraphQL to a gateway, which calls gRPC services behind it.

In an interview#

Use REST for the public API. When the design has many internal services calling each other, say those internal links use gRPC: smaller messages, persistent connections and typed contracts. Mention a deadline on every call, since one slow service can otherwise hold up every service that calls it.

A good way to say it: "Clients hit the gateway over REST. Behind it, the booking service calls availability and payments over gRPC, because those calls are internal, frequent and latency-sensitive."

Key takeaways#

  • RPC models an API as remote function calls. gRPC is the common framework.
  • A .proto file defines the contract, and client and server code is generated from it.
  • Protocol Buffers and HTTP/2 make gRPC smaller and faster than JSON over HTTP/1.1, and it supports streaming.
  • Treat every call as a network call: deadlines, safe retries, idempotency keys.
  • REST outside, gRPC inside is a common, easy-to-defend split.
Revision notes · 1 min

Core idea#

RPC makes a call to another service look like a local function call. gRPC is the common framework and the usual choice between internal services.

Key points#

  • A .proto file defines services and messages. Client and server stubs are generated from it.
  • Protocol Buffers: binary, smaller and faster than JSON.
  • HTTP/2: many calls share one long-lived connection.
  • Supports streaming in one or both directions.
  • Browsers need a proxy (gRPC-Web), so public clients usually use REST or GraphQL.

RPC vs REST#

REST gRPC
Model Resources and methods Function calls
Format JSON Protocol Buffers
Contract Optional Required, generated code
Best for Public APIs Internal, low-latency calls

Pitfalls#

  • A network call can be slow or fail. Set a deadline on every call.
  • Retry only idempotent calls, or add an idempotency key.
  • Never reuse or renumber a field in the .proto file.
  • Binary messages are hard to debug without tools like grpcurl.

Say it in an interview#

  • "REST outside at the gateway, gRPC inside between services."
  • "Every internal call has a deadline, so one slow service can't stall the rest."

Found a mistake? Email ask@bigonotes.com