# RPC: Call a Function on Another Machine as If It Were Local

> Learn how a remote procedure call works step by step, why it can time out or run twice, and when it is the right style for service-to-service calls.

**In short:** A remote procedure call, or RPC, lets one program call a function on another machine as if it were local, though it can time out or run twice.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/rpc

## What You Will Learn

This lesson explains RPC, which stands for remote procedure call. An RPC lets one program run a function that lives on another machine. The code you write looks almost the same as a normal function call, but the call really crosses a network. That hidden detail causes most of the trouble. By the end, you will be able to walk through one RPC step by step, explain why a remote call can time out or run twice, and decide when RPC is a good style for a system and when it is not.

- **RPC:** Remote procedure call. One program calls a function that runs on another machine, and the call is written like a local one.
- **Procedure:** Another word for a function, or a named action that code can run.
- **Client:** The side that starts the call. It is the caller.
- **Server:** The side that holds the real function and runs it when a call arrives.
- **Stub:** A small helper piece of code that stands in for the remote function and handles the network work.
- **Marshalling:** Packing the arguments into a message that can travel over a network. Unmarshalling is the reverse: unpacking the message.
- **Timeout:** The longest time a caller will wait for an answer before it gives up.
- **Retry:** Sending the same request again after a failure or a timeout.
- **Idempotent:** Safe to repeat. Doing the action twice has the same result as doing it once.

## How Does an RPC Work, Step by Step?

Think of an RPC as a chain of hand-offs. Each part has one job. The order below follows the classic model that most RPC systems still use.

1. **The caller invokes the stub:** The calling code calls a function by name, as if the function were in its own program. That name is really the client stub, a helper that lives on the caller's machine.
2. **The stub marshals the arguments:** The stub packs the arguments into one message, written in a standard format that both sides can read.
3. **The message crosses the network:** The network layer sends the message to the server machine. Nothing runs on the server until this message arrives.
4. **The server stub unmarshals:** On the server, a server stub unpacks the message and reads out each argument.
5. **The real function runs:** The server stub calls the real function in the normal way. The function does its work and returns a value to the stub.
6. **The result travels back:** The server stub marshals the result into a reply and sends it back. The client stub unpacks the reply and returns the value to the caller, who sees what looks like a normal return.

Notice what the caller never does. It never builds a message, opens a connection, or reads a reply byte by byte. Those jobs belong to the stubs and the runtime that sits under them.

### What Are Stubs, the Hidden Helper?

A stub is a procedure that hides the packing and the network details from the code around it. Many RPC systems describe their functions in a shared interface file, written in a neutral format called an interface description language. Tools read that file and generate the stub code for each language, so the client and the server agree on names and types before any code runs.

> **Tip:** Marshalling must use a standard format, and the type representations on both sides must match. If the two sides disagree about the format, a call can fail even when the network and the server are fine.

## Why Is a Remote Call Not a Local Call?

The code looks local, but the situation is not local. Remote calls are usually much slower and less reliable than local calls. The RPC model hides the location of the function, but the two kinds of call are usually not identical, so good designers still treat them differently.

- **Slower:** A local call stays inside one program's memory. A remote call waits for the network trip, the server's queue, and the trip back with the reply.
- **Timed out:** The caller can stop waiting before any answer arrives. The timeout is the caller's own limit. It does not tell the caller whether the server finished the work.
- **Failed partway:** The server may finish part of the job and then crash before it replies. The caller sees only silence, so it cannot tell which part happened.
- **Run twice:** After a timeout, a caller that tries again may cause the same work to happen a second time.

The key point is that the caller often does not know whether the remote function ran at all. Calls to idempotent functions, which are safe to repeat, are easier to handle. Many other functions are not safe to repeat, and that is where the real design work begins.

### How Do Timeouts and Retries Work?

Good RPC practice starts with a deadline on every call. In gRPC, a popular RPC framework, the client sets how long it will wait, and the call ends with a DEADLINE_EXCEEDED error when that time runs out. Idempotence matters when a call is repeated, because the caller may not know whether the procedure ran. You must make that choice.

1. **Set a deadline on every call:** Never let a call wait forever. Pick a limit that fits the work, and pass it on to the next service if that service makes more calls.
2. **Treat a timeout as unknown:** A timeout means you do not know the result yet. The work may have finished, may have failed, or may still be running.
3. **Retry only when a repeat is safe:** Retry a call only if repeating it cannot cause harm, or if the server can spot a repeat. Otherwise, check the result first.
4. **Stop and report clearly:** Stop after a small number of tries, and return a clear error to the user instead of waiting or guessing.

> **Tip:** A timeout means I do not know yet. It does not mean nothing happened. Write your code for that middle state.

### Try It: A Lost Reply Charges Twice

The sample below imitates one RPC inside a single program. The server stub runs the real charge first, and then the reply is lost on the way back. The caller sees a timeout, but the money has already moved. A retry then charges again.

```python
import json

charges = {}

def charge_card(user_id, amount):
    charges[user_id] = charges.get(user_id, 0) + amount
    return {'ok': True, 'total': charges[user_id]}

def server_side(message):
    request = json.loads(message)
    result = charge_card(**request['args'])
    return json.dumps({'result': result})

def charge_card_stub(user_id, amount, reply_lost=False):
    message = json.dumps({'method': 'chargeCard', 'args': {'user_id': user_id, 'amount': amount}})
    reply = server_side(message)
    if reply_lost:
        raise TimeoutError('no reply before the deadline')
    return json.loads(reply)['result']

try:
    charge_card_stub('ann', 10, reply_lost=True)
except TimeoutError as err:
    print('caller saw:', err)
print('server recorded:', charges)
charge_card_stub('ann', 10)
print('after retry:', charges)

```

