If you are using Pub/Sub to publish messages that amount to a lot of data, you can use gRPC to compress your data to save networking costs before your publisher client sends out the publish request. The Pub/Sub compression for gRPC uses the Gzip algorithm.
This document provides information about compressing messages published to a topic.
About compressing messages
The compression ratio for using the gRPC client-side compression feature is different for different publisher clients and dependent on the following factors:
Amount of data. The compression ratio improves when the size of the payload increases from a few hundred bytes to many kilobytes of data. The batch settings of a publish request decides the amount of data that is included in each publish request. We recommend that you turn on batch settings in conjunction with gRPC compression to get the best results.
Type of data. Text-based data such as JSON or XML are more compressible compared to binary data such as images.
If your publisher client is on Google Cloud, you can use
the Sent bytes (instance/network/sent_bytes_count
) metric
to measure the publishing throughput in bytes. If your publisher
client is on a different application, you must use the
client-specific tools for making the measurement.
The code sample in this section shows a sample Java client library code snippet that also includes gRPC compression.
Before you begin
Before configuring the publish workflow, ensure you have completed the following tasks:
- Learn about topics and the publishing workflow.
- Create a topic.
Required roles
To get the permissions that you need to compress messages,
ask your administrator to grant you the
Pub/Sub Publisher (roles/pubsub.publisher
) IAM role on your topic.
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
You need additional permissions to create or update topics and subscriptions.
Compress a message
C++
Before trying this sample, follow the C++ setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C++ API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Java API reference documentation.
What's next
To learn how to configure advanced publishing options, see the following: