การดู : 0

12/04/2026 18:16น.

EP.103 การปรับปรุง Latency ด้วย Binary Protocol และ Protobuf

EP.103 การปรับปรุง Latency ด้วย Binary Protocol และ Protobuf

#Real-time System

#Golang

#Latency Optimization

#Binary Protocol

#WebSocket

ในระบบ WebSocket ที่ต้องสื่อสารข้อมูลแบบ เรียลไทม์ (Real-time) ความเร็วในการรับ–ส่งข้อมูลมีความสำคัญอย่างมาก โดยเฉพาะเมื่อจำนวนผู้ใช้เพิ่มขึ้น หรือปริมาณข้อมูลมากขึ้น

 

การใช้ Binary Protocol และ Protocol Buffers (Protobuf) จะช่วยให้เราลดขนาดข้อมูล, ลด latency และเพิ่ม throughput ของ WebSocket Server ได้อย่างมหาศาล 🚀

 

1. ทำไมต้องใช้ Binary Protocol?

 

โดยทั่วไป WebSocket มักส่งข้อมูลแบบ JSON ซึ่งแม้อ่านง่าย แต่มีข้อเสีย:

  • ขนาดข้อมูลใหญ่
  • ต้อง parse ทุกครั้ง
  • เปลือง CPU และ Bandwidth

 

Binary Protocol เข้ามาช่วยลด overhead เพราะ:

✅ ใช้ขนาดข้อมูลน้อยกว่า JSON ถึง 30–70%
✅ แปลงข้อมูลเป็น binary stream ที่ parse เร็วกว่า
✅ ลด latency ในการส่งข้อมูลระหว่าง server ↔ client

รูปแบบขนาดข้อมูล (bytes)Latency
JSON~120 bytes15 ms
Binary (Protobuf)~40 bytes5 ms

 

2. Protocol Buffers (Protobuf) คืออะไร?

 

Protobuf เป็น format สำหรับการ serialize ข้อมูลที่มีประสิทธิภาพสูงของ Google
เหมาะมากสำหรับระบบที่ต้องการความเร็ว เช่น WebSocket, gRPC หรือ IoT

 

ตัวอย่างการประกาศไฟล์ .proto:

syntax = "proto3";

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

 

จากนั้น generate โค้ด Go ด้วยคำสั่ง:

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

 

3. ตัวอย่างการใช้ Protobuf ใน WebSocket ด้วย Golang

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)
}

✅ ฝั่ง Client ก็สามารถใช้ Protobuf ในการ encode/decode ข้อมูลได้เช่นกัน

 

4. ผลลัพธ์ที่ได้จากการใช้ Binary Protocol + Protobuf

 

  • 🔹 ลด latency ได้มากกว่า 60%
  • 🔹 ประหยัด bandwidth โดยเฉพาะบน mobile
  • 🔹 ขยายระบบได้ง่ายขึ้นโดยไม่ต้องเปลี่ยน schema
  • 🔹 ใช้ CPU น้อยลงในการ parse message

 

5. Best Practices

 

  • 💡 ใช้ gzip หรือ Brotli compression คู่กับ Binary
  • 💡 ออกแบบ schema ของ Protobuf ให้ ยืดหยุ่น เผื่อรองรับ version อนาคต
  • 💡 ใช้ connection pooling เพื่อรองรับผู้ใช้จำนวนมาก

 

🚀 ท้าให้ลอง!

 

ลองเปลี่ยนจาก JSON → Protobuf แล้ววัด latency, throughput และ memory usage คุณจะเห็น performance พุ่งขึ้นอย่างชัดเจน ⚡️

 


 

🔜 Next EP:

 

EP.104 – การใช้ WebSocket Compression และ Delta Updates
เจาะลึกเทคนิคการ บีบอัดข้อมูลแบบเรียลไทม์ และการอัปเดตเฉพาะ “ส่วนที่เปลี่ยนแปลง” เพื่อทำให้ WebSocket Server ของคุณเร็วและเบายิ่งกว่าเดิม! 🔥

 

อ่านบทความ Series อื่นๆ

🔵 Facebook: https://www.facebook.com/superdev.academy.th

🔴 YouTube : Superdev Academy

📸 Instagram: Superdev Academy

🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH

🌐 Website: https://www.superdevacademy.com/