design-protobuf-grpc-service
Skill9 starsChanged 5 months agoApache-2.0View on GitHub
Designs and evolves gRPC/protobuf service contracts — message and service definitions, unary vs streaming RPC selection, wire-compatible schema evolution (reserved tags, safe vs breaking changes), canonical status codes, deadlines/cancellation, interceptors, and buf-driven codegen plus breaking-change detection.
In this repository147 other files it gives its agents.
- second-brain/AGENTS.mdAGENTS.md
- second-brain/CLAUDE.mdCLAUDE.md
- agent-tool-mcp-builderSkill
- api-design-reviewSkill
- async-concurrency-correctnessSkill
- audit-accessibility-wcagSkill
- audit-license-complianceSkill
- audit-technical-seoSkill
- auth-jwt-sessionSkill
- author-codemodSkill
- brainstorm-designSkill
- build-audit-loggingSkill
- build-cdc-streaming-pipelineSkill
- build-cli-toolSkill
- build-data-tableSkill
- build-etl-pipelineSkill
- build-form-validationSkill
- build-native-mobile-uiSkill
- build-office-docsSkill
- build-offline-first-syncSkill
- build-react-componentSkill
- build-realtime-channelSkill
- build-spreadsheetSkill
- build-vector-searchSkill
- caching-strategySkill
- cicd-pipeline-authorSkill
- cloud-cost-optimizeSkill
- code-commentsSkill
- code-reviewSkill
- compose-local-dev-stackSkill
- configure-bundler-buildSkill
- configure-dns-tlsSkill
- configure-reverse-proxy-lbSkill
- configure-security-headers-cspSkill
- contract-testingSkill
- datetime-timezone-correctnessSkill
- db-migration-safetySkill
- debug-ci-pipeline-failureSkill
- debug-flaky-testsSkill
- debug-frontend-browserSkill
- debug-root-causeSkill
- defend-llm-prompt-injectionSkill
- deliver-webhooksSkill
- dependency-upgradeSkill
- deploy-releaseSkill
- design-api-paginationSkill
- design-authorization-modelSkill
- design-backup-dr-recoverySkill
- design-event-sourcing-cqrsSkill
- design-multi-tenancySkill
- design-relational-schemaSkill
- design-search-index-infraSkill
- design-state-machineSkill
- design-token-systemSkill
- diff-table-paritySkill
- distributed-locks-leasesSkill
- dockerfile-optimizeSkill
- encrypt-sensitive-dataSkill
- error-messageSkill
- estimate-workSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 29
Removing a field: delete it *and* reserved both its number and name, so nobody reuses them.
- Very specific: line 39
Use optional (proto3) when you must distinguish "unset" from zero-value.
- Very specific: line 44
Prefer google.protobuf.Timestamp/Duration over raw int64.
- Distinctive: line 55
Stream vs websocket: if both ends are gRPC and you need typed messages + backpressure + deadlines, use a gRPC stream.
- Distinctive: line 95
TLS always; mTLS for service-to-service: Never run a non-loopback gRPC server on an insecure channel — h2c in the clear leaks every byte.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use
Reach for this skill when the contract is a .proto / gRPC wire format, not an HTTP/JSON shape:
- "Design the messages and RPCs for this new service" / "add a method to this
.proto" - "Is renaming/renumbering this field safe to deploy?" — wire-compat review
- "Should this be unary, server-streaming, or bidi?" / "stream vs websocket?"
- "Wire codegen for Go + TS + Python off one schema" / "set up
buf+ breaking-change CI" - "Set deadlines / map our errors to gRPC status codes / add an auth interceptor"
- "Expose this to a browser" → gRPC-Web / Connect
NOT this skill:
- REST resources, JSON envelopes, OpenAPI/SDL, HTTP versioning/pagination → rest-graphql-contract
- Reviewing an existing HTTP/RPC API diff for naming/compat as an audit pass → api-design-review
- Issuing/verifying JWTs, OAuth/OIDC flows, RBAC logic (the interceptor calls this) → auth-jwt-session
- Adding tracing/metrics/logs to the service internals → observability-instrument
- Correctness of the streaming/concurrency code itself (races, missing await) → async-concurrency-correctness
Steps
- Model messages — field numbers are the contract, names are not. The tag number is what goes on the wire; renaming a field is free, renumbering is catastrophic.
- Number
1–15cost 1 byte; reserve them for the hot, always-present fields.16+cost 2 bytes. - Removing a field: delete it and
reservedboth its number and name, so nobody reuses them. This is non-negotiable.message User { reserved 4, 7 to 9; // retired tags — never reuse reserved "email_verified"; // retired name — block re-add under old meaning string id = 1; string display_name = 2; optional string email = 3; // optional => field presence (knows set-vs-default) } - Use
optional(proto3) when you must distinguish "unset" from zero-value; bare scalars can't tell0/""/falsefrom absent. - Every enum starts at
0 = *_UNSPECIFIED. 0 is the default on the wire; if 0 means a real state you can't detect "not set," and you can't safely add values before it.enum Status { STATUS_UNSPECIFIED = 0; STATUS_ACTIVE = 1; STATUS_BANNED = 2; } - Prefer
google.protobuf.Timestamp/Durationover raw int64;map<k,v>over parallel lists; aMoney{currency_code, units, nanos}message over a float. Never put currency in adouble.
- Number
Pick the RPC shape from the data flow — default to unary. Streaming is for unbounded or incremental data, not for "it's faster."
Shape Signature Use when Don't use for Unary rpc Get(Req) returns (Resp)request/response, bounded payload — the default huge/unbounded results Server-streaming returns (stream Resp)feed/tail, large result set, server-push progress a single object that fits in memory Client-streaming (stream Req) returns (Resp)chunked upload, batch ingest, client-side aggregation small fixed-size input Bidi (stream Req) returns (stream Resp)live chat, long-lived sync, interactive session anything a sequence of unary calls covers - Stream vs websocket: if both ends are gRPC and you need typed messages + backpressure + deadlines, use a gRPC stream. Reach for a websocket only when a browser needs raw duplex and you're not on Connect/gRPC-Web.
- Page large reads with
page_size/page_token(AIP-158) before reaching for server-streaming — pagination is resumable and cacheable; a broken stream restarts from zero.
Run the wire-compat checklist before any schema change — clients and servers deploy at different times, in multiple languages, and old binaries must keep parsing new messages.
Change Wire-safe? Why Add a new field (new tag) ✅ old readers skip unknown fields Add a new RPC / new message ✅ additive Rename a field (same tag/type) ✅ wire / ⚠️ JSON wire keys on number; gRPC-JSON/Connect keys on name — breaks JSON clients Add an enum value ✅ but old clients see it as the unknown/default — handle that branch int32↔int64,sint↔int,optional↔repeated❌ different wire encoding → silent corruption Reuse / renumber a tag ❌ old data deserializes into the wrong field Remove a field without reserved❌ tag can be reused later → corruption Change a field's type/cardinality ❌ re-version the message or add a new field instead Rename/move a service or package ❌ path is /pkg.Service/Method— old stubs 404 withUNIMPLEMENTEDTo evolve incompatibly: add a new field/method, deprecate the old (
[deprecated = true]), migrate, thenreservedit — never mutate in place. Enforce this withbuf breaking(step 6).- Error & control plane — set a deadline on every call, return canonical codes.
- Deadlines are mandatory. A call without one can hang forever and pin a server thread. Set an absolute deadline client-side (
context.WithTimeout, ~the SLO); servers must checkctx.Err()/isCancelledand stop work when the client gives up. Propagate the deadline to downstream calls — don't reset it. Map failures to the canonical status codes, not a generic
UNKNOWN/INTERNAL:Code Use for Retry? INVALID_ARGUMENTmalformed request, fails regardless of state no FAILED_PRECONDITIONvalid request, wrong system state no (fix state first) NOT_FOUND/ALREADY_EXISTSmissing / duplicate resource no PERMISSION_DENIED/UNAUTHENTICATEDauthz fail / missing-bad creds no RESOURCE_EXHAUSTEDquota / rate limit yes, with backoff + honor Retry-After-style detailDEADLINE_EXCEEDEDcall ran past deadline yes if idempotent UNAVAILABLEtransient — server down/restarting yes, backoff (the canonical retryable code) ABORTEDconcurrency conflict (CAS/txn) yes, after re-read - Attach machine-readable detail with
google.rpc.Status+ typed details (ErrorInfowith a stablereason+domain,BadRequest.field_violations,QuotaFailure) — not a prose string clients must regex. - Retry only idempotent methods. Configure a service-config retry policy (
maxAttempts,UNAVAILABLE/DEADLINE_EXCEEDEDonly, exponential backoff). For non-idempotent creates, pass a client-generated idempotency key in metadata and dedupe server-side. Cancellation propagates automatically when the client closes the stream/context — release resources on it.
- Deadlines are mandatory. A call without one can hang forever and pin a server thread. Set an absolute deadline client-side (
- Cross-cutting concerns belong in interceptors + metadata, not in every method.
- Interceptors (chained, ordered) for auth, logging, tracing, panic-recovery, rate-limit. Auth interceptor reads the token from metadata and delegates verification (that logic lives in auth-jwt-session) — return
UNAUTHENTICATED(missing/invalid creds) vsPERMISSION_DENIED(valid identity, not allowed). - Metadata = gRPC's headers/trailers. Lowercase ASCII keys; a key carrying raw bytes must end in
-bin(e.g.trace-id-bin) so the runtime base64-handles it. Carry auth (authorization: Bearer …), request id, idempotency key, locale. Never put a deadline in metadata — it's a first-class call property. Reservedgrpc-*keys are runtime-owned; don't set them yourself. - TLS always; mTLS for service-to-service. Never run a non-loopback gRPC server on an insecure channel — h2c in the clear leaks every byte.
- Browser/edge: native gRPC needs HTTP/2 trailers a browser can't send, so expose Connect (speaks gRPC, gRPC-Web, and JSON over the same handler — easiest) or gRPC-Web behind an Envoy/proxy translation layer. Don't try to call raw gRPC from
fetch.
- Interceptors (chained, ordered) for auth, logging, tracing, panic-recovery, rate-limit. Auth interceptor reads the token from metadata and delegates verification (that logic lives in auth-jwt-session) — return
- Codegen + lint with
buf, not rawprotoc— and wire breaking-change detection into CI.protocplugin/path juggling is the classic footgun;bufmakes the schema the source of truth.# buf.yaml version: v2 lint: { use: [STANDARD] } breaking: { use: [WIRE_JSON] } # catch tag/type/name breakage# buf.gen.yaml — one schema, many languages version: v2 plugins: - { remote: buf.build/protocolbuffers/go, out: gen/go, opt: paths=source_relative } - { remote: buf.build/connectrpc/go, out: gen/go, opt: paths=source_relative } - { remote: buf.build/bufbuild/es, out: gen/ts }
Check generated stubs into VCS or regenerate in CI — pick one and enforce it; a stale committed stub that disagrees with thebuf lint # naming/style/UNSPECIFIED rules buf breaking --against '.git#branch=main' # FAIL CI on any wire/JSON break buf generate # regenerate all stubs from .proto.protois a silent contract drift. Back the contract with a contract test (step in Verify) so the running server and the.protocan't diverge.
Common Errors
- Reusing or renumbering a field tag. Old bytes deserialize into the wrong field — silent data corruption, no error. Always
reservedremoved tags and names;buf breakingcatches it if you let it. - Enum without
0 = *_UNSPECIFIED. 0 is the wire default, so you can't distinguish "unset" from your first real value, and you can't prepend values later. Always reserve 0 for UNSPECIFIED. - No deadline on the call. One slow/hung downstream pins server resources indefinitely and cascades into outage. Set an absolute deadline on every client call; propagate, don't reset, downstream.
- Returning
INTERNAL/UNKNOWNfor everything. Clients can't tell retryable from fatal and either hammer a down service or give up on a transient blip. Map to the specific canonical code; reserveINTERNALfor genuine server bugs. - Retrying non-idempotent RPCs. A retried
Create/Chargeafter a timeout double-executes. Restrict the retry policy to idempotent methods; for the rest use a server-deduped idempotency key. - Changing a scalar type to a "compatible-looking" one (
int32→int64,optional→repeated). Different wire encoding → garbled values on old readers. Add a new field instead and migrate. - Renaming a field assumed free, but a Connect/gRPC-JSON client keys on the name. Wire-safe, JSON-breaking. If any client speaks JSON, treat a rename as breaking.
- Calling raw gRPC from a browser. Native gRPC needs HTTP/2 trailers the browser can't produce. Use Connect or gRPC-Web through a proxy.
protocplugin/import-path hell producing stale or wrong stubs. Usebufwith a remote plugin set so paths and versions are pinned and reproducible.- Insecure (h2c, no TLS) channel in prod. Everything including bearer tokens is in cleartext. TLS always; mTLS between services.
- Breaking-change check missing from CI. A bad merge ships an incompatible schema and breaks every deployed client.
buf breaking --against mainmust gate merges.
Verify
- Lint clean:
buf lintpasses — every enum has*_UNSPECIFIED = 0, fields snake_case, services/methods follow the standard naming rules. - Breaking-change gate:
buf breaking --against '.git#branch=main'is green; deliberately renumber a tag locally and confirm it goes red (proves the gate actually fires). - Codegen reproducible:
buf generatefrom a clean tree produces stubs byte-identical to what's committed (no uncommitted diff) for every target language. - Wire round-trip across versions: serialize a message with the new schema, parse it with a binary built on the old schema (and vice-versa) — no error, no field loss for additive changes. This is the real proof of compatibility, not eyeballing the diff.
- Deadline honored: a call given a 100ms deadline against an artificially slow method returns
DEADLINE_EXCEEDEDnear 100ms (not hanging), and the server logs show it cancelled work rather than running to completion. - Status mapping: each error path returns its specific canonical code (asserted in tests), and retry policy retries
UNAVAILABLE/DEADLINE_EXCEEDEDonly — a deliberateINVALID_ARGUMENTis not retried. - Streaming flow: a server-stream consumer that cancels mid-stream causes the server's context to cancel and stop producing (no leaked goroutine/thread); a client-stream upload that drops mid-send leaves no half-written state.
- Auth interceptor: missing token →
UNAUTHENTICATED; valid token without permission →PERMISSION_DENIED; both asserted, and the path runs over TLS (insecure channel rejected). - Contract test: a cross-language client built from the generated stub calls the running server end-to-end and gets the expected typed response — proves
.proto, server, and stubs agree.
Done = buf lint and buf breaking --against main pass in CI (and the gate provably fails on a real break), buf generate leaves no diff, the old↔new wire round-trip and the cross-language contract test both pass, every call sets a deadline, and each error path returns its specific canonical status code over TLS.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

