fleeting distributedsystem/microservice/grpc systemdesign

‘What Is gRPC?’
—Kasun Indrasiri, “gRPC: Up & Running”


Catalog

gRPC - Communication Patterns
gRPC - Under The Hood
gRPC - Protobuf
gRPC - Reflection
gRPC - Interceptors
gRPC - Multiplexing
gRPC deep dive: from service definition to wire format | Kreya

Interface Definition Language (IDL)
Procedure Calls
Evolution of IPC


Background

Inter Process Communication (IPC)

Microservices architecture | Part 3 (Inter-process communication) | by Danial Eskandari | Medium

In distributed systems such as microservices based software which consist of tens and hundreds of individual and standalone services, there need to be a way collaboration between the services.

Inter-Process Communication (IPC) in a Distributed System refers to the mechanisms that allow processes running on different machines (or nodes) to communicate and coordinate with each other.

Unlike IPC in a single system (where processes communicate via shared memory or local message passing), distributed IPC operates over a network, adding complexity such as latency, failure handling, and synchronization.

Synchronous communication —> the client process sends a request message to the server process over the network and wait for a response message.

Asynchronous —> the client process sends a request message to the server process over the network but does not wait for a response. Instead, the client continues executing other tasks and processes the response when it arrives.
👉 SA01.4 - Event Driven Architecture
👉 Evolution of IPC

RESTful Services

When it comes to building synchronous request-response style communication for modern cloud native applications and microservices, the most common and conventional approach is to build them as RESTful services.

But most use cases RESTful services are quite bulky, inefficient, and error-prone for building inter-process communication.

Highly scalable, loosely coupled IPC Technology that is more efficient that RESTful services.

Procedure Calls (RPC)

👉 Procedure Calls

gRPC Intro

Interface Definition Language (IDL)
gRPC - Protobuf

gRPC is an Inter-Process communication technology that allows you to connect, invoke, operate and debug distributed heterogeneous applications as easily as making a local function call.

the “g” stands for something different in every gRPC release

Before develop gPRC application we want to define a Service interface. We use Interface Definition Language (IDL) to it.

Using service definition we can generate the server side code known as a server skeleton.Also we can generate client side code known as a client stub.

Server Skeleton & Client Stub

Go - Data Serialization
[!video]- Protobuf - How Google Changed Data Serialization FOREVER

Server Skeleton

server side code generated by an RPC Framework.

  • Receives requests from a client.
  • Deserialize the Incoming data
  • Calls the actual server function or method
  • Serialize the response and sends it back to the client

Client Stub

client side code generated by an RPC framework.

  • Handles communication with the server.
  • Marshals (serializes) the request data to be sent over the network.
  • Sends the request to the server skeleton.
  • Receives the response, unmarshals it, and returns it to the client application.

Analogy:
Client Stub: The messenger that wraps up your request, delivers it, and brings back the reply.
Server Skeleton: The receptionist that receives the request, passes it to the correct department (function), and prepares the reply.

👉 HTTP

gRPC uses gRPC - Protobuf as the IDL to define service interface.

%%{init: {'theme':'default'}}%%
sequenceDiagram
    participant Client
    participant ClientLibrary
    participant ServerLibrary
    participant Server

    Client->>ClientLibrary: Invoke gRPC service
    ClientLibrary->>ClientLibrary: Marshal request using Protocol Buffers
    ClientLibrary->>ServerLibrary: Send request over HTTP/2
    ServerLibrary->>ServerLibrary: Unmarshal request using Protocol Buffers
    ServerLibrary->>Server: Execute procedure
    Server->>ServerLibrary: Return response
    ServerLibrary->>ServerLibrary: Marshal response using Protocol Buffers
    ServerLibrary->>ClientLibrary: Send response over HTTP/2
    ClientLibrary->>ClientLibrary: Unmarshal response using Protocol Buffers
    ClientLibrary->>Client: Return result

Info

Marshaling is the process of packing parameters and a remote function into a message packet that is sent over the network, while unmarshaling unpacks the message packet into the respective method invocation.

Why gRPC ?

Advantages

  • It’s efficient for IPC
  • Simple & Well-defined service interfaces and schema
  • Strongly Typed
  • Polyglot
  • Duplex Steaming

Disadvantages

  • Not suitable for external facing services
  • Drastic service definition changes are a complicated development process
  • gRPC ecosystem is still relatively small compared to the conventional REST/HTTP protocol.

gRPC vs. Apache Thrift and GraphQL

gRPC vs. Apache Thrift

  • Overview: Both are RPC frameworks that use interface definition files to define services and data types, generating client and server code.
  • Transport:
    • gRPC is more opinionated, offering first-class support for HTTP/2 with efficient streaming capabilities.
    • Thrift is transport-agnostic and can run on implementations like TCP, HTTP, etc.
  • Streaming: gRPC natively supports bidirectional streaming as part of its service definition, while Thrift lacks this feature natively.
  • Adoption: gRPC has broader community adoption, better ecosystem support, and richer documentation compared to Thrift.
  • Performance: While unofficial comparisons suggest Thrift might perform slightly better, gRPC’s frequent benchmarking ensures solid performance.

gRPC vs. GraphQL

  • Overview: GraphQL is a query language for APIs that allows clients to specify the data they need, whereas gRPC defines a fixed contract for remote methods between client and server.
  • Use Cases:
    • GraphQL: Ideal for external-facing APIs where clients need flexibility to request specific data in custom formats.
    • gRPC: Better suited for internal services and APIs requiring high performance, fixed communication contracts, and streaming.
  • Example: In an online retail app, GraphQL lets external consumers query specific product attributes, while gRPC powers the backend services behind the APIs.