Performance First
gRPC is 5-10x faster than REST due to binary encoding and HTTP/2. Perfect for microservices.
gRPC (gRPC Remote Procedure Calls) is a high-performance, open-source RPC framework developed by Google. It enables you to call remote functions as if they were local functions, abstracting away the complexity of network communication.
gRPC uses Protocol Buffers (protobuf) for serialization and HTTP/2 for transport. This combination provides significant performance advantages over traditional REST APIs using JSON over HTTP/1.1. The framework generates client and server code from .proto files, ensuring type safety and reducing boilerplate code.
Key Insight: gRPC is designed for inter-service communication in microservices architectures. It’s optimized for performance, with binary serialization reducing payload sizes by 50-70% compared to JSON, and HTTP/2 providing multiplexing and header compression.
| Feature | REST/JSON | gRPC |
|---|---|---|
| Speed | Slower (text parsing) | Faster (binary) |
| Payload Size | Larger (text) | Smaller (binary) |
| Streaming | Limited | Full support |
| Type Safety | Runtime | Compile-time |
| Code Generation | Manual | Automatic |
| Browser Support | Excellent | Limited |
Protocol Buffers (protobuf) is a binary serialization format. Think of it as a more efficient alternative to JSON.
JSON (text-based):
{ "id": 12345, "name": "John Doe", "age": 30}Size: ~80 bytes
Protocol Buffers (binary):
[encoded binary data]Size: ~25 bytes (3x smaller!)
.proto files define your service contract. They’re like API documentation + code generator input.
syntax = "proto3";
package user_service;
// Message definition (like a struct/class)message User { int32 id = 1; string name = 2; string email = 3; int32 age = 4;}
message GetUserRequest { int32 user_id = 1;}
message GetUserResponse { User user = 1;}
// Service definitionservice UserService { // Unary RPC: one request, one response rpc GetUser(GetUserRequest) returns (GetUserResponse);
// Server streaming: one request, multiple responses rpc ListUsers(GetUserRequest) returns (stream User);
// Client streaming: multiple requests, one response rpc CreateUsers(stream User) returns (CreateUsersResponse);
// Bidirectional streaming: multiple requests, multiple responses rpc ChatUsers(stream ChatMessage) returns (stream ChatMessage);}Field numbers (1, 2, 3…) are important:
Common types:
int32, int64 - Integersfloat, double - Floating pointbool - Booleanstring - UTF-8 stringbytes - Raw bytesrepeated - Arrays/listsmap - Key-value pairsOne request, one response. Like a function call.
service UserService { rpc GetUser(GetUserRequest) returns (GetUserResponse);}Flow:
Client → Request → ServerClient ← Response ← ServerOne request, multiple responses. Server sends stream of data.
service UserService { rpc ListUsers(GetUserRequest) returns (stream User);}Flow:
Client → Request → ServerClient ← User 1 ← ServerClient ← User 2 ← ServerClient ← User 3 ← Server...Use cases:
Multiple requests, one response. Client sends stream of data.
service UserService { rpc CreateUsers(stream User) returns (CreateUsersResponse);}Flow:
Client → User 1 → ServerClient → User 2 → ServerClient → User 3 → ServerClient ← Response ← ServerUse cases:
Multiple requests, multiple responses. Both sides stream.
service ChatService { rpc Chat(stream ChatMessage) returns (stream ChatMessage);}Flow:
Client → Message 1 → ServerClient ← Message 2 ← ServerClient → Message 3 → ServerClient ← Message 4 ← Server...Use cases:
gRPC uses status codes (similar to HTTP but different):
| Code | Meaning | HTTP Equivalent |
|---|---|---|
OK | Success | 200 |
INVALID_ARGUMENT | Bad request | 400 |
NOT_FOUND | Resource not found | 404 |
ALREADY_EXISTS | Conflict | 409 |
PERMISSION_DENIED | Forbidden | 403 |
UNAUTHENTICATED | Unauthorized | 401 |
RESOURCE_EXHAUSTED | Rate limited | 429 |
INTERNAL | Server error | 500 |
Typical performance differences:
| Metric | REST/JSON | gRPC |
|---|---|---|
| Latency | 10-50ms | 2-10ms |
| Throughput | 1K-10K req/s | 10K-100K req/s |
| Payload Size | 100% | 30-50% |
| CPU Usage | Higher | Lower |
Why gRPC is faster:
At the code level, gRPC translates to service interfaces, message types, and streaming handlers.
Performance First
gRPC is 5-10x faster than REST due to binary encoding and HTTP/2. Perfect for microservices.
Streaming Support
gRPC supports four streaming types: unary, server, client, and bidirectional. REST can’t do this.
Strong Typing
Protocol Buffers provide compile-time type safety. Schema defines contract, code is generated.
Service Contracts
.proto files define service contracts. Generate client/server code in any language automatically.