← build log
reliabilitypattern 04Reliability & contracts

Proto-first contracts: the schema as the review gate

NestJS, Go, Elixir, Kotlin, and Python all read the same event. One of them has to define it.

The problem

Every cross-service message in Vitrin — Post, FeedRequest, RecommendationCandidate — gets consumed by services written in at least two different languages, sometimes four. Without a single source of truth, each language ends up with its own hand-maintained type describing the same wire format, and there’s nothing structural stopping them from drifting apart. A field rename in the TypeScript type doesn’t fail a build in the Go service still expecting the old name — it fails silently at runtime, in whichever language deserializes worst.

Approaches considered

Hand-write types per language, keep them in sync by convention. No tooling, no build step, and drift is a matter of “when,” not “if” — nothing catches it until a consumer breaks.

JSON Schema shared across services. Language-agnostic and versionable, but code generation support is uneven across TypeScript, Go, and Python, and it doesn’t map cleanly onto gRPC service definitions, which Vitrin needs anyway for synchronous calls.

Proto-first contracts with generated clients. Define every message and service once in .proto files, compile to native clients per language with buf.

Trade-offs

Proto-first means a schema change is now a two-step process — edit the .proto, regenerate — instead of a one-line diff in whichever service happens to need the change. It’s ceremony that a single-language monolith would never pay.

The decision

libs/contracts holds every .proto file. buf compiles them into TypeScript, Go, and Python clients, and CI runs buf breaking against the previous version on every pull request.

Why

The whole value is in catching the failure mode a shared library alone can’t: a breaking change reaching a consumer written in a language nobody touched in the same PR. buf breaking fails the build the moment a field is removed or its type changes, and lists every affected consumer by name — before the change ever ships, not after a service starts throwing deserialization errors in production. Ceremony on a Tuesday is a lot cheaper than an incident discovered by a consumer that isn’t even in the diff.

Implementation

// libs/contracts/post.proto — the single definition of a post,
// compiled into every language that touches this message.
syntax = "proto3";
package vitrin.post.v1;

message Post {
  string id = 1;
  string author_id = 2;
  string body = 3;
  int64 created_at = 4;
}

service PostService {
  rpc CreatePost(CreatePostRequest) returns (Post);
  rpc GetPost(GetPostRequest) returns (Post);
}
# buf.yaml — breaking-change detection runs against the last
# published version on every PR that touches libs/contracts.
version: v2
breaking:
  use:
    - FILE
lint:
  use:
    - STANDARD
# CI step — fails the PR before a breaking change ever reaches
# a Go or Python consumer that wasn't touched in the diff.
buf breaking libs/contracts --against '.git#branch=main'
buf generate libs/contracts