惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
T
Tailwind CSS Blog
博客园_首页
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
J
Java Code Geeks
量子位
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
Check Point Blog
V
Visual Studio Blog
H
Help Net Security
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Always support compressed response in an API service
Ashish Bhatia · 2024-03-10 · via ashishb.net

If you run any web service always enable support for serving compressed responses. It will save egress bandwidth costs for you. And, more importantly, for your users. Over time, the servers as well as client devices have become more powerful, so, compressing/decompressing data on the fly is cheap.

For example,

1
2
3
4
5
6
$ curl https://ashishb.net/ > tmp1.htm && du -shc tmp1.htm
32K
$ curl -H 'Accept-encoding: gzip' https://ashishb.net > tmp1.gz && du -shc tmp1.gz
12K
# curl can automatically decompress the response as well
$ curl --compressed https://ashishb.net

To support this on your servers, handle Accept-encoding. In many cases, you might find standard libraries that support this. For example, for the Go language’s popular Gin framework, it is possible to enable this via middleware.

Here’s a self-contain example demonstrating the same.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Save this file as server.go
// Then one-time setup
// $ go mod init tmp1 && go mod tidy
// Usage: go run server.go
package main

import (
  "net/http"

  "github.com/gin-contrib/gzip"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression))
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run()
}

Now

1
2
3
4
5
6
# This will produce plain-text output
$ curl localhost:8080/ping
# This will produce compressed binary output
$ curl -H 'Accept-encoding: gzip' localhost:8080/ping -o /dev/stdout
# The compressed binary output can be decoded via the following
$ curl -H 'Accept-encoding: gzip' localhost:8080/ping | gunzip -

Note: if you are using Ngnix, you can enable this by adding the following to your Nginx config file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
...
http {
  ...
  server {
    ...
    gzip on;
    gzip_min_length 1000;
    ...
  }
}