Chapter 24: gRPC API Design Best Practices

Request/Response Pattern

// Every RPC gets its own Request and Response message
rpc GetUser(GetUserRequest) returns (GetUserResponse);
rpc ListUsers(ListUsersRequest) returns (ListUsersResponse);
rpc UpdateUser(UpdateUserRequest) returns (UpdateUserResponse);

Standard Method Names (Google AIP)

MethodRequestResponse
GetGetXRequest {name}X
ListListXsRequest {page_size, page_token}ListXsResponse {xs, next_page_token}
CreateCreateXRequest {parent, x}X
UpdateUpdateXRequest {x, update_mask}X
DeleteDeleteXRequest {name}Empty or X

FieldMask for Partial Updates

import "google/protobuf/field_mask.proto";
message UpdateUserRequest {
  User user = 1;
  google.protobuf.FieldMask update_mask = 2;
  // Client sets: update_mask = "name,email"
  // Server only updates those fields, ignores others
}