0

Lesson 1: What is gRPC? Why do we need gRPC instead of REST/JSON?

1. What is gRPC?

  • gRPC (pronounced g-R-P-C) is a high-performance, open-source framework initially developed by Google in 2015.
  • The letter "g" initially changed its meaning with each version (e.g., gaRPC, grevious...), but now it simply serves as a prefix representing the framework.
  • At its core, gRPC is an RPC (Remote Procedure Call) technology.
  • Simply put: Instead of writing complex API call code between systems (like REST), gRPC allows an application to directly call a function/method located on another server (hosted on a different server, written in a different language) exactly as if calling a local function within the same program.

2. The Core Mechanics: How does gRPC work? To understand why gRPC is so powerful, we need to look at its two "secret weapons":

  • HTTP/2 as the transport protocol: Unlike HTTP/1.1 used in REST (where each request must open a separate connection or share one but wait sequentially), HTTP/2 allows Multiplexing. Multiple requests and responses can be transmitted in parallel over a single TCP connection. Additionally, HTTP/2 supports header compression (HPACK), which significantly reduces network footprint.
  • Protocol Buffers (Protobuf) as the data format: Instead of using JSON strings (which are human-readable but computationally expensive for machines to parse), gRPC uses Protobuf to compile data into highly compact binary payloads that are extremely optimized for processing speed.

3. Detailed Comparison: gRPC vs REST/JSON

Criteria REST API (JSON) gRPC (Protobuf)
Network Protocol Typically HTTP/1.1 Strictly HTTP/2
Data Format Text (JSON, XML) – Easily human-readable Binary – Machine-readable, hard for humans to read directly
Performance & Speed Slower due to large payload size and time taken to parse JSON Extremely fast and lightweight (up to 3-10x smaller, 5-10x faster parsing speed)
API Contract Optional (often uses Swagger/OpenAPI) Mandatory via .proto definition files (very strict)
Communication Type Mainly Request-Response (1-way) Fully supported: Unary, Server Streaming, Client Streaming, Bidirectional Streaming (real-time 2-way)
Code Generation Requires external libraries (Swagger Codegen...) Native support via protoc for almost all programming languages
Browser Support Good, 100% native Requires additional configuration (gRPC-Web) due to browser limitations with direct HTTP/2 for gRPC

4. Why do we need gRPC instead of REST/JSON? While REST/JSON remains the "king" of Public APIs (allowing third-party, Mobile App, and Web Frontend access), gRPC was created to solve more demanding internal challenges:

  • Microservices Communication (Service-to-Service): In a Microservices architecture, tens or hundreds of services must call each other constantly. Using REST/JSON leads to massive cumulative network latency and bandwidth consumption. gRPC helps minimize this latency significantly.
  • High-Performance and Real-time Systems: Applications like game servers, financial/stock trading systems, IoT services (receiving data from millions of sensors), or chat systems require millisecond response times.
  • Polyglot Programming: Does your company have a backend team using Go, another using Java, and yet another using Python? gRPC handles this seamlessly by compiling a shared .proto file into standard source code for all languages without worrying about data structure inconsistencies.
  • Strict Data Contract Control (API Contract): With REST, it's easy for Frontend and Backend developers to clash over modified JSON fields without updating documentation. With gRPC, if the .proto file doesn't match, the source code won't even compile (compile error), catching errors right at the coding stage.

💡 Practical Advice: gRPC is not a "complete replacement" for REST. REST is still fantastic for Frontend-to-Backend communication. But when it comes to Backend-to-Backend (Microservices), gRPC is the superior choice.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí