TL;DR
A remote procedure call (RPC) is a way for one program to run a function that lives in another program, often on another machine, by writing what looks like an ordinary function call. Hidden helper code, called stubs, packs the arguments into bytes, sends them over the network, runs the function on the far side, and sends the result back. It makes network work read like local work, but the network can fail in ways a local call never can.
Builds on: Client-Server Model, Data Serialization · Leads to: Network Protocol, Hypertext Transfer Protocol
Where it's used
Internal services at large companies. A checkout service might call a pricing service, a tax service and an inventory service thousands of times per second. Each call is an RPC, and the team defines each function's signature in a shared file so every service agrees on it.
Network file systems. Sun Microsystems built its Network File System on top of Sun RPC in the mid-1980s. Reading a
file on a server became a call to a read function that the server ran, which is why a remote disk could look local to
a program.
Mobile and data-center clients. gRPC, released by Google in 2015 and used widely since, lets a mobile app call a server function with an argument type defined once and generated for each language. Messages travel as compact binary data rather than long text.
How it works
Take one tiny call: a client asks a server to run add(2, 3), and the server's add returns 5. Two stubs do the
hidden work. The client stub has the same signature as add, and the server stub knows how to find add by a number.
sequenceDiagram participant App as Client program participant CS as Client stub participant Net as Network participant SS as Server stub participant Fn as add on server App->>CS: add(2, 3) CS->>Net: request 17, method 1, args 2 and 3 (16 bytes) Net->>SS: same 16 bytes SS->>Fn: add(2, 3) Fn-->>SS: 5 SS->>Net: reply 17, result 5 (8 bytes) Net->>CS: same 8 bytes CS-->>App: 5
- The program makes a normal call. The client code calls
add(2, 3)on a local object called a stub. It does not know the function is remote. Check: the program is waiting for a return value, just as with a local call. - The client stub builds a request. It gives the call a request number, 17, so a reply can be matched to it later.
It writes the method number 1, which the server maps to
add, and the two arguments, 2 and 3. In a compact layout each number takes four bytes, so the request is 4 + 4 + 4 + 4 = 16 bytes. Check: the arguments are now bytes, serialized as the previous lesson describes. - The bytes cross the network. The request travels over a connection, usually TCP, which delivers the bytes in order and resends any that are lost (see automatic repeat request and Transmission Control Protocol). Check: the server receives the same 16 bytes it was sent.
- The server stub decodes and dispatches. It reads the request number 17, looks up method 1 and finds
add. It decodes the two arguments, 2 and 3. Check: the server now holds the same values the client had. - The real function runs. The server calls
add(2, 3)and gets 5. From here on the function does not know it was called remotely. Check: the work done is identical to a local call. - The server stub sends the reply. It writes request number 17 and the result 5, which is 4 + 4 = 8 bytes. Check: the reply carries the request number so the client can match it.
- The client stub returns the value. It finds the waiting call numbered 17, decodes 5, and returns it to the
program as if
addhad run locally. Check: the program receives 5 and continues.
If no reply arrives in time, the client stub raises an error instead of waiting forever. The program must now decide whether to try again, and that decision is where RPC gets hard.
The math
Let a local call take time , the time the function itself needs.
A remote call adds the steps above. Let be the time to encode and decode the arguments and the result, the network round-trip time, and the time the server spends on dispatch. Then
Step 2 in symbols. A message is a sequence of fields, and each fixed-size field of bytes adds to the size. For the request, , four fields of four bytes each.
Step 5 in symbols. The server's work is the same function call, so its term does not change.
Step 7 in symbols. The client needs a timeout that is at least the expected . If it is shorter, the client may give up on a call that would have succeeded. If each attempt succeeds with probability and the client retries until success, the number of attempts follows a geometric distribution, and its expected value is
Worked examples
Use the request from the walkthrough, where each number takes four bytes. Count the bytes of a request that carries one argument, the number 7, and a request number and method number.
- Request number: 4 bytes.
- Method number: 4 bytes.
- One argument: 4 bytes.
- Total: 4 + 4 + 4 = 12 bytes.
- Interpretation: each extra argument of this kind adds exactly four bytes, so a call with three such arguments needs 4 + 4 + 12 = 20 bytes.
A server sits 40 milliseconds away, so the round-trip time is ms. The function's work takes ms, and the server's dispatch and encoding together add ms.
- Local time: ms.
- Remote time: ms.
- Ratio: , so the remote call is about 21 times slower than the local call.
- Interpretation: the network dominates. Making the function faster barely helps; making fewer calls, or moving the work closer, is what reduces the total.
A checkout client calls charge_card on a payment server. The round trip takes 60 milliseconds, the server's work takes
5 milliseconds, and overhead adds 3 milliseconds. The server charges the card, but the reply is lost on the way back, so
the client sees a timeout. The client retries the same call.
- First attempt: the server charges the card, the reply is lost, and the client waits out its timeout.
- Retry: the client sends the same arguments again. Without a protection, the server treats it as a new request and charges the card a second time.
- The customer is now charged twice, even though each attempt looked like a normal RPC.
- Fix: the client sends a unique key with the request, such as a random identifier for this purchase. The server stores the result under that key and, on a retry, returns the stored result without charging again. A call that can be repeated with the same effect as running it once is called idempotent.
- Interpretation: a timeout does not tell the client whether the work happened. Retries are safe only when the operation is idempotent or the server can detect the repeat.
Try it: with ms, ms and ms, what is for one successful attempt? Answer: ms.
Common mistakes and tradeoffs
It is tempting to think that an RPC behaves like a local call. In fact it can fail in ways a local call cannot. The network may lose the request, the server may crash after doing the work, or the reply may never arrive. From the client's side all three look the same, a timeout, and the client cannot tell which one happened. The program must handle failure at every call, not as an exception it rarely sees.
It is tempting to think that retrying a failed call is always safe. In fact a retry of a call that already ran can do the work twice, as in the payment example. Retries are safe only when the operation is idempotent, or when the server keeps a record that lets it recognize the repeat.
It is tempting to think that passing an object to a remote function passes a reference to it. In fact the server receives a copy, built from the serialized bytes. A Pointer from the client's memory means nothing on the server, so the function cannot change the caller's object in place. The result must come back as a value.
When to use RPC: between services that one team controls, where a strict shared interface, speed and a compact encoding matter. When not to use it: for public web APIs read by browsers and many unknown clients, where a resource-style interface over HTTP (see Hypertext Transfer Protocol) is easier to cache and to evolve, and for long streams of data, which need their own design.
| Approach | Strength | Weakness |
|---|---|---|
| RPC with a generated interface (gRPC) | Strict types, compact messages, fast | Both sides must regenerate code when the interface changes |
| HTTP with resource URLs (REST) | Caches well, works with browsers and any client | Looser contract, more text on the wire |
| Message queue | Decouples sender and receiver in time | No direct reply, so the logic is harder to follow |
History
Before RPC was common, a program that needed another machine's services usually wrote its own messages by hand, which made each new service a new protocol. In 1984 Andrew Birrell and Bruce Nelson published a paper describing how to build RPC so that a call behaves like a local one, covering how stubs are generated and how failures are reported. That same decade, Sun Microsystems built its Network File System on Sun RPC, and the design spread through Unix systems.
RPC systems later grew more capable. Common Object Request Broker Architecture (CORBA) and Java remote method invocation tried to share objects across machines in the 1990s, but their complexity made many teams prefer simpler interfaces. The move back toward explicit messages came with web services, and in 2015 Google released gRPC, which pairs a generated interface with a binary encoding and runs over HTTP/2. It caught on because it made the strict interface and the compact encoding come together in one tool.
Recap
- An RPC lets a program call a function on another machine with an ordinary-looking call.
- Client and server stubs hide the work: they serialize the arguments, send them, dispatch the call, and return the result with the same request number.
- The network adds time, roughly the round-trip time plus overhead, so a remote call is far slower than a local one.
- A timeout does not reveal whether the work ran, so retries need idempotent operations or keys the server can check.
- Arguments cross as copies, never as pointers.
Next, read Protocol Layering to see where RPC sits in the stack of network layers, and Retransmission Timeout to see how a sender decides how long to wait before a retry.
Check yourself
QuestionWhy can a program not treat an RPC timeout as proof that the server did nothing?Show answer
The request may have been lost before reaching the server, which then did nothing. But the server may have run the function and lost the reply on the way back. The client sees the same timeout in both cases, so it cannot tell them apart without an idempotency mechanism.
QuestionAn RPC's round-trip time is 80 ms, its work takes 4 ms, and its overhead is 2 ms. What is the total remote time for one successful attempt?Show answer
Add the parts: 80 + 4 + 2 = 86 ms. The network round trip is the largest term, so reducing the work from 4 ms to 1 ms would save only 3 ms of the 86.
QuestionWhy does an RPC pass a copy of an object rather than a pointer to it?Show answer
The server runs in its own process, with its own memory. A Pointer is an address valid only in the client's memory, so on the server it would point to unrelated data or nothing. The arguments are therefore serialized into bytes, the server rebuilds them as new objects, and any result is sent back as a value.
QuestionA service retries an RPC with probability of success 0.5 per attempt. On average, how many attempts are needed, and what does that suggest about the design?Show answer
The expected number of attempts is 1 divided by 0.5, which is 2. Each success costs two attempts on average, so if the operation is not idempotent, the expected number of duplicated effects is high. The design should make the operation idempotent or add a request key the server checks.