Call types, status codes, deadlines and metadata
The result of a gRPC call lives in grpc-status in the trailers, not in the HTTP code, because the server usually answers 200 whatever the outcome. The status follows the messages, since a stream may break midway. There are four call types: unary, server, client and bidirectional streaming. There is no deadline by default, so it is set explicitly, and retries are enabled only for idempotent methods.
API · gRPC
gRPC answered HTTP 200, yet the call still failed. Where do you look for the real result?
An automated test for a gRPC service is green: the response status is 200. A week later it turns out the method returned INTERNAL on every other request. The test looked in the wrong place. The lesson shows where the result lives in gRPC, what call types exist, how deadlines, retries and metadata work and what to check.
The four call types
HTTP/2 allows many streams in one connection, so gRPC has four RPC types: unary (one request, one response, the closest to REST), server streaming (an event feed, large selections), client streaming (a file upload, a batch of metrics) and bidirectional streaming (chat, interactive sessions).
Four call types
Where the result lives: statuses and trailers
A gRPC server answers with HTTP status 200 whatever the result: another code usually arrives only from a proxy or with a wrong content-type. The call status is appended by the server at the end, in the trailers, as grpc-status (a number) and grpc-message. It is done so because a response stream can break midway: the status cannot be known before the stream ends, so it is sent after the messages.
The structure of a gRPC response
The same call for a missing order looks roughly like this on the wire:
# schematic, not a verbatim output
HTTP/2 200
content-type: application/grpc
(the response body is empty)
grpc-status: 5
grpc-message: order 42 not found
The main codes and their closest HTTP counterparts:
| Code | When | HTTP |
|---|---|---|
OK (0) | success | 200 |
INVALID_ARGUMENT (3) | the request is invalid by content | 400 |
DEADLINE_EXCEEDED (4) | the deadline ran out | 504 |
NOT_FOUND (5) | no such resource | 404 |
ALREADY_EXISTS (6) | the resource already exists | 409 |
PERMISSION_DENIED (7) | authenticated, not allowed | 403 |
RESOURCE_EXHAUSTED (8) | limits, quotas, a too large message | 429 |
FAILED_PRECONDITION (9) | the system state forbids the operation | 400 |
UNIMPLEMENTED (12) | the method is not implemented | 501 |
INTERNAL (13) | a server error | 500 |
UNAVAILABLE (14) | temporarily unavailable, may be retried | 503 |
UNAUTHENTICATED (16) | no valid credentials | 401 |
Error details (for example, which fields are invalid) are passed in a google.rpc.Status structure with typed details (BadRequest, ErrorInfo) in the grpc-status-details-bin trailer.
Metadata, deadlines, retries, interceptors
- Metadata is the analogue of HTTP headers: key-value pairs at the start (headers) and at the end of a call (trailers). Keys are lowercase, binary ones have the
-binsuffix, thegrpc-prefix is reserved. Metadata carries the token (authorization: Bearer …), a trace id, the language. - A deadline is the time until which the client is ready to wait; on the wire it goes as
grpc-timeout. By default there is no deadline, so a call can hang forever: production clients always set one. The deadline should propagate further down the chain of services, and after it expires the server should stop working. - Retries are set in the service config (
retryPolicy: the number of attempts, backoff, the list of statuses inretryableStatusCodes, usuallyUNAVAILABLE). Only idempotent calls are worth retrying. - Interceptors (middleware) wrap calls on the client and the server: logging, authorisation, metrics, tracing.
Worked example: a test that looks in the right place
The test reads the code from the client's error, not the HTTP status, and sets the deadline explicitly:
// a 2 s deadline; expect NOT_FOUND for a missing id
const deadline = new Date(Date.now() + 2000);
const err = await new Promise<grpc.ServiceError | null>((resolve) =>
client.getOrder({ id: 'missing' }, meta, { deadline }, (e) => resolve(e)),
);
expect(err?.code).toBe(grpc.status.NOT_FOUND);
Without an explicit deadline, a test of a slow dependency hangs until the framework timeout and never shows DEADLINE_EXCEEDED.
Limits. The HTTP equivalents in the table are a recommendation from google.rpc.Code, not a protocol rule: on a direct call the HTTP status stays 200, and a gateway or proxy may map codes its own way, so gateway tests check its actual codes. A stream's status arrives only at the end, so a test must read the stream to completion, or it will miss an error after the fifth message.
Common mistakes
- Checking HTTP 200 instead of
grpc-status: errors go unnoticed. - Not setting a deadline: the call hangs and CI keeps the agent busy until the overall timeout.
- Enabling retries on non-idempotent methods: a repeat creates a duplicate.
- Mixing up
PERMISSION_DENIEDandUNAUTHENTICATED: like 403 and 401, they are different access errors.
Check yourself
A stream of ten messages broke on the fifth, and the client received 200. Where do you see the error?
Answer
In the trailers: grpc-status comes after the messages, while HTTP 200 was already sent in the headers. The client library reports it as a stream error.
Why is retrying by default safe only for UNAVAILABLE and not INTERNAL?
Answer
UNAVAILABLE means the request most likely did not reach the server logic, so a retry is usually safe for an idempotent method. INTERNAL says the server logic already ran and failed, so a retry may repeat a partial side effect.
grpc-status, not in the HTTP code, because the server returns 200. Middle/Senior: I explain that the status travels in the trailers because a stream can break midway. In tests I set the deadline explicitly, check INVALID_ARGUMENT with details, DEADLINE_EXCEEDED and that the server stops working after cancellation. I enable retries only for idempotent methods and UNAVAILABLE.