DEV Community

The Byte Mechanics
The Byte Mechanics

Posted on

One Timeout, Two OTPs: What RPC Really Does

Stubs, Protocol Buffers, HTTP/2, and why a failed call doesn't always mean a failed operation

One timeout. Two OTPs. And nothing crashed.

One line of code asks a remote server to send a login code. The server hands it to an email provider, and the provider accepts it. Then the response disappears on its way back. The application hits its deadline and reports an error. So did it fail, or did it succeed? Retry blindly, and the user gets a second code.

That trap sits inside a very useful idea: the Remote Procedure Call, or RPC. This article follows one call from a Go service to a Java service, through the generated stub, Protocol Buffers and HTTP/2, and ends with how to make that retry safe. If you'd rather watch, here's the animated version:

Full Video

The services, the request ID (OTP-42), the email flow and the deadline are illustrative. "Accepted" means the email provider accepted the message for delivery, not that it reached the inbox. Technical details are based on the gRPC documentation listed at the end.

The problem before RPC

Imagine an application with two services. An authentication service, written in Go, handles login. A notification service, written in Java, sends the OTP. When someone taps Log In, the auth service needs the notification service to do some work.

A normal HTTP API can do this. Send a POST request, pass an email address, parse the response, handle errors. There's nothing wrong with that, and mature HTTP libraries, typed clients and OpenAPI generators already remove a lot of boilerplate.

Now suppose twenty internal services need to call each other, in several languages, and your team wants one consistent, strongly typed contract between them. Instead of hand-maintaining every integration, what if the client method could be generated straight from the service contract?

A call that only looks local

A procedure is an operation. Remote means it runs in another process, often on another machine. With RPC, your application doesn't think about endpoints and payload encoding each time. It calls a method on a generated client:

resp, err := client.SendOtp(ctx, req)
Enter fullscreen mode Exit fullscreen mode

That line looks like a normal method call inside the Go application. But the real work happens inside the Java notification service.

The benefit isn't that the network disappears. It's that your code gets a well-defined interface for talking across that network.

Inside one RPC call, step by step

1. The request. The Go application builds a request with an email address and a request ID.

2. The stub. It calls the generated client stub, a local representative of the remote service whose methods match the remote contract.

3. Encoding. In gRPC, Protocol Buffers usually encode the fields into a compact binary message. Why encode at all? Because a Go struct and a Java object aren't interchangeable pieces of memory. Both sides need a shared wire format.

4. The network. The gRPC runtime sends the request over HTTP/2.

5. The server. The server runtime receives it, decodes the message and dispatches it to the Java method. The Java code submits the OTP for delivery and builds a response, which travels back through the same stages until the Go caller gets a response or an error.

So the stub isn't teleporting code. It turns a method call into a network conversation, and that distinction matters most when the network fails.

Where the method came from

Who writes that stub? Usually, not you. You start with an Interface Definition Language (IDL), a language-independent contract. For gRPC, that's commonly a .proto file written with Protocol Buffers:

service NotificationService {
  rpc SendOtp(SendOtpRequest) returns (SendOtpResponse);
}
message SendOtpRequest {
  string email = 1;
  string request_id = 2;
}
message SendOtpResponse {
  bool accepted = 1;
}
Enter fullscreen mode Exit fullscreen mode

Code-generation tools read the contract and create message types and gRPC interfaces for Go and Java. Your Go code calls the generated client; the Java team implements the server method. A compiler can now catch many type mismatches that would otherwise show up at runtime. And compatible changes, like adding a new field number without reusing an old one, can often roll out gradually, so you don't have to deploy every service at once.

RPC doesn't mean "no HTTP"

A common misconception: REST uses HTTP and RPC doesn't. Not true. REST is an architectural style, usually organized around resources. RPC is organized around calling operations. Both can use HTTP, and standard gRPC runs over HTTP/2.

HTTP/2 lets many streams share one connection, and Protocol Buffers can make messages smaller than equivalent JSON in many cases. But smaller messages don't guarantee lower latency: database work, network distance and server load may dominate your response time. Neither style is inherently more reliable either.

The useful question isn't "which one wins?" It's "which one fits this interaction?"

The request that succeeded twice

Back to the first request. The Go service sends OTP-42. The Java service processes it and asks an email provider to send the code. The provider accepts. The Java service returns success. But the response gets lost, and the Go client's deadline expires.

What does the caller know? Only that it didn't receive a successful result in time. It cannot conclude that the OTP wasn't sent. If it retries as a brand-new request, the notification service may send another email.

This is an ambiguous outcome: the caller doesn't know whether the remote side effect happened. A deadline stops the client from waiting forever. It doesn't undo work that already happened.

Retries aren't magic either. gRPC supports configurable retry policies and limited transparent retries, but it can't guarantee exactly-once execution of your business operation.

A safer design, in three layers

1. Same request ID on every attempt. A retry of OTP-42 is still OTP-42, not a new request.

2. Store that ID durably, with a uniqueness constraint. Two attempts arriving at the same time can't both create a new OTP. If the ID already exists, return the earlier result.

3. Hand the email off reliably. For example, record the work and an outbox event in one database transaction.

Even then, an outbox can retry delivery after a crash, so the final protection against duplicate emails may need to come from the email provider's own deduplication or idempotency support. The goal is to control duplicate effects, not to pretend the network can guarantee certainty.

A failed RPC does not necessarily mean a failed operation.

Why teams still choose gRPC

If one timeout can turn into two OTPs, why use gRPC for internal systems at all? Four reasons:

  • Contracts. Caller and server agree on method names and message types.
  • Generated clients. Go, Java and other supported languages share one service definition without sharing implementation code.
  • Efficient communication. Binary encoding and HTTP/2 can help with high-volume service traffic, depending on the workload.
  • Streaming. Beyond one request and one response, gRPC supports client, server and bidirectional streaming, so a long-running job can push progress updates instead of the client polling every second.

REST or RPC?

For a public API used by browsers, partners and third-party tools, a conventional HTTP API may be easier to inspect, integrate and debug. For internal services with shared contracts, several languages and heavy service-to-service traffic, gRPC can be a strong option.

Don't make it a false battle. REST-style APIs can also use OpenAPI contracts, generated clients, efficient formats and streaming techniques. And gRPC has costs: schema tooling, generated-code workflows, RPC-specific debugging, and browser access through tools such as gRPC-Web or a gateway.

Our login flow shows a common pattern: the browser calls a public HTTP API, and behind it the Go auth service calls the Java notification service over gRPC. The edge speaks HTTP; the services talk gRPC.

Decide by your consumers, deployment constraints, observability, latency measurements and how your team works, not by a benchmark copied from someone else's system.

The one-line takeaway

Behind that simple SendOtp call is a contract, generated code, serialization, a network, a remote handler, and a response that might never arrive. RPC makes remote communication easier to write. It doesn't make the network behave like memory.

So here's the question worth sitting with: if the server finishes the work but the response is lost, what should your retry do?

Sources

The services, request ID, email flow, deadline and size comparison are illustrative examples, not measurements.


โ–ถ๏ธ Watch the animated version: One Timeout, Two OTPs (RPC Explained) on YouTube: https://youtu.be/s93lZrzkoVo


Follow The Byte Mechanics

How software works, under the hood โš™๏ธ

Top comments (0)