View : 222

25/04/2026 02:47am

EP.103 Optimizing Latency with Binary Protocol and Protobuf

EP.103 Optimizing Latency with Binary Protocol and Protobuf

#WebSocket

#Protobuf

#Binary Protocol

#Golang

#Real-time

In real-time WebSocket communication systems, speed and efficiency are critical especially when user volume or data payload increases.

 

By leveraging Binary Protocol and Protocol Buffers (Protobuf), you can dramatically reduce data size, lower latency, and significantly boost your WebSocket server’s throughput. 🚀

 

1. Why Use a Binary Protocol?

 

WebSockets typically transmit data in JSON format, which is easy to read but comes with several drawbacks:

  • Large data payload
  • Requires parsing every time
  • Consumes CPU and bandwidth

 

Binary protocols help reduce this overhead because:

✅ They use 30–70% less data than JSON
✅ Convert data into a binary stream that's faster to parse
✅ Lower latency between server ↔ client

FormatData Size (bytes)Latency
JSON~120 bytes15 ms
Binary (Protobuf)~40 bytes5 ms

 

2. What Is Protocol Buffers (Protobuf)?

 

Protobuf is a high-efficiency serialization format developed by Google.
It’s ideal for speed-critical systems such as WebSockets, gRPC, or IoT networks.

 

Example .proto definition:

syntax = "proto3";

message ChatMessage {
  string sender = 1;
  string content = 2;
  int64 timestamp = 3;
}

 

You can then generate Go code using:

protoc --go_out=. --go_opt=paths=source_relative chat.proto

 

3. Example: Using Protobuf with WebSocket in Go

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/websocket"
	"google.golang.org/protobuf/proto"
)

var upgrader = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool { return true },
}

func handleConnection(w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	for {
		_, data, err := conn.ReadMessage()
		if err != nil {
			break
		}

		var msg ChatMessage
		if err := proto.Unmarshal(data, &msg); err == nil {
			fmt.Printf("[%s]: %s\n", msg.Sender, msg.Content)
		}
	}
}

func main() {
	http.HandleFunc("/ws", handleConnection)
	fmt.Println("WebSocket Server running on :8080")
	http.ListenAndServe(":8080", nil)
}

✅ The client side can also encode/decode messages using Protobuf — enabling fast and lightweight communication.

 

4. Results from Using Binary Protocol + Protobuf

 

🔹 Reduce latency by over 60%
🔹 Save bandwidth — especially for mobile users
🔹 Scale the system without changing schema
🔹 Lower CPU usage during message parsing

 

5. Best Practices

 

💡 Use Gzip or Brotli compression along with binary data
💡 Design your Protobuf schema to be flexible for future versions
💡 Use connection pooling to manage large user bases efficiently

 

🚀 Challenge for You

 

Try switching from JSON to Protobuf. Measure your system's latency, throughput, and memory usage and see how performance skyrockets! ⚡️

 


 

🔜 Next EP

 

EP.104 – WebSocket Compression and Delta Updates
Dive into real-time compression techniques and learn how to update only the changed parts of data to make your WebSocket server faster and lighter than ever! 🔥

 

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