| 1 |
## gRPC Compression |
| 2 |
|
| 3 |
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", |
| 4 |
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be |
| 5 |
interpreted as described in [RFC 2119](http://www.ietf.org/rfc/rfc2119.txt). |
| 6 |
|
| 7 |
### Intent |
| 8 |
|
| 9 |
Compression is used to reduce the amount of bandwidth used between peers. The |
| 10 |
compression supported by gRPC acts _at the individual message level_, taking |
| 11 |
_message_ [as defined in the wire format |
| 12 |
document](PROTOCOL-HTTP2.md). |
| 13 |
|
| 14 |
The implementation supports different compression algorithms. A _default |
| 15 |
compression level_, to be used in the absence of message-specific settings, MAY |
| 16 |
be specified for during channel creation. |
| 17 |
|
| 18 |
The ability to control compression settings per call and to enable/disable |
| 19 |
compression on a per message basis MAY be used to prevent CRIME/BEAST attacks. |
| 20 |
It also allows for asymmetric compression communication, whereby a response MAY |
| 21 |
be compressed differently, if at all. |
| 22 |
|
| 23 |
### Specification |
| 24 |
|
| 25 |
Compression MAY be configured by the Client Application by calling the |
| 26 |
appropriate API method. There are two scenarios where compression MAY be |
| 27 |
configured: |
| 28 |
|
| 29 |
+ At channel creation time, which sets the channel default compression and |
| 30 |
therefore the compression that SHALL be used in the absence of per-RPC |
| 31 |
compression configuration. |
| 32 |
+ At response time, via: |
| 33 |
+ For unary RPCs, the {Client,Server}Context instance. |
| 34 |
+ For streaming RPCs, the {Client,Server}Writer instance. In this case, |
| 35 |
configuration is reduced to disabling compression altogether. |
| 36 |
|
| 37 |
### Compression Method Asymmetry Between Peers |
| 38 |
|
| 39 |
A gRPC peer MAY choose to respond using a different compression method to that |
| 40 |
of the request, including not performing any compression, regardless of channel |
| 41 |
and RPC settings (for example, if compression would result in small or negative |
| 42 |
gains). |
| 43 |
|
| 44 |
If a client message is compressed by an algorithm that is not supported |
| 45 |
by a server, the message WILL result in an `UNIMPLEMENTED` error status on the |
| 46 |
server. The server will then include a `grpc-accept-encoding` response |
| 47 |
header which specifies the algorithms that the server accepts. If the client |
| 48 |
message is compressed using one of the algorithms from the `grpc-accept-encoding` header |
| 49 |
and an `UNIMPLEMENTED` error status is returned from the server, the cause of the error |
| 50 |
MUST NOT be related to compression. If a server sent data which is compressed by an algorithm |
| 51 |
that is not supported by the client, an `INTERNAL` error status will occur on the client side. |
| 52 |
|
| 53 |
Note that a peer MAY choose to not disclose all the encodings it supports. |
| 54 |
However, if it receives a message compressed in an undisclosed but supported |
| 55 |
encoding, it MUST include said encoding in the response's `grpc-accept-encoding` |
| 56 |
header. |
| 57 |
|
| 58 |
For every message a server is requested to compress using an algorithm it knows |
| 59 |
the client doesn't support (as indicated by the last `grpc-accept-encoding` |
| 60 |
header received from the client), it SHALL send the message uncompressed. |
| 61 |
|
| 62 |
### Specific Disabling of Compression |
| 63 |
|
| 64 |
If the user (through the previously described mechanisms) requests to disable |
| 65 |
compression the next message MUST be sent uncompressed. This is instrumental in |
| 66 |
preventing BEAST/CRIME attacks. This applies to both the unary and streaming |
| 67 |
cases. |
| 68 |
|
| 69 |
### Compression Levels and Algorithms |
| 70 |
|
| 71 |
The set of supported algorithm is implementation dependent. In order to simplify |
| 72 |
the public API and to operate seamlessly across implementations (both in terms |
| 73 |
of languages but also different version of the same one), we introduce the idea |
| 74 |
of _compression levels_ (such as "low", "medium", "high"). |
| 75 |
|
| 76 |
Levels map to concrete algorithms and/or their settings (such as "low" mapping |
| 77 |
to "gzip -3" and "high" mapping to "gzip -9") automatically depending on what a |
| 78 |
peer is known to support. A server is always aware of what its clients support, |
| 79 |
as clients disclose it in the Message-Accept-Encoding header as part of the |
| 80 |
RPC. A client doesn't a priori (presently) know which algorithms a |
| 81 |
server supports. This issue can be addressed with an initial negotiation of |
| 82 |
capabilities or an automatic retry mechanism. These features will be implemented |
| 83 |
in the future. Currently however, compression levels are only supported at the |
| 84 |
server side, which is aware of the client's capabilities through the incoming |
| 85 |
Message-Accept-Encoding header. |
| 86 |
|
| 87 |
### Propagation to child RPCs |
| 88 |
|
| 89 |
The inheritance of the compression configuration by child RPCs is left up to the |
| 90 |
implementation. Note that in the absence of changes to the parent channel, its |
| 91 |
configuration will be used. |
| 92 |
|
| 93 |
### Test cases |
| 94 |
|
| 95 |
1. When a compression level is not specified for either the channel or the |
| 96 |
message, the default channel level _none_ is considered: data MUST NOT be |
| 97 |
compressed. |
| 98 |
1. When per-RPC compression configuration isn't present for a message, the |
| 99 |
channel compression configuration MUST be used. |
| 100 |
1. When a compression method (including no compression) is specified for an |
| 101 |
outgoing message, the message MUST be compressed accordingly. |
| 102 |
1. A message compressed by a client in a way not supported by its server MUST |
| 103 |
fail with status `UNIMPLEMENTED`, its associated description indicating the |
| 104 |
unsupported condition as well as the supported ones. The returned |
| 105 |
`grpc-accept-encoding` header MUST NOT contain the compression method |
| 106 |
(encoding) used. |
| 107 |
1. A message compressed by a server in a way not supported by its client MUST |
| 108 |
fail with status `INTERNAL`, its associated description indicating the |
| 109 |
unsupported condition as well as the supported ones. The returned |
| 110 |
`grpc-accept-encoding` header MUST NOT contain the compression method |
| 111 |
(encoding) used. |
| 112 |
1. An ill-constructed message with its [Compressed-Flag |
| 113 |
bit](PROTOCOL-HTTP2.md#compressed-flag) |
| 114 |
set but lacking a |
| 115 |
[grpc-encoding](PROTOCOL-HTTP2.md#message-encoding) |
| 116 |
entry different from _identity_ in its metadata MUST fail with `INTERNAL` |
| 117 |
status, its associated description indicating the invalid Compressed-Flag |
| 118 |
condition. |