← Building blocks

Choose the API style

Networking & deliveryfundamental

External clients want simple HTTP + JSON; internal services want speed and typed contracts.

Say it like this

I expose REST over JSON to outside clients because every tool speaks it, and use gRPC with Protobuf between services for typed contracts, smaller payloads and streaming over one HTTP/2 connection.

Basic solution

REST / JSON for public APIs, gRPC with Protobuf between services, GraphQL when clients need flexible shapes.

RESTgRPCGraphQLOpenAPI

REST at the edge, gRPC inside

Many external callers speak REST to a gateway that validates and translates. Services talk to each other over multiplexed gRPC connections with generated, typed stubs.

How it works

  1. A REST API models each thing as a URL and uses HTTP verbs to act on it: GET /v1/urls/{alias} reads a link, POST /v1/urls creates one, DELETE removes one. The body is JSON, the status code carries the outcome (200, 404, 429), and because it is ordinary HTTP, caches, proxies and browsers understand it without any help.
  2. A gRPC API is defined in a .proto file that lists services, their methods, and the message types with numbered fields. A compiler generates client and server code for each language from that file, so calling a remote method looks like calling a local function with typed arguments.
  3. gRPC encodes messages as Protobuf, a compact binary format typically 3 to 10 times smaller than the same data as JSON, and sends them over HTTP/2. HTTP/2 lets many calls share one connection at the same time (multiplexing) and supports streaming in either direction, so a server can push a sequence of updates over one call.
  4. GraphQL puts a single endpoint in front of a typed schema. The client sends a query that names exactly the fields it wants, possibly spanning several related objects, and the server resolves them in one response. This removes the over-fetching that happens when a screen needs a few fields from each of five REST resources.
  5. In practice the edge speaks REST (or GraphQL) to browsers and mobile apps because every client can, an API gateway translates those calls, and services call each other with gRPC for the typed contract and the lower cost per call.

The property to name

  • REST buys reach and cacheability. gRPC buys contracts and efficiency. GraphQL buys client-driven shapes. Pick by who the caller is.

Worked example

Notification system internals

  • The router calls the preference service for every notification: 2,900/s average, 30K/s during a campaign.
  • A JSON response with channel flags, quiet hours and category settings is about 600 B; the Protobuf equivalent is about 120 B. At 30K/s that is 18 MB/s versus 3.6 MB/s on the internal network, and fewer bytes to parse per call.
  • Latency: a REST call over HTTP/1.1 with a new connection costs a TCP handshake per call unless pooled; gRPC keeps one HTTP/2 connection per client-server pair and multiplexes, so p50 drops from about 3 ms to under 1 ms in-region.
  • Contract: the preference message has field 7 "quiet_hours" added last quarter; old router binaries ignore it and new ones read it, with no coordination, because Protobuf fields are numbered and optional by default.
  • Public side: producers (orders, security) call POST /v1/notifications over REST because they are many teams with many languages, and the gateway validates JSON against the OpenAPI schema before forwarding as gRPC.

Variants and when each wins

REST with JSON

  • Resources as URLs, JSON bodies, standard status codes, readable by a human in a browser or with curl. It wins for any public API, because every language, CDN and proxy already speaks it; the costs are larger payloads and no contract beyond what an OpenAPI document adds.

gRPC with Protobuf

  • A strict schema compiled into client and server stubs, binary messages, and streaming over one HTTP/2 connection. It wins between your own services, where you control both ends and care about latency and typed contracts; it needs HTTP/2 end to end and a translating proxy (grpc-web or a gateway) to reach browsers.

GraphQL

  • One endpoint, a typed schema, and queries where the client chooses the fields. It wins for product screens that stitch together many resources with one client team controlling both sides; it needs query depth and cost limits, per-field authorisation, and caching is harder than with REST URLs.

Async messaging instead of RPC

  • The caller publishes an event and does not wait for a reply at all. It wins whenever the caller does not need an answer now, because it removes the coupling and the failure propagation entirely.

Recommendation: REST (or GraphQL for rich client apps) at the edge, gRPC between services, events where a reply is not needed.

Failure modes

Breaking a gRPC contract

  • After a deploy, old clients crash or read garbage out of a field. A field number in the .proto was reused for a different purpose, or a field's type was changed, so old binaries decode the new bytes with the old meaning. Never renumber or retype a field: add new fields with new numbers, mark old ones deprecated, and declare removed numbers as reserved so nobody can reuse them.

gRPC through a layer-7 proxy that is not HTTP/2 aware

  • Calls hang, or a streaming call never delivers anything. An HTTP/1.1 proxy or load balancer sits in the path and cannot carry HTTP/2 frames, so the gRPC stream is broken or buffered forever. Put an HTTP/2-capable proxy in the path, such as Envoy or a modern ALB, or terminate gRPC at the gateway and speak HTTP/1.1 only outside it.

Unbounded GraphQL queries

  • One request takes seconds and pins a CPU core. A deeply nested query, or a list of lists, expands into thousands of resolver calls. Enforce depth and complexity limits, allow only persisted (pre-registered) queries from production clients, and batch the per-item lookups with a DataLoader so a list does not become an N+1 query.

Chatty REST between services

  • A page is assembled from 20 sequential REST calls and its latency is the sum of them. The internal API was designed as one resource per call, which is fine for a browser but slow when a service needs many resources at once. Add a batch endpoint, move the hop to gRPC with one typed request that returns everything needed, or put a backend-for-frontend in front that aggregates on behalf of the page.

Misused status codes

  • Clients retry failures that can never succeed, or treat a failure as success. The server returns 200 with an error in the body, or 500 for a validation mistake the client made, so the client cannot tell retryable from permanent. Use 4xx for client errors, 5xx for server errors, and 429 with a Retry-After header for rate limits; gRPC status codes map onto the same categories.

Interviewer follow-ups

Answer out loud before you open each one.

How do you evolve a Protobuf message without breaking anyone?

Only add fields with new numbers, never change a type or reuse a number, and mark removed numbers reserved. Readers ignore unknown fields and treat missing ones as defaults, so old and new binaries interoperate. Semantic changes still need a new method or version.

Can a browser call gRPC directly?

Not natively, because browsers do not expose HTTP/2 framing. grpc-web runs through a proxy that translates, or the gateway exposes REST or GraphQL that it maps onto gRPC internally. Most teams do the latter and keep gRPC internal.

When is GraphQL the wrong choice?

For public APIs consumed by many unknown clients, where caching, rate limiting and simple tooling matter more than flexible shapes; and for write-heavy or streaming workloads. It shines for one product team controlling both client and schema.

What does HTTP/2 give gRPC that matters for latency?

One long-lived connection carries many concurrent calls as separate streams, so there is no per-call handshake and no head-of-line blocking at the HTTP layer. It also enables server and bidirectional streaming for things like location updates or progress events.

Connections

Pairs with
One front door for many services · Change an API without breaking clients · Split a monolith by domain · Batch small operations · Validate at the edge

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon API GatewayAPI gatewayAPI Gateway, ApigeeAPI ManagementKong, Tyk
Amazon EC2 Auto ScalingNotification router, Preference service, Template serviceCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad