Courses 0%
09
Api Protocols and Api Architectures · Chapter 9 of 42

Google Remote Procedure Call (GRPC)

Akhil
Akhil Sharma
30 min

Google Remote Procedure Call (GRPC)

High-performance RPC built on HTTP/2 and Protocol Buffers — 5-10x faster than REST+JSON for service-to-service communication.

GRPC:

Modern RPC Done Right (How Google Talks to Itself)

🎯 Challenge 1: The Function Call Across the Universe Imagine this scenario: You're writing code and you call a function:

result = calculateTax(amount=100, country="US");

Simple, right? The function runs on your computer and returns a result.

Now imagine: What if that function lived on a server in California, but you're in New York? What if it was written in Python, but you're coding in Java?

Pause and think: How can we make calling a remote function feel as natural as calling a local one?

The Answer: gRPC (gRPC Remote Procedure Call) makes remote functions feel local! It's like having a magic telephone that makes distant functions feel like they're right next to you.

Key Features:

✅ Call remote functions like local functions (RPC abstraction)

✅ Uses Protocol Buffers (fast, compact, typed)

✅ HTTP/2 powered (multiplexing, streaming, fast)

✅ Language-agnostic (Java calls Python calls Go...)

✅ Built-in streaming (not just request-response!)

✅ Production-grade (load balancing, auth, tracing)

Key Insight: gRPC treats network calls as if they were function calls in your codebase!

🎬 Interactive Exercise: The Local vs Remote Call

Local Function Call (Traditional):

calculator.py

python

What happens:

  1. Function call happens in same process

  2. Parameters passed via memory

  3. Result returned immediately

  4. Type-safe (Python knows parameter types)

Timeline: < 0.001ms

REST API Approach (Traditional Remote):

// Client code

js

What happens:

  1. Construct HTTP request manually

  2. Serialize data to JSON (string)

  3. Send over network

  4. Parse JSON response

  5. No type safety (could send anything!)

  6. Manual error handling

Timeline: 50-200ms (network latency) Boilerplate: High Type safety: None

gRPC Approach (Modern Remote):

Client code - looks almost like local call!

python
  1. Call looks like local function

  2. Protocol Buffers serialization (binary, fast)

  3. HTTP/2 multiplexing (efficient)

  4. Automatic deserialization

  5. Full type safety (compiler checks!)

  6. Built-in error handling

Timeline: 50-200ms (same network, but faster processing) Boilerplate: Low Type safety: Full

Real-world parallel:

  • REST API = Writing a formal letter to make a request
  • gRPC = Picking up a phone and calling a function directly

The magic: gRPC generates client code that makes remote calls feel local!

🏗️ Building Your First gRPC Service

Step 1: Define the service (.proto file) // calculator.proto

proto

Step 2: Generate code

For Python

Step 3: Implement the server

bash

server.py

python

Step 4: Create the client

client.py

python

Real-world parallel: It's like creating a universal remote control:

  1. Define what buttons exist (.proto)

  2. Generate the remote control (code generation)

  3. Implement what each button does (server)

  4. Press buttons to control things (client)

🎪 The Four Types of gRPC Calls

  1. Unary RPC (Simple Request-Response):

rpc GetUser (UserRequest) returns (UserResponse);

Flow:

Client: "Get user 123" ────→ Server

Client: ←──── "Here's user data" Server

Usage: Most common, like REST GET/POST Example: Fetch user profile, submit form

  1. Server Streaming RPC:

rpc ListProducts (Empty) returns (stream Product);

Flow:

Client: "List all products" ────→ Server

Client: ←──── Product 1 Server

Client: ←──── Product 2 Server

Client: ←──── Product 3 Server

Client: ←──── Product N Server

Client: ←──── [Stream ends] Server

Usage: Large datasets, real-time updates Example: Stock price feed, log streaming, search results

Code example:

Server

python
  1. Client Streaming RPC:

rpc UploadImages (stream Image) returns (UploadSummary);

Flow:

Client: Image 1 ────→ Server

Client: Image 2 ────→ Server

Client: Image 3 ────→ Server

Client: [Done sending] ────→ Server

Client: ←──── "Uploaded 3 images" Server

Usage: Large uploads, batch data Example: File uploads, sensor data collection

  1. Bidirectional Streaming RPC:

rpc Chat (stream Message) returns (stream Message);

Flow:

Client: "Hello" ────→ Server

Client: ←──── "Hi there!" Server

Client: "How are you?" ────→ Server

Client: ←──── "I'm good!" Server

(continues both ways simultaneously)

Usage: Real-time chat, gaming, live collaboration Example: Video calls, multiplayer games, collaborative editing

Real-world parallel:

  • Unary = Phone call: ask question, get answer, hang up

  • Server streaming = Radio broadcast: you listen, they keep sending

  • Client streaming = Talking to voicemail: you keep talking, they listen

  • Bidirectional = Video call: both talk and listen simultaneously

🎮 Decision Game: Which RPC Type?

Match the use case to the right gRPC pattern:

Use Cases:

A. User login

B. Live stock price feed

C. File upload (large video)

D. Chat application

E. Fetch user profile

F. Real-time analytics dashboard

G. Batch import CSV data

H. Multiplayer game

Patterns:

  1. Unary

  2. Server Streaming

  3. Client Streaming

  4. Bidirectional Streaming

Think about the data flow direction...

Answers:

A. User login → Unary (1)

One request (credentials), one response (token)

B. Live stock price feed → Server Streaming (2)

Subscribe once, receive continuous updates

C. File upload → Client Streaming (3)

Send file chunks continuously, get final confirmation

D. Chat application → Bidirectional Streaming (4)

Send and receive messages continuously

E. Fetch user profile → Unary (1)

One request, one response

F. Real-time analytics dashboard → Server Streaming (2)

Dashboard subscribes, server pushes updates

G. Batch import CSV → Client Streaming (3)

Stream rows to server, get summary at end

H. Multiplayer game → Bidirectional Streaming (4)

Send player actions, receive game state updates

🚀 Why gRPC is Fast: The Performance Stack

Layer 1: HTTP/2 (The Foundation)

HTTP/1.1 (REST APIs):

├── One request per connection (head-of-line blocking)

├── Text-based protocol (overhead)

├── No multiplexing (need multiple connections)

└── Large headers (repeated on every request)

Client → Server: [Request 1] Wait... [Request 2] Wait...

HTTP/2 (gRPC):

├── Multiple requests on one connection (multiplexing!)

├── Binary protocol (efficient)

├── Header compression (HPACK)

└── Server push capability

Client → Server: [Req1][Req2][Req3] all at once!

Layer 2: Protocol Buffers (Binary Encoding)

JSON (REST): {"name":"Alice","age":30,"email":"alice@example.com"} Size: 55 bytes

Protobuf (gRPC):

[Binary data]

Size: ~20 bytes (64% smaller!)

Speed comparison:

JSON serialization: 100 microseconds

Protobuf serialization: 10 microseconds (10x faster!)

Layer 3: Multiplexing Magic

REST (Multiple HTTP/1.1 connections):

┌──────┐ ┌──────┐ ┌──────┐

│Conn 1│────→│Req A │

├──────┤ ├──────┤ │

│Conn 2│────→│Req B │ Server

├──────┤ ├──────┤ │

│Conn 3│────→│Req C │

└──────┘ └──────┘ └──────┘

Overhead: 3 TCP connections!

gRPC (Single HTTP/2 connection):

┌──────────────┐

│ One TCP │────→│Req A│

│ Connection │────→│Req B│──→ Server

│ │────→│Req C│

└──────────────┘ Overhead: Just 1 TCP connection!

Real-world performance:

Benchmark: 1000 API calls

REST (JSON over HTTP/1.1):

├── Total time: 5.2 seconds

├── Connections: 50 (with connection pooling)

├── Data transferred: 2.5 MB

└── CPU usage: High (JSON parsing)

gRPC (Protobuf over HTTP/2):

├── Total time: 0.8 seconds (6.5x faster!)

├── Connections: 1

├── Data transferred: 0.6 MB (76% less!)

└── CPU usage: Low (binary parsing)

Mental model: gRPC is like a modern highway (HTTP/2) with efficient cargo containers (Protobuf), while REST is like an old road (HTTP/1.1) with bulky packages (JSON).

🔐 Security & Authentication

Built-in Security Options:

  1. TLS/SSL (Transport Security)

Server with TLS

python
  1. Token-based Authentication

Client sends token with each request

python
  1. Server-side Authentication Check

| # Server validates token

python

Real-world parallel: Like airport security:

  • TLS = Encrypted luggage (can't peek inside)
  • Token auth = Boarding pass (proves you're authorized)
  • Server validation = Security checkpoint (verify credentials)

🚨 Error Handling: The Status Code System

gRPC Status Codes: Here's the properly formatted gRPC status codes:

Status CodeCodeDescription
OK0Success
CANCELLED1Request cancelled by client
UNKNOWN2Unknown error
INVALID_ARGUMENT3Bad parameters
DEADLINE_EXCEEDED4Request timeout
NOT_FOUND5Resource not found
ALREADY_EXISTS6Duplicate resource
PERMISSION_DENIED7No permission
RESOURCE_EXHAUSTED8Rate limit exceeded
FAILED_PRECONDITION9System state issue
ABORTED10Concurrency conflict
OUT_OF_RANGE11Out of bounds
UNIMPLEMENTED12Method not implemented
INTERNAL13Server error
UNAVAILABLE14Service unavailable
UNAUTHENTICATED16Not authenticated

Handling errors:

| # Server raises error

python
python

Real-world parallel: Like HTTP status codes, but more specific to RPC scenarios. 404 Not Found maps to NOT_FOUND, 401 Unauthorized maps to UNAUTHENTICATED, etc.

⚡ Advanced Features

  1. Deadlines & Timeouts

| # Client sets deadline

python
  1. Metadata (Custom Headers)

| # Client sends metadata

python
  1. Interceptors (Middleware)

Logging interceptor

python
  1. Load Balancing

| # Client-side load balancing

python

Real-world parallel: These features are like having a smart postal service:

  • Deadlines = Express delivery deadlines

  • Metadata = Package labels with extra info

  • Interceptors = Security checkpoints scanning all packages

  • Load balancing = Multiple post office branches sharing work

🎪 gRPC vs REST: The Great Comparison

FeatureRESTgRPC
ProtocolHTTP/1.1HTTP/2
Data FormatJSON (text)Protobuf (binary)
API ContractOptional (OpenAPI)Required (.proto)
Browser SupportNativeNeeds grpc-web
StreamingLimitedBuilt-in
PerformanceGoodExcellent
Type SafetyNoneStrong
Code GenerationOptionalRequired
Learning CurveEasyMedium
ToolingMatureGrowing
Best ForPublic APIsMicroservices

When to use REST:

✅ Public-facing APIs (browsers, third-party integration)

✅ Simple CRUD operations

✅ When human-readability matters

✅ When existing tools/libraries are REST-focused

✅ Mobile apps (though gRPC is catching up)

When to use gRPC:

✅ Microservice-to-microservice communication

✅ High-performance requirements (low latency, high throughput)

✅ Streaming data (server/client/bidirectional)

✅ Polyglot environments (multiple languages)

✅ Strong typing and contract enforcement needed

✅ Internal APIs within your organization

Real-world parallel:

  • REST = Postal mail (works everywhere, slower, text-based)
  • gRPC = Private courier (faster, efficient, needs setup)

Hybrid Approach (Common in Practice):

External: Mobile Apps ─────REST/JSON────→ API Gateway

Internal: API Gateway ─────gRPC─────→ Service A ↓ gRPC Service B ↓ gRPC Service C

Benefits: Public REST API + efficient internal gRPC

💡 Final Synthesis Challenge: The Function Call Across the Internet

Complete this comparison: "Traditional REST APIs are like sending a letter to request something. gRPC is like..."

Your answer should include:

  • How it feels to developers
  • Performance characteristics
  • Type safety
  • Streaming capabilities

Take a moment to formulate your complete answer...

The Complete Picture: gRPC is like having a direct phone line to remote functions that:

✅ Makes remote calls feel local (RPC abstraction)

✅ Uses an efficient binary language (Protobuf, not verbose text)

✅ Leverages modern highways (HTTP/2 multiplexing)

✅ Provides strong contracts (typed .proto schemas)

✅ Supports real-time conversations (bidirectional streaming)

✅ Includes built-in security (TLS, auth, interceptors)

✅ Works across languages (language-agnostic)

✅ Optimized for internal services (microservice communication)

This is why:

  • Google uses gRPC for internal microservices (billions of calls/day)
  • Netflix uses gRPC for service mesh communication
  • Uber uses gRPC for high-performance features
  • Square uses gRPC for payment processing services

gRPC transforms network calls from HTTP requests into typed function calls, making distributed systems feel like local codebases!

🎯 Quick Recap: Test Your Understanding Without looking back, can you explain:

  1. What are the four types of gRPC calls and when to use each?
  2. Why is gRPC faster than REST APIs?
  3. How does gRPC maintain type safety?
  4. What's the difference between gRPC and REST for API design?

Mental check: If you can answer these clearly, you've mastered gRPC fundamentals!

🚀 Your Next Learning Adventure Now that you understand gRPC, explore:

Advanced gRPC:

  • gRPC reflection for dynamic clients
  • Advanced streaming patterns and backpressure
  • gRPC-Web for browser support
  • Custom interceptors for auth, logging, tracing

Production Considerations:

  • Service mesh (Istio, Linkerd) with gRPC
  • gRPC load balancing strategies
  • Monitoring and observability (OpenTelemetry)
  • Error handling and retry policies at scale

Related Technologies:

  • GraphQL: Another modern API approach
  • Apache Thrift: Facebook's RPC framework
  • Protocol Buffers deep dive
  • HTTP/2 and HTTP/3 internals

Real-World Case Studies:

  • How Google uses gRPC internally
  • Netflix's gRPC adoption journey
  • Migrating from REST to gRPC patterns
  • Building polyglot microservices with gRPC

Key Takeaways

  1. gRPC uses HTTP/2 and protobuf for high-performance service communication — 5-10x faster than REST+JSON for internal services
  2. Four communication patterns — unary (request-response), server streaming, client streaming, and bidirectional streaming
  3. gRPC is ideal for internal microservice communication — not suitable for browser clients without a proxy (grpc-web)
  4. Service definitions in .proto files generate client and server code — ensuring type safety and reducing boilerplate
Chapter complete!

Course Complete!

You've finished all 42 chapters of

System Design Indermediate

Browse courses
Up next Web Real Time Communication (WEBRTC)
Continue