← Back to Backend, page 1

backend-0008 · Easy fresher

How do you decide between gRPC and REST for a service, and how do gRPC status codes map to HTTP status codes?

Choose by who calls the API and what the call needs. Use REST (or GraphQL) for public APIs, browser clients and cacheable reads. Use gRPC for internal service-to-service calls, streaming, strict contracts across languages and low latency. Many systems use both: gRPC inside, REST at the edge through a gateway. gRPC has its own status codes, which map roughly to HTTP codes (NOT_FOUND to 404, INVALID_ARGUMENT to 400, UNAUTHENTICATED to 401).

Full answer

Decision questions

  1. Who are the clients? Browsers and third parties favour REST. Your own backend services favour gRPC.
  2. Do you need streaming or long-lived calls? gRPC supports it natively. REST needs SSE or WebSockets.
  3. How important is a strict, generated contract across languages? gRPC and protobuf are strong here.
  4. Do you need HTTP caching, simple debugging with curl, or human readable logs? REST is easier.
  5. How many teams and languages? A shared .proto repository scales well with many languages.

Mixed design

  • Public REST API at the edge (or a gRPC-JSON transcoding gateway such as Envoy), internal gRPC between services.

gRPC details to know

  • Errors are a status code plus a message and optional details, not an HTTP status. gRPC over HTTP/2 always returns HTTP 200 for the transport and puts the status in trailers.
  • Deadlines are sent with the call and should be propagated to downstream calls.
  • Client side or proxy-based load balancing is required, because one HTTP/2 connection carries many calls to one server.

Mapping (common): OK 200, INVALID_ARGUMENT 400, UNAUTHENTICATED 401, PERMISSION_DENIED 403, NOT_FOUND 404, ALREADY_EXISTS 409, FAILED_PRECONDITION 400 or 412, RESOURCE_EXHAUSTED 429, CANCELLED 499, INTERNAL 500, UNIMPLEMENTED 501, UNAVAILABLE 503, DEADLINE_EXCEEDED 504.

javascript
const mapping = {
  OK: 200, CANCELLED: 499, INVALID_ARGUMENT: 400, DEADLINE_EXCEEDED: 504, NOT_FOUND: 404, ALREADY_EXISTS: 409,
  PERMISSION_DENIED: 403, RESOURCE_EXHAUSTED: 429, FAILED_PRECONDITION: 400, UNIMPLEMENTED: 501, INTERNAL: 500,
  UNAVAILABLE: 503, UNAUTHENTICATED: 401,
};
const retryable = new Set(['UNAVAILABLE', 'DEADLINE_EXCEEDED', 'RESOURCE_EXHAUSTED']);

function recommend({ clients, streaming, needsHttpCache, polyglotTeams }) {
  if (clients === 'browser' || clients === 'third-party') return needsHttpCache ? 'REST (cacheable GET)' : 'REST, or gRPC behind a gateway';
  if (streaming) return 'gRPC';
  return polyglotTeams ? 'gRPC (shared .proto contract)' : 'either; start with the one your team knows';
}

console.log('gRPC status -> HTTP (common mapping), usually retryable for idempotent calls:');
for (const [grpc, http] of Object.entries(mapping)) console.log(' ', grpc.padEnd(20), http, retryable.has(grpc) ? 'retry with backoff' : '');

console.log('\nscenarios:');
console.log(' public mobile app + web      ->', recommend({ clients: 'browser', needsHttpCache: false }));
console.log(' public price list, cached    ->', recommend({ clients: 'third-party', needsHttpCache: true }));
console.log(' internal live updates        ->', recommend({ clients: 'service', streaming: true }));
console.log(' internal, Go + Node + Java   ->', recommend({ clients: 'service', polyglotTeams: true }));

A mapping table and a small decision helper. The mapping is a common convention (it follows the Google API guidance), and individual gateways may differ. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): gRPC status -> HTTP (common mapping), usually retryable for idempotent calls: OK 200 CANCELLED 499 INVALID_ARGUMENT 400 DEADLINE_EXCEEDED 504 retry with backoff NOT_FOUND 404 ALREADY_EXISTS 409 PERMISSION_DENIED 403 RESOURCE_EXHAUSTED 429 retry with backoff FAILED_PRECONDITION 400 UNIMPLEMENTED 501 INTERNAL 500 UNAVAILABLE 503 retry with backoff UNAUTHENTICATED 401 scenarios: public mobile app + web -> REST, or gRPC behind a gateway public price list, cached -> REST (cacheable GET) internal live updates -> gRPC internal, Go + Node + Java -> gRPC (shared .proto contract)

Key points

  • Public and browser clients favour REST; internal services favour gRPC.
  • Streaming and strict generated contracts favour gRPC.
  • A gateway can expose gRPC services as REST.
  • gRPC errors use status codes with a rough HTTP mapping.
  • gRPC needs deadlines and proper load balancing.

Common mistakes

  • Choosing gRPC because it is fashionable.
  • Exposing internal gRPC directly to browsers.
  • Returning INTERNAL for every error.
  • Retrying non-idempotent calls on DEADLINE_EXCEEDED.
  • Forgetting that load balancers must understand HTTP/2.

Follow-ups

  • How does a gRPC-JSON transcoding gateway work?
  • What is the difference between UNAVAILABLE and INTERNAL?
  • How do you pass deadlines between services?

Tips

  • Start with 'who is the client'.
  • Mention using both.
  • Know a few status mappings.