Skip to content

08 Services

DodaTech 3 min read

title: "Services in Protocol Buffers" description: "Define gRPC service endpoints in .proto files using the service keyword. Learn RPC method definitions, request/response message types, client streaming, server streaming, and bidirectional streaming." weight: 8 date: 2026-06-28 lastmod: 2026-06-28 tags: [api-development, grpc] }

Services in Protocol Buffers define the RPC endpoints that gRPC servers implement and clients call. A service contains one or more RPC methods, each with a request message type and a response message type.

What You'll Learn

  • Service definition with the service keyword
  • RPC method syntax and message types
  • Unary, server-streaming, client-streaming, and bidirectional RPC signatures
  • Service naming conventions and package organization
  • Multiple services in one file

Why It Matters

The service definition is the API contract. It tells client and server developers exactly which methods exist and what data they exchange. Well-organized services make the API discoverable and maintainable.

Real-World Use

Google's Cloud Pub/Sub API defines services for Publisher and Subscriber. etcd uses a single KV service with methods for Put, Range, Delete, and Watch. gRPC health checking defines one service with one method.

flowchart TD
    Service[Service Definition] --> Method1[RPC Method 1]
    Service --> Method2[RPC Method 2]
    Method1 --> Request[Request Message]
    Method1 --> Response[Response Message]
    Method1 --> StreamType[Stream Type]
    StreamType --> Unary[Unary: request + response]
    StreamType --> ServerStream[Server Streaming: request, stream response]
    StreamType --> ClientStream[Client Streaming: stream request, response]
    StreamType --> Bidi[Bidirectional: stream request, stream response]

Teacher Mindset

Group related methods into one service. Each service represents a bounded capability. Use meaningful names that reflect the domain. Keep the number of methods per service manageable (5-15).

Code Examples

// Example 1: Service with all RPC types
service OrderService {
  rpc GetOrder (GetOrderRequest) returns (Order);
  rpc ListOrders (ListOrdersRequest) returns (stream Order);
  rpc CreateOrder (stream CreateOrderRequest) returns (Order);
  rpc ProcessOrder (stream ProcessOrderRequest) returns (stream OrderStatus);
}

message GetOrderRequest {
  string order_id = 1;
}

message ListOrdersRequest {
  string customer_id = 1;
  int32 page_size = 2;
}
// Example 2: Multiple services in one package
package ecommerce;

service ProductService {
  rpc GetProduct (GetProductRequest) returns (Product);
  rpc ListProducts (ListProductsRequest) returns (stream Product);
  rpc CreateProduct (CreateProductRequest) returns (Product);
}

service InventoryService {
  rpc CheckStock (StockRequest) returns (StockResponse);
  rpc UpdateStock (stream StockUpdate) returns (StockSummary);
}
# Example 3: Generating server and client code
# Generate server code
protoc --go_out=. --go_opt=paths=source_relative \
  --go-grpc_out=. --go-grpc_opt=paths=source_relative \
  proto/order_service.proto

# Generated files:
# order_service.pb.go  (message types)
# order_service_grpc.pb.go  (client and server stubs)

Common Mistakes

  • Defining all methods in one giant service instead of splitting into focused services
  • Using inconsistent RPC type patterns within a service
  • Not including request/response messages for each method
  • Forgetting to regenerate stubs after changing the service definition
  • Ignoring proto package for service namespace management

Practice

  1. Define a UserService with GetUser, ListUsers, UpdateUser methods.
  2. Add a server-streaming method that returns a stream of active users.
  3. Add a client-streaming method that accepts multiple create requests.
  4. Add a bidirectional streaming method for real-time chat.
  5. Challenge: Design a service hierarchy for an e-commerce platform with 3-4 services.

FAQ

Can a service extend another service?

No. Proto3 does not support service inheritance. Use composition by having a service reference another service's messages.

How many RPC methods should a service have?

5-15 methods per service is typical. Split into multiple services if a service has more than 20 methods.

Can I have an RPC method with no request message?

Use google.protobuf.Empty for methods that take no input or return no output.

How do I version services?

Use packages like ecommerce.v1, ecommerce.v2. For breaking changes, create a new service version and implement both.

Can services from different packages be served on the same port?

Yes. A single gRPC server can register multiple services from different packages.

Mini Project

Define three services for your order management system: OrderService (GetOrder, ListOrders, CreateOrder), PaymentService (ProcessPayment, RefundPayment), and NotificationService (SubscribeNotifications as bidirectional stream).

What's Next

Next, you will learn about the four gRPC RPC types: unary, server-streaming, client-streaming, and bidirectional streaming.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro