{
    "slug": "grpc",
    "term": "gRPC",
    "category": "architecture",
    "difficulty": "advanced",
    "short": "A high-performance RPC framework using Protocol Buffers and HTTP/2 — strongly typed contracts, efficient binary serialisation, and streaming support.",
    "long": "gRPC (Google Remote Procedure Call) uses Protocol Buffers (.proto files) to define strongly-typed service contracts that generate client and server stubs in multiple languages. Over HTTP/2 it provides: unary calls (request/response), server streaming, client streaming, and bidirectional streaming. Compared to REST+JSON: gRPC is ~5–10x faster in serialisation, enforces schema contracts at compile time, supports streaming natively, but is harder to debug (binary protocol), requires HTTP/2, and has less ecosystem support. In PHP: grpc/grpc PECL extension + google/protobuf Composer package. Used for internal microservice communication where performance and schema enforcement matter more than browser accessibility.",
    "aliases": [
        "gRPC",
        "Google RPC",
        "Protocol Buffers RPC"
    ],
    "tags": [
        "architecture",
        "api",
        "microservices",
        "protocol"
    ],
    "misconception": "gRPC is always faster than REST. gRPC uses HTTP/2 and binary Protocol Buffers which are faster for high-throughput service-to-service calls, but browser support requires a proxy, debugging is harder, and the schema contract adds overhead for simple CRUD APIs where REST is often more practical.",
    "why_it_matters": "gRPC uses HTTP/2 and Protocol Buffers for strongly-typed, high-performance RPC — ideal for internal service communication where REST's text overhead and lack of strict contracts are a liability.",
    "common_mistakes": [
        "Using gRPC for public-facing APIs — browsers cannot make native gRPC calls without a proxy layer.",
        "Not versioning proto files — changes to a proto schema without backward compatibility breaks all consumers.",
        "Not handling gRPC status codes correctly — treating every non-OK as a generic error loses retry and fallback logic.",
        "Ignoring streaming capabilities — unary RPC where server-streaming would reduce latency misses the protocol's strength."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "rest",
        "api_versioning",
        "microservices",
        "websockets"
    ],
    "prerequisites": [
        "microservices",
        "protobuf",
        "api_design"
    ],
    "refs": [
        "https://grpc.io/docs/languages/php/",
        "https://developers.google.com/protocol-buffers"
    ],
    "bad_code": "// Unversioned proto — breaking change with no migration path:\nsyntax = \"proto3\";\nmessage User {\n    int32 id = 1;\n    string email = 2;\n    // Field 3 removed — existing clients that expected it will fail silently\n    // Never remove fields; mark deprecated and add new fields with new numbers\n}",
    "good_code": "// gRPC in PHP — high-performance RPC using Protocol Buffers\n\n// 1. Define service in .proto:\n// service OrderService {\n//   rpc PlaceOrder (PlaceOrderRequest) returns (PlaceOrderResponse);\n// }\n\n// 2. Generate PHP stubs:\n$ protoc --php_out=. --grpc_out=. orders.proto\n\n// 3. Client usage:\n\\$channel = new Grpc\\Channel('orders-service:50051', ['credentials' => Grpc\\ChannelCredentials::createInsecure()]);\n\\$client  = new OrderServiceClient('orders-service:50051', [], \\$channel);\n\n\\$request = new PlaceOrderRequest();\n\\$request->setUserId(42);\n\\$request->setTotal(1999);\n\n[\\$response, \\$status] = \\$client->PlaceOrder(\\$request)->wait();\nif (\\$status->code !== Grpc\\STATUS_OK) throw new \\RuntimeException(\\$status->details);\n\necho \\$response->getOrderId();",
    "quick_fix": "Use gRPC for internal service-to-service communication where performance matters — define .proto schemas, generate PHP client stubs, and enable TLS between services",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/grpc",
        "html_url": "https://codeclaritylab.com/glossary/grpc",
        "json_url": "https://codeclaritylab.com/glossary/grpc.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[gRPC](https://codeclaritylab.com/glossary/grpc) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/grpc"
            }
        }
    }
}