DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Configure, Customize, and Use Ballerina Logs
  • Working With Data in Microservices
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j

Trending

  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • Strategies for Securing E-Commerce Applications
  • Designing AI Multi-Agent Systems in Java
  1. DZone
  2. Coding
  3. Languages
  4. gRPC Compression Support in Go, Java, and Ballerina

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.

By 
Buddhi Kothalawala user avatar
Buddhi Kothalawala
·
Oct. 13, 21 · Analysis
Likes (4)
Comment
Save
Tweet
Share
15.9K Views

Join the DZone community and get the full member experience.

Join For Free

In 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:

Go
 
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:

Go
 
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:

Java
 
StringValue msg = client.blockingStub.withCompression("gzip").hello(StringValue.of("content"));


Use the following interceptor to enable Gzip compression on Java server-side:

Java
 
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.

Java
 
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.

Java
 
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.

Java
 
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.

  1. Compressed flag in a gRPC message
  2. gRPC message length
  3. gRPC message encoding

Testing gRPC compression


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.

Java (programming language) Ballerina (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Configure, Customize, and Use Ballerina Logs
  • Working With Data in Microservices
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!