Output:

```text
caller saw: no reply before the deadline
server recorded: {'ann': 10}
after retry: {'ann': 20}

```

The caller did nothing wrong by retrying, yet the customer was charged twice. The fix is not in the caller alone.

## Worked Example: Place Order

Imagine a food delivery app. A user taps Place order. The order service must take payment before it confirms the food. It makes an RPC to the payment service. In the order code, the call looks like this: chargeCard(userId, amount). Here is what happens behind that one line.

1. **The user taps Place order:** The app sends the order to the order service. The user sees a spinner and waits.
2. **The order service calls the stub:** The order code calls chargeCard(userId, amount). The name points to a client stub that lives inside the order service.
3. **The stub marshals the arguments:** The stub packs userId and amount into one message. The payment service is on another machine, so the values must travel as bytes.
4. **The message crosses the network:** The order service waits. Its call is blocked until a reply arrives or the deadline passes.
5. **The payment service runs the real charge:** The server stub unpacks the message and runs the real charge logic, which talks to the card network.
6. **Success or timeout:** If the reply arrives, the order service confirms the order. If the deadline passes first, the order service gets a timeout error and does not know whether the card was charged.
7. **Check before any retry:** Before it tries again, the order service looks up its own request ID with the payment service. If the charge already exists, it does not charge a second time. Only then does it confirm the order or report a clear error.

The example shows the main idea of this lesson. The code reads like a simple function call, but the success of that call depends on the network, the server, and the plan for a timeout. The function name chargeCard is clear, but the design choices around it are where safety comes from.

## When Does RPC Fit and When Does It Not?

RPC is a style, not a rule. A useful way to compare styles is this. RPC is organized around actions, which are function calls such as chargeCard. Resource-style APIs, such as REST, are organized around things that have their own addresses and are changed with HTTP verbs. One source puts it simply: REST organizes APIs as resources, while RPC organizes them as remote function calls.

- **Good fit: internal calls:** Calls between your own services suit RPC well, especially when you control both ends and the interface is clear. Action-style calls that run fast and often fit this shape. RPC is suited to internal services.
- **Poor fit: public or browser APIs:** Outside users and third-party consumers need a simple and widely understood interface. Resource-style APIs usually work better there. For public APIs, REST often wins because it is simple and universal.
- **Trade-off: tight coupling:** When callers depend on exact function names and argument lists, a change to one signature can break them. Both teams must plan upgrades together.

### The Coupling Trade-off

Coupling means how much one part depends on another. With RPC, the caller depends on the function's name, its arguments, and what it means. That makes calls quick to write and easy to read. It also means that a small change on the server can force a change on every caller.

> **Tip:** Ask one question first: do I control both ends of this call? If yes, RPC is a strong option. If no, choose a style that outsiders can read and use without your help.

**Quick check:** Your checkout service calls a payment service with an RPC. The call times out and no response arrives. Can you safely call the payment service again?

A. Yes. A timeout means the charge never happened, so a retry is harmless.
B. Yes. Retries always fix timeouts, and the payment service ignores duplicates by itself.
C. Not without a check. The charge may already have run, so a second call could charge the customer twice.
D. No. After any timeout, the checkout service must never call the payment service again.

**Answer:** C. A timeout only means the caller stopped waiting. The payment may have finished before the reply was lost. A retry is safe only when the payment service can recognize the repeat, for example through a request ID it has already seen.

**Quick check:** A company has a mobile app and partner companies that call its order API from outside. Its own inventory service is called by its order service, and the same team runs both. Which style fits the inventory call best?

A. A resource-style public API, because RPC is never allowed inside a company.
B. RPC, because the same team controls both ends and the call is internal.
C. RPC for the mobile app and partner calls, because outside users need function names.
D. A shared message queue only, because RPC cannot have any interface at all.

**Answer:** B. The inventory call stays inside one team's own services, so RPC suits it. The outside traffic is a different case, where a resource-style API is usually easier for others to use.

## Key Points to Remember

- **An RPC is a call across a network:** The caller uses a stub, the arguments are marshalled into a message, the server runs the real function, and the result comes back the same way.
- **Silence is not an answer:** A timeout leaves the outcome unknown. Plan for that state before you write the retry code.
- **Use RPC where you control both ends:** Internal, action-style calls suit RPC. Public and browser-facing APIs usually suit a resource-style design better.

## Sources

- [Remote procedure call - Wikipedia](https://en.wikipedia.org/wiki/Remote_procedure_call)
- [Core concepts, architecture and lifecycle - gRPC documentation](https://grpc.io/docs/what-is-grpc/core-concepts/)
- [Remote Procedure - an overview (ScienceDirect topics page)](https://www.sciencedirect.com/topics/computer-science/remote-procedure)
- [What Is Remote Procedure Call (RPC)? - phoenixNAP glossary](https://phoenixnap.com/glossary/remote-procedure-call/)
- [Remote Procedure Call (RPC) - WPI course notes](https://web.cs.wpi.edu/~cs4514/b98/week8-rpc/week8-rpc.html)
- [REST vs RPC: when to use gRPC, tRPC, oRPC, or REST - Thiago Marinho](https://tgmarinhopro.com/en/blog/rest-vs-rpc-when-to-use-grpc-trpc-orpc-en)
- [gRPC vs REST for Internal Microservices APIs: A Practical Comparison - Matthew Swong](https://www.matthewswong.com/en/blog/microservices-grpc-vs-rest-internal/)
- [How to Handle Retries and Timeouts in gRPC - OneUptime](https://oneuptime.com/blog/post/2026-01-24-grpc-retries-timeouts/view)
