Development

gRPC คืออะไร? คู่มือ High-Performance Microservices ด้วย Protobuf สำหรับ SME ไทย 2026

เจาะลึก gRPC โปรโตคอลสำหรับ Microservices ที่เร็วกว่า REST 5-10 เท่า ผ่าน HTTP/2 + Protobuf พร้อมตัวอย่างจริงด้วย Node.js และ Go สำหรับ SME ไทย

AF
ADS FIT Team
·8 นาที
Share:

# gRPC คืออะไร? คู่มือ High-Performance Microservices ด้วย Protobuf สำหรับ SME ไทย 2026

เมื่อระบบของธุรกิจถูกแยกออกเป็น Microservices หลายสิบตัว การที่บริการแต่ละตัวคุยกันด้วย REST API + JSON เริ่มกลายเป็นปัญหาคอขวด เพราะ JSON มี Overhead สูง, ไม่มี Schema ที่บังคับใช้, และ HTTP/1.1 เปิด Connection ใหม่ทุกครั้งที่เรียก ทำให้ Latency รวมของระบบยิ่งเพิ่มขึ้นเมื่อ Microservices ขยายตัว

gRPC (gRPC Remote Procedure Call) จาก Google คือคำตอบของปัญหานี้ เป็น RPC framework ที่วิ่งบน HTTP/2 ใช้ Protocol Buffers (Protobuf) เป็น Binary serialization ทำให้ Payload เล็กลง 5-8 เท่าเมื่อเทียบกับ JSON และยังรองรับ Streaming ทั้งสองทิศทางในตัว

บทความนี้จะอธิบาย gRPC สำหรับ SME ไทยที่กำลังย้ายจาก REST มาเป็น Microservices Architecture ตั้งแต่หลักการ, Protobuf, 4 รูปแบบ Streaming, การเปรียบเทียบกับ REST/GraphQL, ไปจนถึงตัวอย่างใช้งานจริงด้วย Node.js และ Go

gRPC คืออะไร และทำงานยังไง

gRPC สร้างขึ้นบนหลักการ Contract-First คือกำหนด Interface ของ Service ในไฟล์ `.proto` ก่อน แล้วใช้ Compiler `protoc` สร้างโค้ด Stub ทั้งฝั่ง Server และ Client โดยอัตโนมัติให้รองรับ 11+ ภาษา ตั้งแต่ Go, Java, Python, Node.js, C#, ไปจนถึง Swift และ Kotlin

จุดเด่นที่ทำให้ gRPC แข็งแกร่งคือ HTTP/2 Multiplexing ที่ส่งหลายคำขอผ่าน Connection เดียว, Binary Encoding ที่ Payload เล็กกว่า JSON 5-8 เท่า, Bi-directional Streaming สำหรับ Use Case แบบ Real-time, Strong Typing ที่ป้องกัน Bug จาก Schema ไม่ตรง, และ Code Generation ที่ลดเวลาเขียน Boilerplate

| คุณสมบัติ | gRPC | REST + JSON |

|---|---|---|

| Transport | HTTP/2 | HTTP/1.1 หรือ HTTP/2 |

| Payload | Binary (Protobuf) | Text (JSON) |

| Schema | บังคับด้วย .proto | ไม่บังคับ (OpenAPI optional) |

| Streaming | 4 modes ในตัว | Server-Sent Events เท่านั้น |

| Browser support | ต้องใช้ gRPC-Web proxy | รองรับโดยตรง |

| Code Gen | อัตโนมัติ 11+ ภาษา | ต้องเขียน SDK เอง |

| Latency (p99) | 5-15 ms | 30-80 ms |

Protocol Buffers: หัวใจของ gRPC

Protobuf เป็น IDL (Interface Definition Language) ที่ใช้กำหนด Message Schema และ Service Contract ตัวอย่างไฟล์ `.proto` พื้นฐาน:

```

syntax = "proto3";

package shop;

service ProductService {

rpc GetProduct (ProductRequest) returns (Product);

rpc ListProducts (ListRequest) returns (stream Product);

rpc UploadImages (stream ImageChunk) returns (UploadResult);

rpc Chat (stream ChatMessage) returns (stream ChatMessage);

}

message Product {

string id = 1;

string name = 2;

double price = 3;

repeated string tags = 4;

}

```

