Logotipo de Backend Grpc Patterns

Backend Grpc Patterns

Comunidad
j4flmao
backend-grpc-patterns

Use this skill when the user says 'gRPC', 'protobuf', 'protocol buffers', 'streaming RPC', 'unary call', 'server streaming', 'client streaming', 'bidirectional streaming', 'gRPC interceptor', 'gRPC error handling', 'protobuf schema', 'service definition', 'RPC design', or when designing gRPC APIs. This skill enforces consistent protobuf schema conventions, streaming patterns, interceptor chains, and structured error handling for gRPC services. Applies to any backend stack using gRPC. Do NOT use for: REST API design, GraphQL schema, message queue design, or frontend data fetching.

Descripción general

Editorj4flmao
Repositorioagent-skills
Nombre de la habilidadbackend-grpc-patterns
Estrellas
41
Forks
6
Archivos incluidos
10
LicenciaMIT
Enlaces
  • Instrucciones en Markdown

    Un archivo SKILL.md que el modelo carga cuando lo necesita, de modo que solo consume tokens cuando una petición es realmente relevante.

  • Funciona con cualquier LLM

    Las habilidades de IA están en Markdown, no en código específico de un proveedor, por lo que funcionan con GPT, Claude, Gemini, Grok o un modelo local.

  • 10 archivos incluidos

    Scripts, plantillas y referencias que el modelo puede leer mientras trabaja. Los archivos son de solo lectura y nunca se ejecutan.

  • Código abierto

    Publicada por j4flmao en GitHub. Revisa el contenido original antes de instalarla.

Instalación

Instala la habilidad de IA Backend Grpc Patterns en TypingMind para usarla con cualquier LLM, o añádela a otro agente que lea SKILL.md.

1

Instalar en TypingMind

TypingMind instala una habilidad directamente desde su carpeta de GitHub: lee SKILL.md, empaqueta los archivos de recursos y guarda el resultado localmente.

  1. Abre la aplicación y ve a Plugins → Habilidades.
  2. Elige «Instalar desde GitHub».
  3. Pega la URL de la carpeta de la habilidad que se muestra a continuación y confirma.
    https://github.com/j4flmao/agent-skills/tree/main/skills/backend/universal/grpc-patterns
  4. Activa la habilidad en cada chat en el que quieras tenerla disponible.
Plugins → Habilidades → Añadir habilidad → Desde URL de GitHub; después, pega la URL de la carpeta y pulsa Continuar.
2

Instalar en otro agente

Cualquier agente que lea el formato Agent Skills puede usar esta habilidad: copia la carpeta en el directorio de habilidades de ese agente.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/j4flmao/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/skills/backend/universal/grpc-patterns .claude/skills/backend-grpc-patterns
Reinicia Claude Code después de copiarla para que detecte la nueva habilidad.

Úsala en TypingMind

Activa Backend Grpc Patterns en cualquier chat de TypingMind y el modelo se encargará del resto. Su nombre y descripción se incluyen en el prompt del sistema y, en cuanto una petición es relevante, el modelo carga las instrucciones completas por sí solo. No tienes que invocarla manualmente y no consume tokens hasta que se utiliza.

El modelo carga Backend Grpc Patterns automáticamente en cuanto una petición es relevante para esa habilidad.

Funciona con cualquier modelo de IA

Las habilidades de IA son instrucciones en Markdown, no código específico de un proveedor, por lo que Backend Grpc Patterns no está vinculada al modelo para el que se creó. Instálala una sola vez en TypingMind y úsala con GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama o un modelo local que ejecutes tú, todo con tus propias claves API.

  • Se carga solo cuando se necesita

    El prompt del sistema solo incluye el nombre y la descripción. Las instrucciones se cargan con la primera petición relevante, por lo que una habilidad que no se usa no supone ningún coste.

  • Cambia de modelo durante el chat

    Como la habilidad contiene instrucciones y no código, cambiar de modelo no impide que funcione: el nuevo modelo lee el mismo archivo SKILL.md.

Instrucciones de la habilidad

Este es el contenido de SKILL.md que carga el modelo. Léelo antes de instalar la habilidad: contiene instrucciones que tu modelo seguirá.

Backend gRPC Patterns

Purpose

Design consistent, production-grade gRPC services. Every RPC must follow the same conventions for protobuf schemas, streaming patterns, interceptors, error handling, and service versioning.

Agent Protocol

Trigger

