gRPC Compression Support in Go, Java, and Ballerina
Different programming languages have different ways to enable/disable gRPC compression. This article describes how to use gRPC in Go, Java, and Ballerina.
Join the DZone community and get the full member experience.
Join For FreeIn distributed applications, occasionally, we use compression methods to save the network bandwidth. gRPC supports message-level compression on both client and server sides. Different programming languages have different kinds of ways to enable/disable gRPC compression. This article describes how to use gRPC compression in several programming languages; Go, Java, and Ballerina. Gzip compression algorithm is the most commonly used compression algorithm in gRPC, and the samples in this article also use Gzip.
gRPC Compression in Go
In Golang, there are two different ways to enable compression on the client-side and server-side. On the client-side, we have to pass a relevant compressor as an option to the RPC call. On the server side, we have to import the gRPC Gzip package to enable message compression.
Use the following code block to enable Gzip compression on Go client-side:
compressor := grpc.UseCompressor(gzip.Name)
r, err := c.Hello(ctx, &wrapperspb.StringValue{Value: string("Hello")}, compressor)
Use the following code block to enable Gzip compression on Go server-side:
import (
_ "google.golang.org/grpc/encoding/gzip"
)
gRPC Compression in Java
Java also has two different ways to enable compression on the client and the server sides. We have to invoke the withCompression
API in the stub class to enable compression on a gRPC Java client. To enable compression on a Java gRPC server, we have to use an interceptor.
Use the following API call to enable Gzip compression on Java client-side:
StringValue msg = client.blockingStub.withCompression("gzip").hello(StringValue.of("content"));
Use the following interceptor to enable Gzip compression on Java server-side:
server = ServerBuilder.forPort(50052).intercept(
new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
call.setCompression("gzip");
return next.startCall(call, headers);
}
})
gRPC Compression in Ballerina
Unlike Go and Java, Ballerina does not have two different ways to enable compression on the client and the server. Instead, we can call an API to enable compression, and it will return a header map that can pass to each RPC. Typically, the gRPC client and server identified the received gRPC message is compressed or not using the grpc-encoding
header. Ballerina API for enabling compression will populate this header and returns a header map. We have to use this populated header map in our RPC calls.
Internally, Ballerina checks whether the relevant compression header is available or not. If that header is available, Ballerina will compress the gRPC message(s). Note that the following compression API is supported in Ballerina Swan Lake Beta 3 onwards.
An example of enabling compression in a Ballerina gRPC client.
map<string|string[]> headers = grpc:setCompression(grpc:GZIP);
ContextString cs = check ep->helloContext({content: s, headers: headers});
Note that even we can reuse an existing header map to populate compression headers.
map<string|string[]> headers = {...}; // An existing header map
headers = grpc:setCompression(grpc:GZIP, headers);
ContextString cs = check ep->helloContext({content: s, headers: headers});
An example of enabling compression in a Ballerina gRPC server.
remote function hello(string value) returns ContextString|error {
...
map<string|string[]> headers = grpc:setCompression(grpc:GZIP);
return {content: "content", headers: headers};
}
Testing
gRPC compression cannot be easily verified. It has to capture all the gRPC packets and check the following entries to verify a successful compression.
- Compressed flag in a gRPC message
- gRPC message length
- gRPC message encoding
The complete implementation of all the examples discussed above is available in the following GitHub repository.
Conclusion
Compressing a message in network communication is an essential task to save bandwidth and increase the communication speed. In this article, we discussed how to use gRPC compression in different programming languages; Go, Java, and Ballerina.
Opinions expressed by DZone contributors are their own.
Comments