25/04/2026 02:47am

EP.104 Using WebSocket Compression and Delta Updates
#Real-time System
#Data Optimization
#Delta Updates
#WebSocket Compression
#Go
#Performance Optimization
When a WebSocket system needs to handle a large amount of real-time data such as in chat systems, online games, or live dashboards the performance of data transmission becomes the key factor.
Techniques that help the system run faster while using less bandwidth are:
- โ Data compression (WebSocket Compression)
- โ Updating only the parts that have changed (Delta Updates)
These two techniques combined will make your system โlight but powerfulโ truly suitable for production-grade workloads. ๐
๐งฉ 1. What is WebSocket Compression?
Normally, WebSocket sends raw data every time, even if only one character changes. Enabling the Compression Extension (permessage-deflate) compresses the data before sending, significantly reducing the payload size.
๐น Example of enabling Compression in Go:
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
EnableCompression: true, // enable compression
CheckOrigin: func(r *http.Request) bool { return true },
}
func handler(w http.ResponseWriter, r *http.Request) {
conn, _ := upgrader.Upgrade(w, r, nil)
defer conn.Close()
conn.EnableWriteCompression(true) // enable compression for message
for {
mt, msg, err := conn.ReadMessage()
if err != nil {
break
}
log.Printf("Received: %s", msg)
conn.WriteMessage(mt, []byte("Compressed response OK!"))
}
}
func main() {
http.HandleFunc("/ws", handler)
log.Println("WebSocket Server running on :8080")
http.ListenAndServe(":8080", nil)
}
โ Result:
- Data size reduced by 30โ80%
- Save bandwidth, especially on mobile networks
- Latency is reduced when data has repeated patterns
โ๏ธ 2. What is Delta Updates?
Delta Update means sending only the parts of the data that have changed, instead of sending the entire object every time.
// Regular method
{
"user_id": 101,
"score": 4200
}
If only the score changes, sending the full object repeatedly is wasteful. So we send only the โdelta,โ like:
{ "score": 4300 }
Or in Binary Protocol, only the changed fields are encoded.
๐น Example of Delta Update in Go:
type UserData struct {
ID int `json:"id"`
Name string `json:"name"`
Score int `json:"score"`
}
var oldData = UserData{ID: 1, Name: "Alice", Score: 4200}
var newData = UserData{ID: 1, Name: "Alice", Score: 4300}
func getDelta(old, new UserData) map[string]interface{} {
delta := make(map[string]interface{})
if old.Score != new.Score {
delta["score"] = new.Score
}
if old.Name != new.Name {
delta["name"] = new.Name
}
return delta
}
func main() {
d := getDelta(oldData, newData)
fmt.Println("Delta Update:", d)
}
Result:
Delta Update: map[score:4300]
Then we can send this delta through WebSocket to the client immediately.
๐ง 3. Combining Compression + Delta Updates
When combining WebSocket Compression with Delta Updates, it results in communication that is โboth fast and light.โ
- Compression reduces payload size
- Delta reduces the amount of data sent
Suitable for systems like:
- Real-time dashboards
- Multiplayer game
- IoT data stream
- Live trading platform
๐ก 4. Best Practices
- Enable
EnableCompressiononly when necessary, because there is overhead from compressing/decompressing data - Use Binary + Delta Encoding together for best results
- Store the latest snapshot of data on the server side for accurate delta calculation
- Verify that the client supports delta logic before enabling it
๐ Give it a try!
Try enabling Compression + Delta Updates in your WebSocket Server and test how much bandwidth is saved and how much latency is improved ๐ช
๐ Next EP:
In EP.105, weโll talk about: "Event Queue Management and Prioritization in WebSocket Systems" How to organize, prioritize, and efficiently dispatch real-time events at scale โ๏ธ
Read more
๐ต Facebook: Superdev Academy
๐ด YouTube: Superdev Academy
๐ธ Instagram: Superdev Academy
๐ฌ TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH
๐ Website: https://www.superdevacademy.com/en