Performance Optimizations - Compress Response Payloads

Speed up the response times on large payloads

If your API feels slow, it’s not always the backend logic. Sometimes the issue is much simpler — like how much data you’re pushing down the wire.

Sometimes, you may have an endpoint returning a list of 10,000 products. No complex logic. No heavy DB queries.

The response times could be bad because of the payload size. With every request, the json response size can eat up bandwidth and can cause slow client-side rendering.

Enable GZIP Compression

Spring Boot supports HTTP compression out of the box. But it’s disabled by default.

To enable it, make these changes in application.properties:

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/plain
server.compression.min-response-size=1024

With this, every response larger than 1 KB is automatically GZIP-compressed — reducing payload size by up to 90%.

And this was without touching a single controller, service, or DTO.

Advantages with this approach

  1. GZIP compresses repetitive data patterns, like JSON keys or similar field names.
  2. Clients (browsers, mobile apps, Postman) automatically handle compressed responses.
  3. Less data over the network = faster delivery and response.

And best of all: it’s backward compatible — clients that don’t support GZIP will get uncompressed data.

Add GZIP Headers for Security/Control

If you’re working with proxies or load balancers, make sure Accept-Encoding: gzip is allowed.

You can also inspect compressed responses using tools like:

  1. Postman → Headers tab
  2. Chrome DevTools → Network > Response Headers

Look for:

Content-Encoding: gzip

When You Should Enable It

Use this trick when:

  1. Your API returns large JSON or XML payloads
  2. You want to reduce bandwidth usage
  3. You need to speed up mobile app or frontend API consumption

When You Should Not

Avoid compression if:

  1. You serve already compressed content (e.g., images, videos, PDFs)
  2. You operate in low-latency internal networks
  3. You care more about CPU usage than bandwidth

Real-World Use Case

In my Spring Boot microservice setup, I had 3 key endpoints:

  1. /api/products – returned large JSON array
  2. /api/export – returned downloadable CSV
  3. /api/status – returned tiny status info

I enabled compression globally but excluded binary downloads:

server.compression.enabled=true
server.compression.mime-types=application/json,text/csv
server.compression.min-response-size=1024

Frontend team was thrilled. Load times dropped by ~60% across the board.

Try it. Benchmark it. And let your frontend team feel the difference.

No code changes. Just smart config.


Links to this note