gRPC is a high-performance, open-source, RPC (Remote Procedure Call) framework developed by Google.
It allows services to communicate directly with each other using strongly typed contracts (.proto files) over a fast binary protocol (HTTP/2).
In simple terms:
gRPC allows one service to call a method in another service as if it were calling a local function, but over the network.
🧱 Key Components of gRPC
| Component | Purpose |
|---|---|
| Protocol Buffers (Protobuf) | Language-neutral binary serialization format |
| .proto file | Defines contracts (messages + service methods) |
| gRPC client & server stubs | Auto-generated code based on .proto |
| HTTP/2 | Transport protocol (multiplexing, streaming) |
⚡ Why is gRPC so Fast?
🚀 Because it uses:
- Binary protocol (Protocol Buffers) → smaller & faster than JSON
- HTTP/2 → multiplexing, header compression
- Streaming built-in
- Strongly typed contracts
Result:
- Super low latency
- High throughput
- Efficient for microservices
🟢 gRPC vs REST
| Feature | gRPC | REST |
|---|---|---|
| Protocol | HTTP/2 | HTTP/1.1 |
| Data Format | Protobuf (binary) | JSON (text) |
| Speed | Faster | Slower |
| Contract | Strong, auto-generated | Optional (Swagger) |
| Streaming | Full support (4 types) | Limited |
| Browser support | Weak (needs gRPC-Web) | Native |
✳️ When to Use gRPC
Use gRPC when you need:
✔ High performance
✔ Service-to-service communication
✔ Real-time communication
✔ Low latency mobile/IoT calls
✔ Strong contract enforcement
Not ideal for:
❌ Public APIs
❌ Browser-to-server calls (requires gRPC-web wrapper)
🎯 Types of gRPC Calls
| Type | Description |
|---|---|
| Unary | Single request → single response |
| Server streaming | Client sends 1 request → server streams responses |
| Client streaming | Client streams requests → server sends 1 response |
| Bi-directional streaming | Both sides stream simultaneously |
📄 Example .proto File
🧩 C# Server Implementation
🧩 C# Client Implementation
🧲 Where gRPC is Used (Real World)
- Google’s internal microservices
- Netflix → high-performance service communication
- Uber → low-latency RPC
- Dropbox
- Cloud Native (Kubernetes uses gRPC internally)
🖥 In .NET Ecosystem
.NET has first-class gRPC support, including:
- gRPC hosting via ASP.NET Core
- gRPC client library
- gRPC-Web
- Protobuf serialization
- Interceptors (similar to middleware)
🧠Simple Interview-Perfect Definition
gRPC is a high-performance RPC framework using HTTP/2 and Protocol Buffers that enables fast, efficient communication between microservices with strongly typed contracts and support for streaming.
No comments:
Post a Comment