Gzip compression middleware with correct Vary header

9533
0

Compressing responses is an easy bandwidth win for JSON APIs, but only when it's done carefully. I check Accept-Encoding for gzip, set Vary: Accept-Encoding so caches behave correctly, and stream output through gzip.Writer so we don't buffer full responses in memory. The key operational detail is CPU: compression can become expensive under load, so I usually enable it at the edge or for selected endpoints, not blindly everywhere. This pattern is also safe for streaming responses because it writes as data is produced. If you add content-type checks and a size threshold, you can avoid compressing already-compressed formats like image/*. Done right, this is a clean middleware that improves latency and reduces egress costs.