Compressing API responses

Performance and scaling Junior Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Predict the output

Is the compressed JSON more than 10× smaller?

Python 3.13
import json
import zlib

rows = [{"id": i, "status": "shipped", "country": "IN"}
        for i in range(500)]
raw = json.dumps(rows).encode()
packed = zlib.compress(raw, 6)
print(len(raw) > 10 * len(packed))

Options
Hint 1 of 1

How repetitive is this JSON?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A True

Correct. Repeated keys and values compress very well.

DEFLATE replaces repeats with back-references. 500 rows with the same keys and almost the same values are nearly all repeats, so they shrink far more than 10×.

Why not the others?
  • B False. 500 near-identical rows are highly repetitive.
  • C An error. zlib.compress takes bytes, which encode() gives.
  • D None. The comparison returns a bool.

30-second answer

Usually yes for text like JSON: repetitive keys compress very well, cutting transfer time on slow mobile networks and egress cost. Use gzip or Brotli negotiated via Accept-Encoding, often at the proxy or CDN rather than in app code. Skip tiny responses (the overhead can make them bigger) and already-compressed content like images. Avoid compressing secrets alongside attacker-controlled text on the same response, the BREACH attack pattern.

Practise saying it · 25 s. No recording, just a timer

Compress responses?

Say your answer out loud, then check which of these points you covered:

  • Yes for JSON
  • gzip / Brotli via Accept-Encoding
  • Skip tiny and pre-compressed
  • BREACH

Deep dive

Brotli usually beats gzip on text at similar CPU cost for static assets.

Set a minimum size, for example 1 KB.

Compression costs CPU; measure on hot endpoints.

Smaller payloads (sparse fields, pagination) beat compressing big ones.

Repetitive JSON compresses well Ran on Python 3.13.5 · mypy --strict
Python 3.13
import json
import zlib

rows = [{"id": i, "status": "shipped", "country": "IN"}
        for i in range(500)]
raw = json.dumps(rows).encode()
packed = zlib.compress(raw, 6)
print(len(raw) > 10 * len(packed))

Common mistakes

  • Compressing JPEGs and zips again.
  • Compressing every 100-byte response.
  • Ignoring payload size because 'gzip will fix it'.

Senior take & follow-ups

Benefits for JSON, negotiation, size thresholds, what not to compress, BREACH.

Follow-up questions

Think of your answer first, then open the hint.

  1. What's BREACH?

    Show hint

    Guessing secrets from compressed sizes when they share a response with attacker input.

  2. Where to compress?

    Show hint

    Usually at the edge proxy or CDN.

Suggest a fix if something here is wrong or out of date.