Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do payload compression in grpc?

Tags:

c++

grpc

I am working on a grpc based application, where the request data could be up to 500KB and response could be bigger, and so I would like to compress the data. I have a hard time finding documents/examples in grpc on how to do it. What does set_compression_algorithm in clientContext do? Do I have to set up something on the server side?

Or should I instead forget about compression on grpc, and do compression/decompression into/out of protobuf messages myself?

like image 354
vwvwvw Avatar asked Apr 26 '16 00:04

vwvwvw


People also ask

Does gRPC support compression?

The compression supported by gRPC acts at the individual message level, taking message as defined in the wire formatdocument". The implementation supports different compression algorithms. A default compression level, to be used in the absence of message-specific settings, MAY be specified for during channel creation.

What is gRPC compression?

gRPC offers lossless compression options in order to decrease the number of bits transferred over the wire. Three levels of compression are available: grpc.


1 Answers

The grpc::ClientContext::set_compression_algorithm method selects the algorithm to be used for the client call, that is, the data being sent from the client to the server.

At the server side, you can control the compression options at channel creation time (that is, to be used for all server calls) via grpc::ServerBuilder::SetCompressionOptions (see https://github.com/grpc/grpc/blob/master/include/grpc++/server_builder.h), which allows you to:

  1. Select which compression algorithms are supposed by the server. By default, all algorithms are enabled.
  2. Select which compression algorithm will be used by default for all server responses (provided the peer client supports it. If it doesn't, responses will be sent uncompressed).

For one-off call responses at the server, you can use grpc::ServerContext::set_compression_algorithm or grpc::ServerContext::set_compression_level. The latter is recommended, as it selects the best algorithm according to the requested compression level that the client is guaranteed to support.

I'll put together hello-world-style examples in the following days. I've created an issue to track this: https://github.com/grpc/grpc/issues/6297 Feel free to ping there. I'm also implementing some low-level logging in order to display some compression statistics, for you guys to be able to assert compression is actually working (otherwise everything is completely transparent).

like image 54
David García Quintas Avatar answered Oct 22 '22 18:10

David García Quintas