Send fewer bytes
Responses are large and the network is the bottleneck.
Say it like this
The feed payload is dominated by repeated JSON keys and text, so I enable Brotli at the edge and return only the fields the client asked for, cutting bytes on the wire by about 80 percent before touching the backend.
Basic solution
Compress responses, use binary encodings for internal traffic, and return only the fields the client asked for.
Bytes on the wire, step by step
Field selection shrinks the response before compression, Brotli at the edge shrinks it again, and the compressed variant is cached so the origin does the work once.
How it works
- Classify each payload. Text (JSON, HTML, logs) compresses 5 to 10x because it is full of repetition; media that is already compressed (JPEG, MP4, gzip archives) does not shrink at all, and compressing it only burns CPU.
- Negotiate at the edge. The client sends an Accept-Encoding header listing what it can decode; the CDN or reverse proxy compresses text responses with Brotli or gzip, and caches the compressed form so your origin compresses each response once, not per request.
- Shrink the payload before compressing it: drop fields the client did not ask for (sparse fieldsets, GraphQL selection), paginate instead of returning everything, and avoid repeating the same nested object in every row.
- Between your own services, use a binary encoding such as Protobuf. It replaces every field name with a small numeric tag and packs integers tightly, so messages are typically 3 to 5x smaller than JSON before any compression and faster to parse.
- Compress at rest where reads are limited by disk: column stores and log stores compress blocks on disk and decompress on read, trading CPU for far fewer pages read.
- Measure bytes per request and p99 latency before and after. Compression wins when the network is the bottleneck and loses when CPU is.
Where the bytes go
- JSON repeats every field name on every row, so a list of 20 posts carries the same 15 field names 20 times. Compression removes that repetition after the fact; a binary encoding never sends it in the first place.
Worked example
Feed page in the news feed
- One page is 20 posts; each post as JSON is about 1.2 KB with author object, media URLs and counters. The page is 24 KB uncompressed.
- Brotli level 5 at the CDN: about 4.5 KB on the wire, a 5.3x reduction. On a 2 Mbps mobile link that is 18 ms of transfer instead of 96 ms, a 78 ms saving before any backend work.
- Sparse fieldsets: the mobile list view needs 7 of the 15 fields. Dropping the rest brings the uncompressed page to 11 KB and the compressed page to about 2.5 KB. Total: 24 KB to 2.5 KB, nearly 10x.
- Fleet egress: 35,000 feed reads per second times 24 KB is 840 MB per second, about 72 TB per day. At 2.5 KB it is 7.5 TB per day, a saving that shows up directly on the CDN bill.
- CPU: Brotli at level 5 costs roughly 1 ms of CPU per 24 KB response. At 35,000 per second that is 35 CPU-seconds per second, about 40 cores, which is why it runs at the edge on cached responses rather than on every origin request.
- Internal: the feed service fetches 20 post bodies from the post service as Protobuf, 300 bytes each instead of 1.2 KB JSON, so 6 KB instead of 24 KB per fan-in call and no key parsing.
Variants and when each wins
Transport compression (gzip, Brotli, zstd)
- The proxy or CDN compresses each response on the way out. Brotli wins for cacheable text at the edge because it produces the smallest output; zstd wins for dynamic responses because it is faster at a similar ratio.
Binary encodings (Protobuf, Avro, MessagePack)
- Field names become numeric tags and numbers are packed, so messages are smaller and faster to parse than JSON. They win for service-to-service traffic and stored events, and they need a schema shared by both sides.
Field selection and sparse responses
- The client names the fields it wants and the server sends only those. It wins for mobile clients because it cuts bytes before compression and also cuts backend work; it needs API support such as a fields parameter or GraphQL.
Storage compression (column stores, LZ4 blocks)
- Data is compressed on disk and decompressed on read. It wins in analytics stores because a column of similar values compresses 10x or more, which means 10x fewer pages read per query.
Recommendation: Brotli at the edge for public text, Protobuf between services, field selection for mobile, and never compress media that is already encoded.
Failure modes
Compressing already-compressed data
- You notice CPU usage up and bytes on the wire unchanged. It happens because gzip is being applied to JPEG or MP4 responses that cannot shrink further. Fix it by compressing only by content type, and serving media uncompressed with long cache headers.
Origin CPU saturation
- You notice the app servers hit 100 percent CPU after compression is enabled. It happens because every dynamic response is compressed at a high level on the origin itself. Fix it by moving compression to the proxy or CDN, lowering the compression level, and caching compressed variants so each response is compressed once.
Cache fragmentation by encoding
- You notice the CDN hit rate drops after enabling compression. It happens because the cache keys on the exact Accept-Encoding header, so every slightly different header value becomes a separate cache entry. Fix it by normalising Accept-Encoding at the edge to two or three canonical values.
Compressed response sizes leak secrets
- You notice a security review flags BREACH or CRIME. It happens because a response mixes attacker-controlled input with a secret, and the compressed size reveals how much of the secret the input matched. Fix it by not compressing responses that contain CSRF tokens alongside reflected input, or by masking the tokens so they differ per response.
Binary encoding without schema discipline
- You notice a consumer crashes after a field is renumbered. It happens because Protobuf identifies fields by tag number, and reusing a tag makes old data decode as the wrong field. Fix it by never reusing or renumbering tags, reserving removed ones, and running schema compatibility checks in CI.
Interviewer follow-ups
Answer out loud before you open each one.
When is compression a net loss?
When CPU is the bottleneck and the network is not, when payloads are already compressed media, or when responses are tiny so the header overhead and CPU exceed the bytes saved. Measure p99 latency and CPU per request on both sides of the change.
Why use Protobuf internally but JSON externally?
Internal callers are code you control, so a schema and generated clients are cheap and the size and parse savings are large. External clients value readability, browser support and debuggability, and the CDN already compresses JSON well. gRPC-web or JSON with Brotli covers the public edge.
Where should compression happen in the request path?
As close to the client as possible for cacheable responses, so the CDN compresses once and serves the compressed body many times. For dynamic responses, at the reverse proxy rather than in application code, so the setting is uniform and the app CPU stays for business logic.
How does compression interact with caching headers?
The cache must store one variant per encoding and declare Vary: Accept-Encoding so a gzip body is never served to a client that cannot decode it. Normalising Accept-Encoding to two or three values keeps the number of variants small and the hit rate high.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon CloudFront | CDN edge | Cloud CDN, Media CDN | Azure Front Door | Cloudflare, Fastly, Akamai |
| Amazon EC2 Auto Scaling | Feed service, Post service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |