Skip to content

// Communication & Integration · 02

gRPC

Client stubleads toHTTP/2 + ProtobufServer

Typed contracts, binary payloads, streaming. Fast between services.

How it fits together

gRPC structure«client»Client stubdeadline 2s«interface»inventory.protorpc GetStockrpc WatchStock streamInventory serverNotFound / Unavailablecodegencodegen1 GetStock2 stream updates
createsuses / callspublishes / notifies· numbered steps replay in order
  • inventory.proto creates Client stub (codegen)
  • inventory.proto creates Inventory server (codegen)
  • Client stub uses / calls Inventory server (1 GetStock)
  • Inventory server publishes / notifies Client stub (2 stream updates)

Spot it

  • Chatty service-to-service calls add up
  • Hand-written DTOs drift between client and server
  • You need server push or bidirectional streams

It costs

Not browser-native or human-readable, and it needs HTTP/2 end to end.

In code

public sealed class InventoryService(IStockRepository stock) : Inventory.InventoryBase{    public override async Task<StockReply> GetStock(        StockRequest request,        ServerCallContext context)    {        var item = await stock.FindAsync(request.Sku, context.CancellationToken)            ?? throw new RpcException(new Status(StatusCode.NotFound, $"Unknown {request.Sku}"));​        return new StockReply { Sku = item.Sku, Quantity = item.Quantity };    }​    public override async Task WatchStock(        StockRequest request,        IServerStreamWriter<StockReply> responseStream,        ServerCallContext context)    {        await foreach (var change in stock.Changes(request.Sku, context.CancellationToken))        {            var reply = new StockReply { Sku = change.Sku, Quantity = change.Quantity };            await responseStream.WriteAsync(reply);        }    }}
Go deeperThe full explanation and what to ask for · 1 min readExpand

A .proto file defines services and messages; code generation produces typed clients and servers in each language. Protobuf encodes compactly in binary, and HTTP/2 multiplexes many calls over one connection and supports streaming in both directions.

The classic mistake is breaking the contract: renumbering fields, changing a field type or reusing a deleted field number corrupts data for old clients. Add new fields and reserve removed numbers. Another is calls without deadlines, which can hang indefinitely.

Browsers cannot call gRPC directly; put gRPC-Web or JSON transcoding in front, or keep REST at the edge and gRPC inside. HTTP/2 connections are long-lived, so a layer-4 load balancer pins each client to one backend; use client-side or layer-7 balancing, which a service mesh provides.

Ask for it

  • .proto contract
  • Protocol Buffers
  • unary / server / bidi streaming
  • deadline
  • status codes
  • gRPC-Web

Add an Inventory service defined in inventory.proto with unary GetStock and server-streaming WatchStock, generate client and server stubs from it, set a 2s deadline on every call, map NotFound and Unavailable status codes, and reserve removed field numbers instead of reusing them.