what is grpc trailers metadata used for?

Viewed 3141

I was looking through grpc documents and found out that on the server-side you are able to set metadata both in the form of headers and trailers. Headers seem like the usual replacement for normal HTTP headers with key-value mapping. I don't see any needs for trailers anymore seems the header is serving somewhat a similar purpose or am I missing something here?

2 Answers

Trailers can be used for anything the server wishes to send to the client after processing the request. Typically this should be used for information common to all methods a service provides, for example, data about the load created by the RPC for metrics purposes.

gRPC uses HTTP trailers for two purposes.

  • it sends its final status (grpc-status) as a trailer header after the content has been sent.
    • When an application or runtime error occurs during an RPC a Status and Status-Message are delivered in Trailers.
    • For responses end-of-stream is indicated by the presence of the END_STREAM flag on the last received HEADERS frame that carries Trailers
  • The second reason is to support streaming use cases. These use cases last much longer than normal HTTP requests. The HTTP trailer is used to give the post-processing result of the request or the response. For example, if there is an error during streaming data processing, you can send an error code using the trailer, which is not possible with the header before the message body.

Source 1 2

Related