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:
- The client stub serializes the request into a compact binary format (Protocol Buffers).
- It sends the bytes over HTTP/2 to the availability service.
- The server stub decodes them and calls your handler.
- 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
.protocontract. 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 likegrpcurland 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
.protofile 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.