DEV Community

DevCorner2
DevCorner2

Posted on

πŸš€ Enabling GZIP Compression in Spring Boot for Faster Web Apps

πŸ“… Last Updated: June 2025

πŸ› οΈ Author: DevCorne2


πŸ“ Table of Contents

  1. Introduction
  2. Why GZIP Compression?
  3. How GZIP Works
  4. Spring Boot GZIP Setup
  5. Configuration Properties
  6. Testing the GZIP Compression
  7. Advantages of GZIP Compression
  8. Best Practices
  9. Troubleshooting
  10. 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

  1. The client (browser or API consumer) sends a request with the header:
   Accept-Encoding: gzip
Enter fullscreen mode Exit fullscreen mode
  1. If the server supports GZIP, it compresses the response and sends it with:
   Content-Encoding: gzip
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

Or use application.yml:

server:
  compression:
    enabled: true
    mime-types: application/json,application/xml,text/html,text/xml,text/plain
    min-response-size: 1024
Enter fullscreen mode Exit fullscreen mode

πŸ”Έ 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
Enter fullscreen mode Exit fullscreen mode

You should see:

Content-Encoding: gzip
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Option 2: Postman

  1. Open Postman.
  2. Set a custom header:
   Accept-Encoding: gzip
Enter fullscreen mode Exit fullscreen mode
  1. Send the request.
  2. Check response headers for:
   Content-Encoding: gzip
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Option 3: Browser Dev Tools

  1. Open Developer Tools (F12).
  2. Navigate to Network tab.
  3. Inspect any API call.
  4. 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)