Quick revision
The short answer
I would decide with measurements on real payloads, comparing size, CPU and latency for gzip, Brotli and zstd at a few levels, against the alternatives of a binary format such as protobuf or Avro, and of not transferring the data at all through pagination, field selection, deltas or caching. Compression at the proxy or service mesh gives one consistent setting, compression in the JAX-RS layer gives per-endpoint control, and a binary format cuts size and parsing cost but brings schema management and new client tooling. The decision is made per traffic class, recorded as a standard, and checked against cost after rollout.
What to remember
In this answer, we decided how to cut the cost of large internal JSON payloads. We measured size, CPU and latency for gzip, Brotli and zstd on real data and tied them to transfer and compute cost, compared compression at the proxy or mesh, one consistent setting, with compression in the JAX-RS layer, per-endpoint control, and weighed binary formats like protobuf and Avro, which cut size and parsing cost but need schema management and new tooling. We also looked at avoiding transfer through pagination, field selection, deltas and caching, and then decided per traffic class, recorded the standard and tracked cost after rollout.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would decide with measurements on real payloads, comparing size, CPU and latency for gzip, Brotli and zstd at a few levels, against the alternatives of a binary format such as protobuf or Avro, and of not transferring the data at all through pagination, field selection, deltas or caching. Compression at the proxy or service mesh gives one consistent setting, compression in the JAX-RS layer gives per-endpoint control, and a binary format cuts size and parsing cost but brings schema management and new client tooling. The decision is made per traffic class, recorded as a standard, and checked against cost after rollout.
Why this question is asked
Cross-region traffic is priced per gigabyte, and large internal JSON payloads can quietly become one of the biggest lines on a cloud bill. Teams then propose solutions that match their own expertise: the platform team wants mesh compression, a service team wants Brotli in code, someone else wants to switch to protobuf. Interviewers ask this design question to see whether we can compare options that live at different layers and choose with data. In this answer, we will set up the measurement, compare the algorithms, compare where compression runs, weigh a binary format, look at avoiding the transfer altogether, and finish with how the decision becomes a standard.
1Measure With Real Data First
Compression results depend heavily on the data, so we start with a sample of real payloads from the busiest cross-region flows: a few thousand responses per endpoint, captured with any sensitive fields masked. A small benchmark compresses each sample with each algorithm at a few levels and records the compressed size, compression and decompression CPU time per megabyte, and the added latency at the 50th and 99th percentile.
We also need the cost side. If cross-region transfer costs 0.02 per GB and these flows move 400 TB a month, that is 8,000 a month, and a 5 to 1 compression ratio would save about 6,400. CPU spent compressing costs money too, so the benchmark's CPU numbers are converted into instance cost. Only with both numbers can we say whether a stronger algorithm pays for itself.
2Comparing gzip, Brotli and zstd
The three algorithms make different trade-offs between ratio and speed, and each has levels that move along that curve. The table shows the kind of result a benchmark on JSON typically gives; our own numbers will differ, which is why we measure.
| Option | Ratio | Compress CPU | Decompress CPU | Support |
|---|---|---|---|---|
| gzip level 6 | about 6:1 | medium | low | everywhere |
| Brotli level 5 | about 7:1 | medium | low | browsers, proxies, Java via library |
| Brotli level 11 | about 8:1 | very high | low | best for static assets |
| zstd level 3 | about 6.5:1 | low | very low | proxies like Envoy, Java via library |
| protobuf, no compression | about 2-3:1 vs JSON | low | low | needs schemas and generated code |
| protobuf + zstd | about 10:1 vs JSON | low | very low | both of the above |
The pattern is that zstd at a moderate level gives a ratio similar to or better than gzip while using much less CPU, and its decompression is very fast. Brotli at high levels compresses best but is expensive to compress, which suits static assets far better than dynamic internal responses. gzip's advantage is that everything supports it already, including every Java HTTP client. zstd and Brotli need libraries in Java (zstd-jni, Brotli4j) and support in the proxy, so check what the mesh and clients support.
3Where Compression Runs: Proxy or JAX-RS Layer
At the proxy or service mesh, compression is one setting for every service: Envoy's compressor filter, for example, can compress responses and the receiving side's sidecar can decompress, so services need no code changes and every team gets the same behaviour. The costs are that the sidecar spends CPU on every byte, the setting is coarse, and if it compresses between sidecars only, the services' own clients never see the savings within a pod, which is fine because the expensive hop is between regions.
In the JAX-RS layer, using Jersey's EncodingFilter or a custom WriterInterceptor for zstd, we get per-endpoint control: compress the 2 MB catalogue export, skip the 200-byte status call, and choose the level per endpoint. The costs are code and configuration in every service, CPU on request threads, and clients that must all support the chosen encoding. A sensible default is mesh compression for cross-region traffic, with the JAX-RS layer reserved for endpoints that need something special.
4A Binary Format: Real Gains, Real Costs
Switching from JSON to protobuf or Avro shrinks payloads because field names disappear and numbers are encoded compactly, typically 2 to 3 times smaller before compression, and parsing is much faster, which matters when services spend significant CPU parsing large JSON. Combined with compression, the result is smaller still.
But it is a bigger change than a header. It needs schema management: a registry or a shared repository of .proto files, rules for compatible changes, and code generation in every client. Debugging gets harder because payloads are no longer readable with curl, and every consumer, including data pipelines and partner integrations, must change. In JAX-RS, we can serve protobuf alongside JSON with a MessageBodyReader and Writer for application/x-protobuf and content negotiation, which allows gradual migration, but it doubles the formats to support for a while. So a binary format is worth it for a few very large, stable, high-volume flows, and rarely for everything.
5Avoiding the Transfer Altogether
The cheapest byte is the one we never send. Before compressing, we ask why the payload is large. Often a client fetches a 5,000-item list to show 20 rows, when pagination would send 20. Often it uses 6 of 80 fields, when field selection or a dedicated view would send 6. Often it refetches a whole catalogue every minute to detect a few changes, when a delta endpoint (changes since version 18342) or an event stream would send only the changes. And often the same data crosses the region boundary repeatedly, when a regional cache or replica would send it once.
These changes can cut transfer by 10 to 100 times, far more than any algorithm, though they require API changes that compression does not.
6Deciding Per Traffic Class and Following Up
There is no single answer for all traffic, so we decide per traffic class and record it. For example: cross-region service traffic gets zstd or gzip at the mesh by default; the three bulk-sync flows that dominate volume move to delta endpoints, and the largest one to protobuf with zstd; browser-facing traffic gets Brotli at the CDN; small request-response calls are left alone. The decision record lists the measurements, the expected savings, and who owns each change.
After rollout we track the actual cross-region bytes, CPU usage of services and sidecars, p99 latency, and the monthly transfer cost, and compare them with the estimates. If the CPU cost of compression eats most of the saving for some flow, we lower the level or turn it off there.
- Can the transfer be avoided?pagination, fields, deltas, cachingYesChange the API firstNoConsider compression
- Is it high volume and stable?YesConsider protobuf or AvroNoStay with JSON
- Same needs as other services?YesCompress at the meshNoCompress per endpoint in JAX-RS
- Record the standard, track cost after rollout
4 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Why not just enable the strongest compression everywhere?
Because the strongest levels cost much more CPU for a small extra gain, and for dynamic responses that CPU is spent on every request. On internal traffic a moderate zstd or gzip level usually captures most of the saving, and the benchmark shows where extra levels stop paying for themselves.
Does compression add latency?
Some, a few milliseconds per megabyte at moderate levels, but for large payloads across regions it usually reduces total latency, because sending fewer bytes over a long, limited link saves more time than compression costs. For small payloads the reverse can be true, which is why we set a minimum size.
How do we keep JSON readable for debugging if we switch to protobuf?
Serve both formats through content negotiation during migration, provide a small tool or endpoint that converts payloads to JSON for support staff, and log decoded summaries rather than raw bytes.
8Conclusion
In this answer, we decided how to cut the cost of large internal JSON payloads. We measured size, CPU and latency for gzip, Brotli and zstd on real data and tied them to transfer and compute cost, compared compression at the proxy or mesh, one consistent setting, with compression in the JAX-RS layer, per-endpoint control, and weighed binary formats like protobuf and Avro, which cut size and parsing cost but need schema management and new tooling. We also looked at avoiding transfer through pagination, field selection, deltas and caching, and then decided per traffic class, recorded the standard and tracked cost after rollout.
Open with
I would not pick an option before measuring. I would take a sample of real payloads from the busiest cross-region flows and benchmark gzip, Brotli and zstd at a few levels, recording ratio, compression and decompression CPU, and added latency, and then put those next to the actual transfer cost, so we know whether a stronger algorithm pays for its CPU.
Then, if they want more
Typically zstd at a low level gives gzip-like ratios at much lower CPU, and high Brotli levels are too expensive for dynamic responses.
On where to compress: the proxy or service mesh gives one consistent setting for every team with no code changes, while the JAX-RS layer gives per-endpoint control, so I would default to the mesh for cross-region traffic and use code only for special endpoints.
A binary format like protobuf or Avro cuts size and parsing cost, but it needs schema management, generated clients and changes for every consumer, so I would reserve it for a few very large, stable flows.
Before all that, I would ask whether we need to send the data at all: pagination, field selection, delta endpoints and regional caching often save far more than compression. Then I decide per traffic class, record it as a standard with owners, and track bytes, CPU, latency and cost after rollout to check the estimates.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your services exchange large JSON payloads internally and network cost between regions...": https://scrivio-interview-library.netlify.app/java/jax-rs/8f90b2eca6/