🌐 Web Development

gRPC

gRPC Remote Procedure Call

Google's high-performance RPC framework. Uses Protocol Buffers. Based on HTTP/2.

Detailed Explanation

gRPC (gRPC Remote Procedure Call) is a high-performance RPC framework developed by Google. It uses Protocol Buffers (protobuf) as its default serialization format and operates over HTTP/2.

Unlike the text-based JSON of REST APIs, gRPC uses a binary protocol to provide smaller payloads and faster serialization/deserialization. It is particularly strong in communication between microservices.

Cloud-native tools such as Kubernetes, Envoy, and etcd have adopted gRPC, so it has become the de facto standard for microservice architecture.

Code Example

Defining a .proto file

syntax = "proto3"; package user; // Service definition service UserService { // Single request-response rpc GetUser(GetUserRequest) returns (User); // Server streaming rpc ListUsers(ListUsersRequest) returns (stream User); // Bidirectional streaming rpc Chat(stream ChatMessage) returns (stream ChatMessage); } // Message type definitions message User { int32 id = 1; string name = 2; string email = 3; } message GetUserRequest { int32 id = 1; }

Node.js server implementation

const grpc = require('@grpc/grpc-js'); const protoLoader = require('@grpc/proto-loader'); // proto 파일 λ‘œλ“œ const packageDefinition = protoLoader.loadSync('user.proto'); const userProto = grpc.loadPackageDefinition(packageDefinition).user; // μ„œλΉ„μŠ€ κ΅¬ν˜„ const server = new grpc.Server(); server.addService(userProto.UserService.service, { getUser: (call, callback) => { const user = { id: call.request.id, name: '홍길동', email: 'hong@example.com' }; callback(null, user); }, listUsers: (call) => { users.forEach(user => call.write(user)); call.end(); }, }); server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => { server.start(); });

Node.js client

const client = new userProto.UserService( 'localhost:50051', grpc.credentials.createInsecure() ); // Single request client.getUser({ id: 1 }, (err, user) => { console.log(user); // { id: 1, name: '홍길동', email: '...' } }); // Receive streaming const stream = client.listUsers({}); stream.on('data', (user) => console.log(user)); stream.on('end', () => console.log('Done'));

Practical Conversation Example

Architect

"How about switching communication between microservices from REST to gRPC?"

Developer

"Performance will definitely improve. But debugging is a bit inconvenient. It's hard to test with curl like you can with JSON."

Architect

"You can use grpcurl or Postman's gRPC feature. And let's keep REST for external APIs and use gRPC only for communication between internal services."

Cautions

No direct calls from browsers

gRPC calls cannot be made from a regular browser. Web clients need gRPC-Web or a REST gateway.

Managing proto files

When a .proto file changes, the code for all services must be regenerated. Version control of proto files and maintaining compatibility are important.

Debugging complexity

Because it is a binary protocol, it is difficult to read network packets directly. Dedicated tools (grpcurl, Postman) are required.

Related Terms

Learn More