The idea: a remote call that looks like a local one

A remote procedure call (RPC) lets a program call a function that runs on another machine, with the same syntax as a local call: r = add(4, 7). Two pieces of generated code make this work. The client stub has the same signature as add. It packs the procedure number and the arguments into bytes (marshalling), sends them, waits, and unpacks the answer. The server stub receives the bytes, unpacks them (unmarshalling), calls the real add, and packs the result into a reply. Every call carries a transaction id (xid), so the client stub can match a reply to its call.

Left, the client: the app calls add(4, 7), the client stub turns it into 48 bytes: xid 1001, CALL, program 0x20000001, procedure 1, and the words 00 00 00 04 and 00 00 00 07. The bytes cross the network. Right, the server stub unmarshals them and calls add, which returns 11; a 28-byte reply with 00 00 00 0B goes back.
The stubs hide the network: the app sees a function call, the network sees 48 bytes going one way and 28 bytes coming back.

The page models Sun's ONC RPC (the RPC under NFS) on UDP. Each call starts from the same state: an account with a balance of $500 on the server, xid 1001, server instance 1. A message takes one tick, a procedure one tick, and the client stub sets a timer of 4 ticks after each send. Failures hit the first attempt only.

Marshalling with XDR

Client and server may be different machines with different byte orders, so the bytes follow a shared format, XDR (External Data Representation, RFC 4506). In XDR every integer is a 4-byte big-endian word: the most significant byte comes first. 1001 is 0x000003E9, sent as 00 00 03 E9. In Demo 1 the client stub fills these words for add(4, 7):

wordvaluebytes
xid100100 00 03 E9
msg_type0 = CALL00 00 00 00
rpcvers200 00 00 02
prog, vers0x20000001, 120 00 00 01, 00 00 00 01
proc1 = ADD (2 = DEBIT)00 00 00 01
cred, verfAUTH_NONE4 words of 00 00 00 00
a, b4, 700 00 00 04, 00 00 00 07

That is 12 words, 48 bytes. debit(100) has one argument (00 00 00 64), so its request is 44 bytes. The reply is 7 words, 28 bytes: xid, REPLY, MSG_ACCEPTED, an empty verifier (2 words), SUCCESS and the result, 11 = 00 00 00 0B. The server stub reads the words in the same order and gets 4 and 7 back on any CPU. For byte order itself see Little-endian vs big-endian; for a newer encoding with field tags and variable-length ints see gRPC, whose stubs come from a .proto file the way ONC RPC stubs come from an .x file through rpcgen.

What can go wrong

A local call either returns or the whole program dies. A remote call has more ways to fail:

  • lost request: the server never hears of the call;
  • lost reply: the server did the work, but the answer is gone;
  • slow server: the work takes longer than the client's timer;
  • server crash, either before or after it ran the procedure;
  • client crash: the server works for a caller that no longer exists.

In the first four the client sees exactly the same thing: no reply before the timer fires. It cannot tell a lost request from a lost reply, so it cannot know whether the procedure ran. What the stubs do next is the call's semantics.

Three call semantics

semanticsclient resends?server filters copies?a copy that gets throughthe procedure runs
maybenono–0 or 1 times
at-least-onceyes, same xidnoruns again1 or more times
at-most-onceyes, same xidyes, by xidgets the saved reply0 or 1 times

Maybe sends once and gives up at the timeout. In Demo 2 the request is lost: at t = 4 the app gets an error and the balance is still $500. At-least-once resends the same request (up to 3 tries on the page). With the same lost request, the retry at t = 4 gets through, the server debits once, and the app has 400 at t = 7.

At-least-once and idempotent procedures

Resending is harmless when the request was lost, and harmful when only the reply was. In Demo 3 the reply to debit(100) is lost. The retry runs debit a second time: the balance is $300, not $400. The same failure with add(4, 7) also runs add twice, and the app still gets 11. A procedure is idempotent when running it twice has the same effect as running it once: add, "read block 7", "set the balance to 400". At-least-once is correct for those, and that is why the classic NFS (version 2 and 3) was designed with idempotent operations where possible: "write these bytes at offset 8192" can be repeated, "append these bytes" cannot.