Exact user phrases: "gRPC", "protobuf", "protocol buffers", "streaming RPC", "unary call", "server streaming", "client streaming", "bidirectional streaming", "gRPC interceptor", "gRPC error handling", "protobuf schema", "service definition", "design an RPC", "gRPC contract".

Input Context

Before activating, verify:

  • The service or feature being designed is known.
  • The proto package name and version are known.
  • The streaming model (unary/server-stream/client-stream/bidi) is chosen. If not, ask: "What streaming model do you need?"
  • The language/runtime for code generation is known.

Output Artifact

No file output unless the user requests it. Produces protobuf schema and RPC specifications as text.

Response Format

For each RPC:

rpc {MethodName}({RequestType}) returns ({ResponseType})
Streaming: {none/server/client/bidirectional}
Auth: {required/optional/none}
Deadline: {timeout suggestion}
Errors: {list of gRPC status codes}

For a full service definition:

service {ServiceName} {
  {list of RPCs}
}

No preamble. No postamble. No explanations. No filler/hedging/transitions. Compress output — why use many token when few do trick.

Completion Criteria

  • All protobuf packages follow the naming conventions below.
  • Every RPC has: method signature, streaming type, auth requirement, deadline, error codes.
  • Protobuf messages are versioned in the package name.
  • Error responses use standard gRPC status codes with rich detail.
  • All streaming RPCs include proper backpressure handling.
  • Interceptor chain is documented.

Max Response Length

Per RPC: 6 lines. Per service: unlimited.

Decision Tree

Which RPC Pattern?

What is the communication pattern?
  ├── Request → Response (standard CRUD)
  │   └── Unary RPC — simplest, HTTP/2 under the hood
  ├── Request → stream of events (subscribe, watch, notifications)
  │   └── Server-streaming RPC — send multiple responses for one request
  ├── Stream of requests → single response (batch upload, long-form input)
  │   └── Client-streaming RPC — aggregate client data into one result
  ├── Stream → stream (chat, real-time sync, gaming)
  │   └── Bidirectional streaming — full-duplex communication
  └── Fire-and-forget (event, notification, pub/sub)
      └── Consider message queue instead — gRPC streaming is for persistent connections

When to Use gRPC vs REST vs Message Queue?

gRPC:               Internal service-to-service, low latency, high throughput
                    Streaming, real-time, bidirectional
                    Polyglot environments (codegen for many languages)
REST:               External/public APIs (browsers, mobile, third-party)
                    Simple CRUD, cacheable resources
                    When HTTP semantics matter (caching, content negotiation)
Message Queue:      Async communication, event-driven, fire-and-forget
                    When services must be decoupled in time
                    When you need guaranteed delivery, retries, DLQ

Workflow

Step 1: Choose Streaming Model

Unary:              request → response                    (standard CRUD)
Server-streaming:   request → stream<response>            (list, watch, subscribe)
Client-streaming:   stream<request> → response            (batch upload, long-form input)
Bidirectional:      stream<request> → stream<response>    (chat, real-time sync, gaming)

Step 2: Define Protobuf Package and Version

syntax = "proto3";

package acme.users.v1;

option go_package = "github.com/acme/gen/go/users/v1;usersv1";
option java_package = "com.acme.users.v1";

Step 3: Define Messages

Use proto3. Follow these rules:

  • message names are PascalCase: CreateUserRequest, UserResponse.
  • Field names are snake_case: user_id, created_at.
  • Field numbers are dense starting at 1. Never reuse a field number.
  • Use google.protobuf.Timestamp for time, not int64 or string.
  • Use google.protobuf.FieldMask for partial updates.
  • Use google.protobuf.Empty for void responses.
  • oneof for mutually exclusive fields.
  • map for key-value pairs (never for large datasets).
protobuf
message CreateUserRequest {
  string name = 1;
  string email = 2;
  google.protobuf.Timestamp birth_date = 3;
  oneof contact_preference {
    string phone = 4;
    string telegram = 5;
  }
}

Step 4: Standard RPC Patterns

// CRUD service
service UserService {
  rpc CreateUser(CreateUserRequest) returns (User);
  rpc GetUser(GetUserRequest) returns (User);
  rpc ListUsers(ListUsersRequest) returns (ListUsersResponse);
  rpc UpdateUser(UpdateUserRequest) returns (User);
  rpc DeleteUser(DeleteUserRequest) returns (google.protobuf.Empty);
}

