mix · August 8, 2023·

RPC(Remote Procedure Call) and gRPC(Google RPC): Concepts and Comparison

Overview of RPC

Remote Procedure Call (RPC) is a communication protocol that enables one program to execute a procedure or function on a remote server as if it were local. Each RPC involves two sides:

  • Client: Initiates the call and sends a request.
  • Server: Processes the request and sends back a response.

RPC is agnostic to the underlying message-passing mechanism, focusing only on the specification and interpretation of messages. However, certain aspects must be considered:

  • Transport Protocol: Determines the maximum size of RPC messages. For example, TCP is stream-oriented with no size limit, while other protocols may have constraints.
  • Reliability: RPC does not inherently handle reliability; applications may need to address issues like time-outs and reconnections, especially in cases of server crashes.
  • Transport Options: RPC implementations can use various transport protocols. For instance, the Sun RPC package supports both TCP and UDP and is experimenting with ISO TP4 and TP0.

Common Issues and Solutions in RPC

  1. Service Crashes During Function Calls:

    • Applications must handle server or network failures by implementing error-handling mechanisms.
  2. Message Loss:

    • Retransmission is possible but may introduce inefficiencies.
  3. Message Delays:

    • Delays have no guaranteed solutions, though faster protocols like gRPC may mitigate this issue.
  4. Safety of Retries:

    • Global variables and side-effects are inaccessible to the server, preventing the use of hidden arguments. This simplifies retries but necessitates careful design.

Using RPC for Software Unit Testing in QEMU

RPC can facilitate unit testing in QEMU by enabling interaction between host-based test code and target machine code simulated inside QEMU. A potential implementation:

  1. RPC/gRPC Server in QEMU: Provides access to the target machine’s memory, registers, and state.
  2. RPC/gRPC Client on Host: Allows reading and writing data and controlling execution of the simulated target.

Comparing gRPC and RPC

Feature RPC gRPC
Protocol General protocol for remote procedure calls Specific implementation using Protocol Buffers
Platform Support Broad platform support Designed for modern, cloud-native environments
Performance Depends on implementation Faster with binary serialization and HTTP/2
Features Basic procedure call functionality Includes streaming APIs, load balancing, flow control
Flexibility High flexibility, supports various protocols Standardized implementation, less flexible

gRPC Features and Architecture

  1. RPC Life Cycle:

    • Defines interactions between clients and servers, from request initiation to response handling.
  2. Communication Types:

    • Unary RPC: A single request from the client and a single response from the server.
    • Server Streaming RPC: A single client request receives a stream of server responses.
    • Client Streaming RPC: A stream of client requests is sent to the server, which returns a single response.
    • Bidirectional Streaming RPC: Both client and server exchange streams of messages independently within a single connection.
  3. Deadlines/Timeouts:

    • Clients can set time limits for RPC completion, resulting in a DEADLINE_EXCEEDED error if exceeded.
    • Servers can monitor remaining time for an RPC to adjust processing.
  4. RPC Termination:

    • Independent success/failure determination by client and server.
    • Scenarios such as server-side success but client-side failure due to delayed responses are possible.
  5. Cancelling an RPC:

    • Either side can cancel an RPC to immediately halt processing.
  6. Metadata:

    • Contains key-value pairs for additional information about an RPC call.
    • Metadata keys are case-insensitive and can hold binary or ASCII values. Keys must not start with grpc-, which is reserved.
    • User-defined metadata enables context-specific information exchange between client and server.
  7. Channels:

    • Channels provide connections to gRPC servers and maintain state (e.g., connected, idle).
    • Channels allow configuration, such as enabling/disabling compression, and their behavior varies by language.

By understanding these features, gRPC offers robust solutions for modern distributed applications, enhancing traditional RPC protocols with performance and usability improvements.