GZip in Rest APi

S

Sadiul Hakim

Guest

1. What is Gzip in REST API?​

  • Gzip is a popular lossless compression algorithm.
  • In the context of REST APIs, Gzip can compress the HTTP request body (client β†’ server) or the response body (server β†’ client).
  • Compression reduces payload size, which makes data transfer faster over the network.

It works with the Content-Encoding header:

  • Request from client:

Code:
  Accept-Encoding: gzip

(client says β€œI can accept compressed responses”)

  • Server response:

Code:
  Content-Encoding: gzip

(server says β€œHere’s the response, compressed with gzip”)

2. When to Use Gzip​


Good use cases:

  • Large JSON/XML responses (e.g., reports, analytics data, big lists).
  • APIs with high traffic over slow/limited networks (mobile, IoT).
  • Reducing bandwidth costs on public APIs.
  • Serving static content (HTML, CSS, JS, JSON, XML).

Avoid Gzip:

  • Very small responses (e.g., {"status":"ok"}), since compression overhead may make it larger.
  • Already compressed data (images: .jpg, .png, .gif, .zip, .mp4). Gzip won’t help here.
  • Low-latency APIs (where CPU overhead of compressing/decompressing might be worse than bandwidth savings).
  • Internal microservice calls in high-performance clusters (if network speed >> CPU cost).

3. Benefits​

  • Performance: Faster API responses over the internet.
  • Reduced bandwidth usage: Can cut JSON payloads by 60–80%.
  • Better client experience: Especially important for mobile users.

Tradeoff:
CPU usage ↑ (to compress/decompress) vs Network bandwidth ↓.

4. How to Enable Gzip in Spring Boot​


Spring Boot provides built-in support (since 1.3+) through properties.

Option 1: Configure in application.properties


Code:
# Enable response compression
server.compression.enabled=true

# Minimum response size before compression is applied
server.compression.min-response-size=1024

# MIME types that should be compressed
server.compression.mime-types=application/json,application/xml,text/html,text/plain,text/css,text/javascript,application/javascript

Option 2: Configure in application.yml


Code:
server:
  compression:
    enabled: true
    mime-types: application/json,application/xml,text/html,text/plain,text/css,text/javascript,application/javascript
    min-response-size: 1KB

5. Example Flow​

Client request:​


Code:
GET /api/users HTTP/1.1
Host: example.com
Accept-Encoding: gzip

Server response (compressed):​


Code:
HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Type: application/json

[binary gzipped data here]

6. Testing​


You can test using cURL:


Code:
curl -H "Accept-Encoding: gzip" -I http://localhost:8080/api/users

You should see:


Code:
Content-Encoding: gzip

Or in Postman, set Accept-Encoding: gzip.

Summary

  • Use Gzip for large payloads or high-traffic APIs.
  • Avoid for tiny or already-compressed responses.
  • In Spring Boot, just enable via application.properties or application.yml.

Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top