View : 307

23/11/2026 12:54pm

EP.104 Using WebSocket Compression and Delta Updates

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 EnableCompression only 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