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 #systemdesignRemote 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.
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:
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.
service NotificationService {
rpc SendOtp(SendOtpRequest) returns (SendOtpResponse);
}
message SendOtpRequest {
string email = 1;
string request_id = 2;
}
message SendOtpResponse {
bool accepted = 1;
}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.