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
- GZIP compresses repetitive data patterns, like JSON keys or similar field names.
- Clients (browsers, mobile apps, Postman) automatically handle compressed responses.
- 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:
- Postman → Headers tab
- Chrome DevTools → Network > Response Headers
Look for:
Content-Encoding: gzip
When You Should Enable It
Use this trick when:
- Your API returns large JSON or XML payloads
- You want to reduce bandwidth usage
- You need to speed up mobile app or frontend API consumption
When You Should Not
Avoid compression if:
- You serve already compressed content (e.g., images, videos, PDFs)
- You operate in low-latency internal networks
- You care more about CPU usage than bandwidth
Real-World Use Case
In my Spring Boot microservice setup, I had 3 key endpoints:
- /api/products – returned large JSON array
- /api/export – returned downloadable CSV
- /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.