ข้อดีของ Protobuf คือ Field number (เลขหลัง `=`) ทำหน้าที่เป็น "Wire Format Identifier" ทำให้สามารถเพิ่ม/ลบ Field ได้โดยไม่ทำลาย Backward Compatibility ตราบใดที่ Field number เดิมไม่ถูกนำกลับมาใช้

4 Streaming Patterns ที่ gRPC รองรับ

  • **Unary RPC**: Request ครั้งเดียว ตอบครั้งเดียว เหมือน REST ทั่วไป เช่น `GetProduct(id)`
  • **Server Streaming**: Client ส่ง Request เดียว Server ตอบหลาย Message เช่น Stream รายการสินค้า, Live news feed
  • **Client Streaming**: Client ส่งหลาย Message Server ตอบครั้งเดียวสุดท้าย เช่น Upload ไฟล์ขนาดใหญ่เป็น Chunk
  • **Bidirectional Streaming**: ทั้งสองฝั่งส่ง/รับพร้อมกันแบบ Async เช่น Chat แบบ Real-time, การควบคุมเครื่องจักรในโรงงาน
  • How-To: สร้าง gRPC Service ด้วย Node.js ใน 6 ขั้นตอน

  • ติดตั้ง dependencies: `npm install @grpc/grpc-js @grpc/proto-loader`
  • สร้างไฟล์ `product.proto` กำหนด Service และ Message ตามตัวอย่างด้านบน
  • โหลด Proto ใน Server: `const packageDef = protoLoader.loadSync('product.proto');` แล้ว `const proto = grpc.loadPackageDefinition(packageDef);`
  • เขียน Implementation: สร้าง Object ที่มี Method ตรงกับชื่อใน Service เช่น `getProduct(call, callback) { callback(null, {id: call.request.id, name: 'iPhone'}); }`
  • Bind Server: `server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => server.start());`
  • Client เรียกใช้: `const client = new proto.shop.ProductService('localhost:50051', grpc.credentials.createInsecure()); client.getProduct({id: '123'}, (err, res) => console.log(res));`
  • สำหรับ Production ควรใช้ TLS แทน Insecure credentials และใช้ Connection Pool เพื่อ Reuse Connection

    เปรียบเทียบ gRPC vs REST vs GraphQL

    | ปัจจัย | gRPC | REST | GraphQL |

    |---|---|---|---|

    | Performance | สูงสุด (Binary, HTTP/2) | กลาง | กลาง |

    | Browser native | ต้องใช้ gRPC-Web | รองรับเต็ม | รองรับเต็ม |

    | Schema enforcement | แข็งแกร่ง (Protobuf) | Optional (OpenAPI) | แข็งแกร่ง (SDL) |

    | Flexible queries | ไม่ | ไม่ | ใช่ (Client เลือก Field) |

    | Streaming | 4 modes | SSE (1-way) | Subscription |

    | เหมาะกับ | Service-to-service | Public API | Mobile/Frontend complex |

    ในทางปฏิบัติ SME ส่วนใหญ่ใช้ทั้ง 3 ตัวร่วมกัน คือ REST/GraphQL เป็น Public Gateway ให้ Frontend และ gRPC สำหรับสื่อสารระหว่าง Microservices ภายใน

    Use Cases ที่ gRPC เหมาะที่สุด

  • **Microservices Internal Communication**: เร็วกว่า REST 5-10 เท่า ลด Latency รวมของระบบ
  • **Real-time Trading / Order Processing**: Bi-directional streaming รองรับ Latency ต่ำ
  • **IoT Telemetry Aggregation**: รับข้อมูลจาก Edge devices หลายล้านตัวพร้อมกัน
  • **Polyglot Microservices**: ทีมที่ใช้หลายภาษา (Go + Python + Node.js) สามารถใช้ Schema เดียวกันได้
  • **Mobile Apps**: gRPC + Protobuf ลดการใช้ Bandwidth ของ Mobile data
  • Best Practices สำหรับ Production

  • **Versioning**: ใส่ Version ใน Package name เช่น `shop.v1.ProductService` แล้วเมื่อ Breaking change ค่อยสร้าง v2
  • **Deadlines / Timeouts**: ตั้ง Deadline ทุก RPC call (`metadata + grpc.deadline`) ป้องกัน Cascading failure
  • **Retry Policies**: ใช้ gRPC Retry Config ใน Service Config สำหรับ Idempotent methods เท่านั้น
  • **Observability**: เปิด OpenTelemetry tracing เพื่อดู Span ระหว่าง Service และตั้ง Health Checking ตาม `grpc.health.v1.Health`
  • **Authentication**: ใช้ TLS + JWT/OAuth2 ผ่าน Metadata header `authorization: Bearer <token>`
  • **Load Balancing**: ใช้ Client-side LB หรือ Service Mesh (Envoy/Istio/Linkerd) เพราะ HTTP/2 แชร์ Connection ทำให้ L4 LB เก่าใช้ไม่ได้ผล
  • **gRPC-Web for Browser**: ถ้าต้องการให้ Browser เรียก gRPC ใช้ Envoy Proxy แปลงเป็น gRPC-Web protocol
  • ข้อจำกัดที่ต้องรู้

  • **Browser Support จำกัด**: ต้องผ่าน gRPC-Web proxy (Envoy) เพราะ Browser ไม่ Support HTTP/2 trailer
  • **Debugging ยากกว่า REST**: ต้องใช้ tools เฉพาะอย่าง `grpcurl`, `BloomRPC`, `Postman gRPC tab` แทน curl
  • **Curve การเรียนรู้สูงกว่า REST**: ทีมต้องเรียนรู้ Protobuf, Code generation, HTTP/2
  • **Caching ทำได้ยาก**: ไม่มี HTTP cache header เหมือน REST ต้องทำ Cache เอง
  • สรุป + Call to Action

    gRPC เป็นเครื่องมือที่ทรงพลังสำหรับ SME ไทยที่กำลังเปลี่ยนผ่านจาก Monolith เป็น Microservices หรือต้องการลด Latency ของระบบที่มี Service Internal เยอะ ๆ การลงทุนเรียนรู้ Protobuf และ Code generation จะให้ผลตอบแทนระยะยาวทั้งในเรื่อง Performance, Type safety, และ Developer Experience

    Key Takeaways:

  • ใช้ gRPC สำหรับ Service-to-service ภายใน และเก็บ REST/GraphQL ไว้สำหรับ Public API ที่ต้องให้ Browser/Third-party เรียก
  • ออกแบบ `.proto` ให้ Backward compatible ตั้งแต่แรก ระวัง Field number ห้ามนำกลับมาใช้
  • ตั้ง Deadline และ Retry policy ทุก call ป้องกัน Cascading failure
  • ใช้ Service Mesh ช่วยจัดการ Load Balancing, mTLS, Observability โดยไม่ต้องเขียนเอง
  • หากธุรกิจของคุณกำลังจะปรับสถาปัตยกรรมเป็น Microservices หรือต้องการ Audit ระบบ Backend ที่มีอยู่ว่าควรย้ายส่วนไหนเป็น gRPC [ติดต่อทีม ADS FIT](https://www.adsfit.co.th/contact) เพื่อรับคำปรึกษาออกแบบ Architecture ที่เหมาะกับขนาดธุรกิจของคุณ หรืออ่านบทความที่เกี่ยวข้องอย่าง Saga Pattern, Domain-Driven Design และ Webhooks Event-Driven Architecture เพื่อต่อยอดความเข้าใจการออกแบบระบบขนาดใหญ่

    Tags

    #gRPC#Protobuf#Microservices#REST API#HTTP/2#Streaming

    สนใจโซลูชันนี้?

    ปรึกษาทีม ADS FIT ฟรี เราพร้อมออกแบบระบบที่ฟิตกับธุรกิจของคุณ

    ติดต่อเรา →

    บทความที่เกี่ยวข้อง