Implementing Compression
1. Enabling gzip Compression
Example: Per-call gzip
import _ "google.golang.org/grpc/encoding/gzip"
res, err := client.GetUser(ctx, req, grpc.UseCompressor("gzip"))
| Step | Detail |
|---|---|
| Import side-effect | Registers compressor |
| Server | Decompresses automatically if registered |
2. Setting Compression Level
| Library | Detail |
|---|---|
| Built-in gzip | Fixed default level |
| Custom compressor | Implement encoding.Compressor for tuned level |
3. Registering Custom Compressor
Example: Custom compressor registration
type zstdCompressor struct{}
func (zstdCompressor) Name() string { return "zstd" }
func (zstdCompressor) Compress(w io.Writer) (io.WriteCloser, error) { ... }
func (zstdCompressor) Decompress(r io.Reader) (io.Reader, error) { ... }
func init() { encoding.RegisterCompressor(zstdCompressor{}) }
4. Configuring Client Compression
| API | Use |
|---|---|
grpc.UseCompressor("gzip") | Per-call |
grpc.WithDefaultCallOptions(grpc.UseCompressor("gzip")) | All calls |
5. Configuring Server Compression
| Note | Detail |
|---|---|
| Decompress | Automatic if compressor registered (via import) |
| Compress responses | Honors grpc-accept-encoding from client |
6. Detecting Compression Support
| Header | Purpose |
|---|---|
grpc-encoding | Compressor used on this message |
grpc-accept-encoding | Algorithms client can decode |
7. Disabling Compression
| Need | Action |
|---|---|
| Pre-compressed bytes | Don't set compressor; payload already small |
| Latency-critical small msg | Compression CPU may exceed savings |
8. Implementing Custom Compression
| Algorithm | Use |
|---|---|
| gzip | Default, widely supported |
| zstd | Better ratio + speed; needs both ends |
| snappy / lz4 | Faster, weaker compression |
9. Setting Compression Threshold
| Guideline | Detail |
|---|---|
| < 1 KB | Often not worth compressing |
| > 4 KB | Usually beneficial |
| Built-in | No threshold — applied to all messages |
10. Measuring Compression Efficiency
| Metric | Detail |
|---|---|
| Ratio | compressed / original |
| CPU time | Profile compress/decompress |
| Latency impact | p50/p99 before vs after |