π Last Updated: June 2025
π οΈ Author: DevCorne2
π Table of Contents
- Introduction
- Why GZIP Compression?
- How GZIP Works
- Spring Boot GZIP Setup
- Configuration Properties
- Testing the GZIP Compression
- Advantages of GZIP Compression
- Best Practices
- Troubleshooting
- Conclusion
π Introduction
In modern web applications, speed and bandwidth optimization are critical. One simple yet effective way to improve performance is HTTP response compression using GZIP.
In this blog post, weβll see how to enable GZIP compression in a Spring Boot application with zero external dependencies, saving bandwidth and speeding up page load times.
β Why GZIP Compression?
HTTP responses often include large payloads such as:
- JSON responses from REST APIs
- HTML content
- Static files (CSS, JS, etc.)
GZIP reduces these response sizes significantly (up to 70β90%), which:
- Reduces network latency
- Improves page load time
- Optimizes mobile performance
- Saves server bandwidth
βοΈ How GZIP Works
- The client (browser or API consumer) sends a request with the header:
Accept-Encoding: gzip
- If the server supports GZIP, it compresses the response and sends it with:
Content-Encoding: gzip
- The client decompresses the response before consuming it.
β Spring Boot GZIP Setup
Spring Boot supports GZIP compression out of the box via embedded Tomcat, Jetty, or Undertow.
πΈ Step 1: No Dependency Needed
You donβt need to add any dependencies. GZIP is supported by the embedded servlet containers.
πΈ Step 2: Add Configuration
If you're using application.properties
:
server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain
server.compression.min-response-size=1024
Or use application.yml
:
server:
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/xml,text/plain
min-response-size: 1024
πΈ What Do These Properties Mean?
Property | Description |
---|---|
server.compression.enabled |
Enables or disables compression |
server.compression.mime-types |
Response content types to compress |
server.compression.min-response-size |
Minimum size in bytes to trigger compression |
π Testing the GZIP Compression
You can test the setup in multiple ways:
π§ͺ Option 1: Curl
curl -H "Accept-Encoding: gzip" -I http://localhost:8080/api/hello
You should see:
Content-Encoding: gzip
π§ͺ Option 2: Postman
- Open Postman.
- Set a custom header:
Accept-Encoding: gzip
- Send the request.
- Check response headers for:
Content-Encoding: gzip
π§ͺ Option 3: Browser Dev Tools
- Open Developer Tools (F12).
- Navigate to Network tab.
- Inspect any API call.
- Look for
Content-Encoding: gzip
under Response Headers.
π Advantages of GZIP Compression
Benefit | Description |
---|---|
π Faster Load Times | Compressing large JSON/HTML speeds up response times |
π Lower Bandwidth | Compressed payloads mean less data over the wire |
π± Mobile Friendly | Reduces data usage for mobile clients |
π SEO Boost | Page speed impacts search ranking |
π΅ Cost Efficient | Reduces cloud bandwidth cost at scale |
π‘οΈ Best Practices
- Use GZIP for API responses, not for already compressed files (like
.zip
,.jpg
,.png
). - Set a reasonable min-response-size (e.g., 1024) to avoid compressing very small responses.
- Benchmark performance to ensure compression doesnβt add noticeable CPU load.
- Use content negotiation to serve GZIP only when the client supports it.
- Use with TLS (HTTPS) for secure transmission of compressed data.
π§― Troubleshooting
Problem | Possible Fix |
---|---|
Content-Encoding header missing |
Check Accept-Encoding request header |
Response not compressed | Ensure mime-type and size match conditions |
Still slow | Use tools like GTMetrix or Lighthouse to check if GZIP is applied |
Using WebFlux | GZIP is not enabled by default in Netty; you need to add it manually (can provide steps if needed) |
β Conclusion
GZIP compression is an easy win for Spring Boot applications to boost performance without extra libraries or code. By compressing JSON, HTML, and text responses, you make your app faster, more efficient, and scalable.
π References
π Need Help?
Leave a comment or contact me if you need help with:
- GZIP in Spring WebFlux
- GZIP with specific headers or content types
- Performance testing scripts
Top comments (0)