protoc --java_out --grpc-java_out or Gradle plugin
Go
protoc-gen-go + protoc-gen-go-grpc
Node/TS
@grpc/proto-loader or ts-proto
Python
grpcio-tools
Modern
buf generate with buf.gen.yaml
3. Implementing Unary RPCs
Example: Unary server (Java)
public class OrderImpl extends OrderServiceGrpc.OrderServiceImplBase { @Override public void get(GetOrderRequest req, StreamObserver<Order> out) { Order o = repo.find(req.getId()) .orElseThrow(() -> Status.NOT_FOUND.withDescription("order").asRuntimeException()); out.onNext(o); out.onCompleted(); }}
RPC Style
Pattern
Unary
1 request → 1 response
When
CRUD, auth, simple lookups
4. Implementing Server Streaming
Use Case
Example
Large lists
Stream rows, bounded memory
Live feeds
Stock prices, telemetry
Backpressure
Use ServerCallStreamObserver.isReady()
5. Implementing Client Streaming
Use Case
Example
Bulk uploads
Stream events; one summary back
Aggregations
Compute over long input stream
Flow control
Server signals readiness
6. Implementing Bidirectional Streaming
Use Case
Example
Chat / collab
Independent send/recv
Real-time sync
State diff streams both ways
Order independence
Frames interleave freely
7. Using gRPC Metadata
Metadata Key
Use
authorization
Bearer token
x-request-id
Correlation ID
grpc-timeout
Auto-set from withDeadline
-bin suffix
Binary metadata: trace-bin
8. Handling gRPC Errors
Status Code
Maps To
OK (0)
Success
CANCELLED (1)
Client cancelled
INVALID_ARGUMENT (3)
HTTP 400
DEADLINE_EXCEEDED (4)
HTTP 504
NOT_FOUND (5)
HTTP 404
ALREADY_EXISTS (6)
HTTP 409
PERMISSION_DENIED (7)
HTTP 403
RESOURCE_EXHAUSTED (8)
HTTP 429
FAILED_PRECONDITION (9)
HTTP 412
UNAUTHENTICATED (16)
HTTP 401
UNAVAILABLE (14)
HTTP 503; safe to retry
INTERNAL (13)
HTTP 500
Note: Use google.rpc.Status with ErrorInfo/BadRequest details for structured errors.