What is RPC? gRPC Introduction.
Summary of What is RPC? gRPC Introduction. from ByteByteGo · Published 2022-12-01 · Views: 760,453
This note was generated automatically from the video transcript.
TL;DR
gRPC is Google’s open‑source RPC framework that couples strongly‑typed Protocol Buffers with HTTP/2 to deliver high‑performance, language‑agnostic inter‑service communication. It excels for microservice‑to‑microservice calls inside data centers and for mobile clients, but browsers lack the low‑level HTTP/2 control needed for native gRPC.
Key Insights
- gRPC uses Protocol Buffers as its default binary serialization, giving compact messages and auto‑generated, type‑safe client/server stubs.
- Built on HTTP/2, gRPC can multiplex many RPC calls over a single long‑lived TCP connection, dramatically reducing connection overhead.
- The generated client stub hides the wire format; developers call it like a local function, while the framework handles encoding, transport, and decoding.
- Benchmarks cited in the video claim gRPC is ~5× faster than JSON‑based REST due to binary encoding and HTTP/2 efficiencies.
- Browsers cannot directly use gRPC because they don’t expose the required HTTP/2 primitives; gRPC‑Web works via a proxy but with a reduced feature set.
- gRPC’s language‑agnostic code generation lets each microservice pick its preferred programming language without sacrificing API compatibility.
- Ideal use‑cases: high‑throughput intra‑datacenter microservice communication and bandwidth‑constrained mobile clients; less suited for public web APIs consumed by browsers.
Detailed Breakdown
1. What is an RPC?
- A local procedure call executes code within the same process.
- A remote procedure call (RPC) lets one machine invoke code on another machine, presenting the remote call as if it were local to the caller.
2. gRPC Overview
- Open‑source framework released by Google in 2016, a rewrite of Google’s internal RPC system.
- Provides a type‑safe, production‑grade API surface for connecting large numbers of microservices across data centers.
3. Protocol Buffers (proto)
- Language‑agnostic schema definition (
.protofiles) describing message structures and service RPC signatures. - Tooling generates data access classes and client/server stubs for languages such as Go, Java, C#, Python, etc.
- Binary format is compact and fast to serialize/deserialize, outperforming text formats like JSON.
4. High‑Performance Foundations
Binary Encoding
- Protocol Buffers’ binary layout reduces payload size and CPU cycles; the video cites a 5× speed advantage over JSON.
HTTP/2 Transport
- gRPC leverages HTTP/2 streams, enabling:
- Multiplexing: many concurrent RPCs share one TCP connection.
- Header compression (HPACK) and binary framing, lowering latency.
- Flow control and server push capabilities (though not commonly used by gRPC).
flowchart LR
client["Order Service (gRPC Client)"] --> stub["Client Stub (generated)"]
stub -->|Proto‑encoded request| http2["HTTP/2 Stream"]
http2 --> server["Payment Service (gRPC Server)"]
server -->|Proto‑encoded response| http2
http2 --> stub
stub --> client
5. End‑to‑End Call Flow
- Client code invokes a method on the generated client stub.
- Stub serializes arguments into a Protocol Buffer message.
- Message is placed into an HTTP/2 data frame and sent over the shared TCP connection.
- Server receives the frame, deserializes the protobuf, and dispatches to the service implementation.
- Service returns a response object; the server stub serializes it, sends it back via HTTP/2, and the client stub deserializes for the caller.
6. Browser Limitations & gRPC‑Web
- Browsers expose only high‑level HTTP/1.1‑style APIs (Fetch, XHR) and lack direct access to HTTP/2 stream IDs or custom frame types.
- gRPC‑Web introduces a lightweight proxy that translates between browser‑friendly HTTP/1.1/2 requests and native gRPC on the server, but it does not support all gRPC features (e.g., bidirectional streaming).
7. When to Use gRPC
- Microservice communication inside data centers where low latency, high throughput, and language heterogeneity matter.
- Mobile clients where bandwidth and battery are limited; binary payloads and multiplexed connections conserve resources.
- Internal APIs where you control both client and server; public web APIs for browsers are better served by REST/JSON or GraphQL.
Trade-offs and Gotchas
- Pros: Strong typing, auto‑generated code, high throughput, multiplexed connections, language‑agnostic.
- Cons: Steeper learning curve than REST, binary payloads are not human‑readable, debugging requires protobuf tooling.
- Browser incompatibility: Direct gRPC calls from browsers are not possible; gRPC‑Web adds latency and feature gaps.
- Operational complexity: Requires HTTP/2‑aware load balancers and proxies; older infrastructure may need upgrades.
- Versioning: Changing
.protoschemas must follow protobuf compatibility rules (e.g., never renumber fields) to avoid breaking existing services.
Takeaways
- Use gRPC when you need fast, type‑safe, multiplexed RPC between services you control.
- Define your API once in a
.protofile; let the tooling generate client and server stubs for any supported language. - Remember that binary protobuf messages are not self‑describing; maintain versioned
.protofiles in source control. - For public web APIs, prefer REST/JSON or GraphQL unless you can accept the overhead of a gRPC‑Web proxy.
- Ensure your deployment stack (load balancers, service mesh, observability) fully supports HTTP/2.
Glossary
- RPC (Remote Procedure Call): A protocol that allows a program to cause a procedure to execute on another address space (commonly on another machine).
- Protocol Buffers: Google’s language‑neutral, platform‑neutral, extensible mechanism for serializing structured data; defined via
.protoschema files. - HTTP/2: The second major version of HTTP, introducing binary framing, multiplexed streams, header compression, and server push.
- Client Stub: Auto‑generated client‑side code that provides local method signatures which internally handle serialization, transport, and deserialization.
- gRPC‑Web: A bridge that enables browser‑based applications to call gRPC services via a proxy that translates between browser‑compatible HTTP and native gRPC.
Leave a comment