Friday, November 21, 2025

Architecture - gRPC (General Remote Procedure Call) Framework

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

ComponentPurpose
Protocol Buffers (Protobuf)Language-neutral binary serialization format
.proto fileDefines contracts (messages + service methods)
gRPC client & server stubsAuto-generated code based on .proto
HTTP/2Transport protocol (multiplexing, streaming)

⚡ Why is gRPC so Fast?

🚀 Because it uses:

  1. Binary protocol (Protocol Buffers) → smaller & faster than JSON
  2. HTTP/2 → multiplexing, header compression
  3. Streaming built-in
  4. Strongly typed contracts

Result:

  1. Super low latency
  2. High throughput
  3. Efficient for microservices

🟢 gRPC vs REST

FeaturegRPCREST
ProtocolHTTP/2HTTP/1.1
Data FormatProtobuf (binary)JSON (text)
SpeedFasterSlower
ContractStrong, auto-generatedOptional (Swagger)
StreamingFull support (4 types)Limited
Browser supportWeak (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

TypeDescription
UnarySingle request → single response
Server streamingClient sends 1 request → server streams responses
Client streamingClient streams requests → server sends 1 response
Bi-directional streamingBoth sides stream simultaneously

📄 Example .proto File

syntax = "proto3"; service OrderService { rpc GetOrder (OrderRequest) returns (OrderResponse); rpc StreamOrders (OrderRequest) returns (stream OrderResponse); } message OrderRequest { int32 orderId = 1; } message OrderResponse { int32 orderId = 1; string status = 2; }

🧩 C# Server Implementation

public class OrderServiceImpl : OrderService.OrderServiceBase { public override Task<OrderResponse> GetOrder(OrderRequest request, ServerCallContext context) { return Task.FromResult(new OrderResponse { OrderId = request.OrderId, Status = "Processing" }); } }

🧩 C# Client Implementation

var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new OrderService.OrderServiceClient(channel); var response = await client.GetOrderAsync(new OrderRequest { OrderId = 1 }); Console.WriteLine(response.Status);

🧲 Where gRPC is Used (Real World)

  1. Google’s internal microservices
  2. Netflix → high-performance service communication
  3. Uber → low-latency RPC
  4. Dropbox
  5. Cloud Native (Kubernetes uses gRPC internally)

🖥 In .NET Ecosystem

.NET has first-class gRPC support, including:

  1. gRPC hosting via ASP.NET Core
  2. gRPC client library
  3. gRPC-Web
  4. Protobuf serialization
  5. 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

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...