Menu
Dev.to #systemdesign·November 28, 2026

Understanding RPC and Idempotency in Distributed Systems

This article demystifies Remote Procedure Calls (RPCs), particularly in the context of gRPC, Protocol Buffers, and HTTP/2. It highlights the crucial distinction between a local method call and a network operation, emphasizing the challenges of partial failures and the need for idempotency in distributed environments. The content provides a step-by-step breakdown of an RPC call, from client stub to server execution, and discusses how to handle network errors to prevent unintended side effects like duplicate OTPs.

Read original on Dev.to #systemdesign

The Illusion of Local Calls in Distributed Systems

Remote Procedure Calls (RPCs) abstract away network complexities, making a remote operation appear like a local method call. While this simplifies development by providing a strongly typed contract and code generation, it masks the fundamental differences and inherent unreliability of network communication. This abstraction can lead to insidious issues, especially when dealing with timeouts and retries, as a failed network response does not necessarily mean the remote operation itself failed.

⚠️

The "One Timeout, Two OTPs" Problem

A common trap in distributed systems is encountered when a client-side timeout occurs before receiving a response from a remote service. If the remote service successfully processed the request (e.g., sent an OTP) but the response was lost, a naive retry by the client will lead to the operation being executed again, resulting in duplicate actions (e.g., two OTPs sent to the user). This underscores the critical need for operations to be idempotent or for clients to handle failures gracefully.

Anatomy of an RPC Call (gRPC Example)

The article uses gRPC as a concrete example to illustrate the lifecycle of an RPC. When a client invokes a method on a generated stub, the following steps occur:

  1. Request Construction: The client application builds the request payload.
  2. Stub Invocation: The client calls a method on its generated client stub, which acts as a local proxy for the remote service.
  3. Encoding: The request is serialized into a compact binary format, typically using Protocol Buffers, ensuring a shared wire format across different languages (e.g., Go to Java).
  4. Network Transmission: The serialized request is sent over HTTP/2, leveraging features like multiplexing.
  5. Server Processing: The server runtime receives the request, decodes it, and dispatches it to the actual service method implementation. The service processes the request and generates a response.
  6. Response Transmission & Decoding: The response travels back through the same encoding, network, and decoding stages until the client receives it or a timeout occurs.

Interface Definition Language (IDL) and Code Generation

A key advantage of RPC frameworks like gRPC is the use of an IDL, such as Protocol Buffers' `.proto` files, to define service contracts. These definitions are language-agnostic and are used by code-generation tools to create client stubs and server interfaces for various programming languages. This ensures strong typing, catches many integration errors at compile-time rather than runtime, and facilitates compatible evolution of APIs, allowing gradual rollouts without requiring all services to be deployed simultaneously.

protobuf
service NotificationService {
  rpc SendOtp(SendOtpRequest) returns (SendOtpResponse);
}

message SendOtpRequest {
  string email = 1;
  string request_id = 2;
}

message SendOtpResponse {
  bool accepted = 1;
}

RPC vs. REST and Network Reliability

The article clarifies that RPC is an architectural style focused on operations, similar to how REST is an architectural style focused on resources. Both can utilize HTTP as a transport layer; gRPC commonly runs over HTTP/2. While Protocol Buffers can result in smaller messages and HTTP/2 offers features like multiplexing, these do not inherently guarantee lower latency or higher reliability. Factors like database load, network distance, and server processing time often dominate overall response times. The core challenge in distributed systems remains handling network failures and ensuring consistency, regardless of the communication style.

RPCgRPCProtocol BuffersHTTP/2IdempotencyMicroservices CommunicationDistributed TransactionsError Handling

Comments

Loading comments...