// Streaming service
service EventService {
  rpc Subscribe(SubscribeRequest) returns (stream Event);
  rpc Publish(stream PublishRequest) returns (PublishSummary);
  rpc Chat(stream ChatMessage) returns (stream ChatMessage);
}

Step 5: Pagination for Streaming Lists

Server-streaming is preferred for large lists to avoid OOM:

protobuf
message ListUsersRequest {
  string page_token = 1;    // opaque cursor, not a number
  int32 page_size = 2;      // max items to return
}

message ListUsersResponse {
  repeated User users = 1;
  string next_page_token = 2;  // empty means no more
}

Step 6: Error Handling

Always return standard gRPC status codes with rich detail via google.rpc.Status:

gRPC CodeNumberWhen
OK0Success
CANCELLED1Call cancelled by client
INVALID_ARGUMENT3Bad request input
DEADLINE_EXCEEDED4Timeout
NOT_FOUND5Resource not found
ALREADY_EXISTS6Duplicate resource
PERMISSION_DENIED7Auth failure
UNAUTHENTICATED16Missing credentials
RESOURCE_EXHAUSTED8Rate limited / quota
FAILED_PRECONDITION9System state wrong
ABORTED10Conflict (concurrent mutation)
INTERNAL13Unexpected server error
UNAVAILABLE14Service temporarily down

Rich error detail:

protobuf
import "google/rpc/error_details.proto";

message ErrorInfo {
  string reason = 1;
  string domain = 2;
  map<string, string> metadata = 3;
}

Step 7: Interceptor Chain

Order matters — apply in this sequence:

Client side:
  1. Deadline/timeout interceptor (outermost)
  2. Auth token injection
  3. Tracing (OpenTelemetry span injection)
  4. Logging (request/response summary)
  5. Circuit breaker / retry (innermost)

Server side:
  1. Tracing (span creation) (outermost)
  2. Logging (request metadata)
  3. Auth validation (JWT, mTLS)
  4. Rate limiting
  5. Deadline enforcement (innermost)
typescript
// gRPC server interceptor — auth + logging (Node.js)
import { ServerInterceptor, status } from '@grpc/grpc-js';

const authInterceptor: ServerInterceptor = (call, definition) => {
  const deadline = call.getDeadline();
  if (deadline && Date.now() > deadline.toMillis()) {
    call.emit('error', { code: status.DEADLINE_EXCEEDED, details: 'Deadline exceeded' });
    return;
  }
  const metadata = call.metadata;
  const token = metadata.get('authorization')[0] as string;
  if (!token) {
    call.emit('error', { code: status.UNAUTHENTICATED, details: 'Missing token' });
    return;
  }
  call.user = verifyToken(token);
  return new (definition as any)(call, definition);
};
go
// gRPC server interceptor — logging (Go)
import (
  "google.golang.org/grpc"
  "google.golang.org/grpc/status"
)

func loggingUnaryInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
  start := time.Now()
  resp, err := handler(ctx, req)
  duration := time.Since(start)
  level := slog.LevelInfo
  if err != nil {
    level = slog.LevelError
  }
  slog.LogAttrs(ctx, level, "gRPC call",
    slog.String("method", info.FullMethod),
    slog.Duration("duration", duration),
    slog.String("status", status.Code(err).String()),
  )
  return resp, err
}

Step 8: Streaming Implementation Patterns

Server-streaming with backpressure (Go):

go
func (s *EventService) Subscribe(req *pb.SubscribeRequest, stream pb.EventService_SubscribeServer) error {
  // Backpressure: server controls send rate
  ch := s.eventBus.Subscribe(req.Topic)
  defer s.eventBus.Unsubscribe(req.Topic, ch)

  for {
    select {
    case event := <-ch:
      if err := stream.Send(event); err != nil {
        // Client disconnected or network error
        return err
      }
    case <-stream.Context().Done():
      // Client cancelled
      return stream.Context().Err()
    }
  }
}

Bidirectional streaming with flow control (Node.js):

typescript
async function chat(call: ServerDuplexStream<ChatMessage, ChatMessage>) {
  call.on('data', (msg: ChatMessage) => {
    // Process incoming message
    // Use backpressure: call.write returns boolean (true = buffer ok, false = backpressure)
    const canWrite = call.write({
      id: msg.id,
      text: `Echo: ${msg.text}`,
      timestamp: now(),
    });
    if (!canWrite) {
      call.pause();  // Wait for drain event
      call.once('drain', () => call.resume());
    }
  });
  call.on('end', () => call.end());
}

