Golang / GoLang System Architecture and Testing Interview Questions
What are the best practices for designing Protocol Buffer schemas in Go microservices?
Protobuf schema design has long-term consequences β once published, breaking changes require coordinated version bumps across all consumers. Good schema design minimises future pain.
// Best practices: // 1. Use well-known types for common data import "google/protobuf/timestamp.proto"; import "google/protobuf/duration.proto"; import "google/protobuf/wrappers.proto"; // for nullable primitives message Order { string order_id = 1; // UUIDs as strings, not int64 google.protobuf.Timestamp created_at = 2; // not int64 unix google.protobuf.Duration processing_time = 3; google.protobuf.StringValue discount_code = 4; // nullable string repeated OrderItem items = 5; OrderStatus status = 6; } // 2. Use enums with an UNSPECIFIED zero value enum OrderStatus { ORDER_STATUS_UNSPECIFIED = 0; // default/unknown Γ’ΒΒ must be 0 ORDER_STATUS_PENDING = 1; ORDER_STATUS_PAID = 2; ORDER_STATUS_SHIPPED = 3; ORDER_STATUS_CANCELLED = 4; } // 3. OneOf for discriminated unions message PaymentMethod { oneof method { CreditCard credit_card = 1; BankTransfer bank_transfer = 2; CryptoCurrency crypto = 3; } } // 4. Avoid nested types Γ’ΒΒ prefer separate top-level messages // Bad: message User { message Address { ... } address = 5; } // Good: message Address { ... } message User { Address address = 5; } // 5. Name convention: snake_case fields, PascalCase messages // 6. Namespace your protos: package company.service.v1; // 7. One service per .proto file; one message per concern
More Related questions...