A slow server causes the same harm with nothing lost at all. In Demo 5 the first run of debit takes 6 ticks. The timer fires at t = 4, the retry runs quickly and its reply (400) reaches the app at t = 7. The slow first run then finishes too: the balance ends at $300, while the app was told 400. Its late reply arrives at t = 8, and the client stub drops it because no call is waiting.

At-most-once: the duplicate request table

For a non-idempotent procedure the server must recognise a copy. It keeps a duplicate request table (NFS servers call it the duplicate request cache), keyed by xid, with the state and the saved reply:

  • xid not in the table: note it in progress, run the procedure, save the reply, mark it done;
  • xid done: resend the saved reply, do not run again (Demo 4: lost reply, 1 run, $400, the app gets 400 at t = 6);
  • xid in progress: drop the copy, the first run will answer (Demo 5, second run: $400).
Two time-down diagrams of a client and a server. Left, at-least-once: the request for debit(100) arrives, the balance goes from 500 to 400, the reply is lost, the timer fires at t = 4, the same request xid 1001 arrives again and debit runs again: balance 300. Right, at-most-once: after the lost reply the retry finds xid 1001 done in the table and the server resends the cached reply 400; the balance stays 400.
The same lost reply: at-least-once runs debit again, at-most-once answers the copy from its table.

The table can be small and short-lived: an entry is only needed while the client may still resend, that is for a few timeouts. Idempotency Keys shows the same idea at the HTTP level, where the client picks the key and the server stores it in a database.

Server crashes: before or after?

A table in memory does not survive a crash. If the server simply ran every request it did not recognise after a restart, a crash after the debit would lead to a second debit: at-least-once again. At-most-once has to fail the call instead. Birrell and Nelson's RPC does it with the server's identity: the client binds to one server instance, and a restarted server is a new instance that rejects calls bound to the old one. On the page the restarted server is instance 2 and answers "ERR: server restarted".

Demo 6 shows what that costs. A crash before debit ends at $500; a crash after it ends at $400. In both runs the app gets the same error, so it cannot tell which happened. At-least-once (Demo 7) resends after the restart and, in the "after" case, debits twice: $300.

debit(100) from $500maybeat-least-onceat-most-once
no failure$400$400$400
lost request$500, error$400$400
lost reply$400, error$300, ran twice$400 (saved reply)
slow server$400, error$300, ran twice, app told 400$400 (copy dropped)
crash before executing$500, error$400$500, error
crash after executing$400, error$300, ran twice$400, error
client crash$400, no answer$400, no answer$400, no answer

No column is $400 with a correct answer in every row. Exactly-once would need the server to save "xid 1001 done, reply 400" in the same durable step as the debit, and the client to keep asking across its own crashes. That moves the problem into the application, as Delivery Semantics shows for messages and Idempotency Keys for HTTP. Why no protocol can do better is the subject of the Two Generals problem.

Client crashes and orphans

In Demo 8 the client crashes at t = 1, right after it sent debit(100). The server runs it anyway ($400) and its reply at t = 3 reaches a dead machine. Work done for a caller that is gone is an orphan. It wastes the server's time, holds locks, and can confuse the restarted client, which may send the debit again under a new xid. Remedies from the literature: the client logs its calls and kills orphans after a restart (extermination), or every call gets a time limit after which the server stops it (expiration).

How long to wait before a retry, and how to spread retries out, is the subject of Retry, Exponential Backoff and Jitter.

See also IoT device shadow, which sets a value on a device that may be offline by writing a cloud document instead of calling the device directly.

See also NFS vs AFS Client Caching, where NFS v3 relies on idempotent operations and resends a lost GETATTR with the same xid after a server reboot.

What the page leaves out

  • Binding: finding the server's port with the portmapper / rpcbind (program 100000), and authentication flavours other than AUTH_NONE.
  • RPC over TCP, where record marking frames the messages and TCP itself retransmits lost segments.
  • XDR strings, arrays, structs and unions; the page marshals only 32-bit ints.
  • Real servers run many calls at once and age entries out of the duplicate table; the page has one client, one call at a time and one table entry.
  • The instance check is a simplification of Birrell and Nelson's call identifiers; ONC RPC itself has no such word in its header.

On the mind map

This page on the Networking mind map · 71 pages in 10 branches · open it to see the whole map and test yourself in recall mode.