Step 9: Client-Side Patterns

typescript
// gRPC client with deadline and retry
import { credentials, ServiceError } from '@grpc/grpc-js';

async function callWithRetry<T>(fn: () => Promise<T>, maxRetries = 3): Promise<T> {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      return await fn();
    } catch (err) {
      const grpcErr = err as ServiceError;
      if (grpcErr.code === status.UNAVAILABLE && attempt < maxRetries - 1) {
        await delay(Math.pow(2, attempt) * 100);  // exponential backoff
        continue;
      }
      throw err;
    }
  }
  throw new Error('Unreachable');
}

const client = new UserServiceClient('localhost:50051', credentials.createInsecure());
const response = await callWithRetry(() => client.getUser({ userId: '123' }));

Step 10: Service Versioning

Package version in proto:   acme.users.v1 → acme.users.v2
Never break existing v1 RPCs — add new RPCs in v2 or add fields to v1 messages
Field deprecation:
  string old_field = 3 [deprecated = true];
  // Add replacement field
  string new_field = 4;
Client migration: run both versions simultaneously, migrate clients one by one

Production Considerations

ConcernPractice
TimeoutsEvery RPC must have a deadline. Server enforces; client sets
Connection managementKeep-alive pings (server: 1h idle, client: 30s). gRPC connection pooling
TLSmTLS for inter-service. Use Kubernetes cert-manager or SPIFFE
Load balancingClient-side load balancing (lookaside) or proxy (Envoy, Linkerd). Avoid random LB
Max message sizeDefault 4MB. Increase if needed, but prefer streaming for large payloads
Flow controlHTTP/2 flow control is automatic. Monitor GOAWAY frames for connection issues
Graceful shutdownDrain connections: stop accepting, wait for in-flight, then shutdown

Performance

FactorImpactMitigation
Protobuf serializationSmaller payloads and cheaper encode/decode than JSON (gain depends on message shape)Already using protobuf — good
Connection reuseHTTP/2 multiplexing — many RPCs on one connectionEnable keep-alive
Message sizeLarge messages block the connectionUse streaming for >1MB
TLS handshakeAdds 1-3 RTT per connectionConnection pooling, HTTP/2 reduces connections
ReflectionDisable in production (security + startup perf)Use proto descriptors, not reflection

Security

RiskMitigation
No auth by defaultAlways require auth token in metadata. Use mTLS for inter-service
Reflection enabled in prodDisable grpc.reflection in production
Large message DoSSet MaxReceiveMessageSize (default 4MB, max 100MB)
Slow loris attackSet MaxConcurrentStreams and connection timeouts
Unauthenticated streamingAuth happens once at stream open — verify at reconnect
Metadata leaking secretsNever log metadata (may contain tokens)

Anti-Patterns

Anti-PatternWhy It's BadFix
Reusing field numbersRemoved field can be confused with new oneAlways reserved removed fields
No deadlinesRPCs hang foreverEvery RPC must set a deadline
Using int64 for timestampsNo timezone, no formattingUse google.protobuf.Timestamp
Catching all errors as INTERNALClient cannot react appropriatelyReturn specific status codes
Streaming for single-item responsesOver-engineered, more complexityUse unary
No backpressure in streamingServer OOMs when client is slowRespect stream.Send() return value
Blocking the event loopgRPC uses async I/O — blocking starves the connectionOffload CPU work to thread pool

Rules

  • Never reuse field numbers in protobuf — even after deletion, reserve them.
  • All RPCs must have a deadline/timeout. Server enforces it; client sets it.
  • Always return google.rpc.Status for errors, never bare strings.
  • Use RESOURCE_EXHAUSTED for rate limiting, not PERMISSION_DENIED.
  • Always paginate list RPCs with cursor tokens, never offset/limit.
  • Server-streaming calls must support a cancel mechanism at the application level.
  • Deprecate fields with [deprecated = true], do not remove them until the next major package version.
  • Client-streaming and bidi RPCs must handle client disconnect gracefully.
  • Never use reflection in production.
  • Always enable keep-alive pings on both client and server.
  • Every interceptor must call the next handler exactly once.
  • Use connection pooling for high-throughput scenarios.

