View : 225

25/04/2026 02:47am

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

๐ŸŒ Website: https://www.superdevacademy.com/en