References

  • references/grpc-error-handling.md — gRPC Error Handling
  • references/grpc-interceptors.md — gRPC Interceptors
  • references/grpc-performance.md — gRPC Performance
  • references/grpc-security.md — gRPC Security
  • references/grpc-streaming.md — gRPC Streaming Patterns
  • references/grpc-testing.md — gRPC Testing
  • references/grpc-vs-rest.md — gRPC vs REST
  • references/protobuf-basics.md — Protocol Buffer Basics

Handoff

No artifact produced unless requested. Next skill: backend-message-queue — if the service needs async communication or event-driven patterns. Next skill: backend-caching — if the service needs response caching or data store optimization. Carry forward: protobuf schemas, service definitions, auth requirements, deadline configurations.

  • Review code for security, performance, and correctness before merging.

Archivos incluidos

El modelo los lee según los necesita mientras la habilidad está cargada. Se ofrecen como archivos de lectura y nunca se ejecutan.

Preguntas frecuentes

¿Qué hace la habilidad de IA Backend Grpc Patterns?

Use this skill when the user says 'gRPC', 'protobuf', 'protocol buffers', 'streaming RPC', 'unary call', 'server streaming', 'client streaming', 'bidirectional streaming', 'gRPC interceptor', 'gRPC error handling', 'protobuf schema', 'service definition', 'RPC design', or when designing gRPC APIs. This skill enforces consistent protobuf schema conventions, streaming patterns, interceptor chains, and structured error handling for gRPC services. Applies to any backend stack using gRPC. Do NOT use for: REST API design, GraphQL schema, message queue design, or frontend data fetching.

¿Por qué usar Backend Grpc Patterns en TypingMind?

Porque la instalas una vez y la usas con cualquier modelo. Backend Grpc Patterns está escrita en Markdown, no en código específico de un proveedor, por lo que la misma habilidad funciona con GPT-5, Claude, Gemini, Grok o un modelo local, y puedes cambiar de modelo en mitad del chat sin que deje de funcionar. TypingMind utiliza tus propias claves API, de modo que pagas directamente a los proveedores en lugar de pagar una suscripción por puesto, y tus habilidades y chats permanecen en tu propio almacenamiento.

¿Cómo instalo Backend Grpc Patterns en TypingMind?

Abre Plugins → Habilidades → Instalar desde GitHub en TypingMind y pega https://github.com/j4flmao/agent-skills/tree/main/skills/backend/universal/grpc-patterns. TypingMind lee su archivo SKILL.md, empaqueta sus archivos y la instala como una habilidad que puedes activar en cada chat.

¿Qué modelos de IA pueden usar Backend Grpc Patterns?

Cualquier modelo que conectes en TypingMind. Las habilidades de IA son instrucciones en Markdown, no código específico de un proveedor, por lo que GPT, Claude, Gemini, Grok y los modelos locales pueden cargar esta habilidad cuando una solicitud se corresponda con ella.

¿Con cuántos modelos de IA puedo usar Backend Grpc Patterns?

Con tantos como quieras. Siempre que un modelo admita habilidades, puedes usar Backend Grpc Patterns con él: GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama y muchos más, todos en TypingMind con tus propias claves API.

¿Es gratuita la habilidad de IA Backend Grpc Patterns?

Sí. j4flmao la ha publicado en GitHub bajo la licencia MIT. Solo pagas a tu proveedor de IA por los tokens que utilizas.

¿Qué son las habilidades de IA?

Una habilidad de IA es un conjunto de instrucciones reutilizables que enseña a un modelo de IA a realizar una tarea concreta. Sigue el formato abierto Agent Skills: un archivo SKILL.md con un nombre y una descripción, además de los scripts, plantillas o archivos de referencia que pueda necesitar el modelo. El modelo solo lee las instrucciones cuando tu solicitud se corresponde con la habilidad, por lo que una habilidad instalada no supone ningún coste hasta que se utiliza.

¿En qué se diferencian las habilidades de IA de los plugins o los servidores MCP?

Un plugin o un servidor MCP proporciona al modelo nuevas herramientas que puede invocar: código que se ejecuta en algún lugar y devuelve un resultado. En cambio, una habilidad de IA aporta conocimientos y procedimientos al modelo: cómo abordar una tarea, qué pasos seguir y qué se considera un buen resultado. Las habilidades son instrucciones en Markdown, por lo que no necesitan servidor, clave API ni entorno de ejecución, y funcionan con cualquier modelo.

Ver todos

Crea ya tu propio espacio de trabajo IA

Suscríbete a nuestro boletín para recibir avisos sobre nuevas funciones y próximos sorteos 👇