
What is RPC …
遠端程序呼叫 (Remote Procedure Call),顧名思義,就是可以呼叫遠端的機器,執行某些運算。這個方式大量地使用在分布式系統中,增加系統的運算能力。
Remote procedure call literally means that it allows programmers to call procedures that are located and executed on remote machines. RPC is widely used in distributed systems, in order to improve the capacity of calculation of systems.
Advantages …
- RPC 可以基於多種協定。 RPC is based on several protocols, like HTTP, TCP.
- RPC 自帶負載平衡策略。 RPC has its own load balance strategy.
- RPC 的傳輸效率好,因為請求的體積小。 Since RPC has small content of request, it has high efficiency of transmission.

Challenges …
1.
需要一定的傳輸時間。
Since it needs to pass parameters and results, it takes certain time.
2.
需要有錯誤處理跟確保資料正確性.
It needs fault tolerance and ensures correctness of data.

Process …
1.
客戶端將參數打包成訊息,並將訊息傳給服務端。
Client machine packs the parameters into a message and sends message to server machine.
2.
客戶端會等待(暫停) 直到收到服務端的回復。
Client machine will wait( block itself) until it receives the reply from server machine.
3.
服務端收到請求後,OS 會將訊息傳給 Server Stub。
After receiving the request, server machine’s OS passes message to Server Stub.
4.
Server Stub 打開訊息,取得參數,呼叫執行程序。
Server Stub unpacks message, gets the parameters, and calls procedure.
5.
得到結果之後,打包結果,傳給客戶端。
After getting result, packs result into message and send back to client machine.
6.
客戶端收到結果後,Client Stub 解除等待,打開訊息,並且傳給呼叫者。
Client machine received message, then it unblocks Client Stub. Client Stub unpacks message and passes it to caller.

Some questions …
1.How are pointers or references passed?
直接複製整個資料結構。
Simply copy entire data structure.
2. Why does it pack parameters?
將參數轉換成特定的格式,適合在機器跟網路之間傳輸(語言獨立)。
In order to transform the data to specific format which is suitable for transmission between machines and network(language independence).
3.Why do parameters need to transform to specific format?
因為客戶端的程式語言跟服務端的程式語言可能不同。所以轉換成跟語言無關的特定格式,這樣無論什麼程式語言,都可以理解訊息內容。
Client and server might use different programming languages. By transforming to specific format (language independent), the message can be read no matter programming language.
4.Does client take long time on waiting reply from server?
這取決於網路跟計算速度。有時候很可能等待太久而超時。異步 RPC 是一種解決方式,但我不會在這裡過多討論。
It depends on the network and calculation of server. It might take too long and timeout. Asynchronous RPC is one of solutions, but I won’t talk about it too much.
5.What is asynchronous RPC?
客戶端不需要等待服務端的回應,只要知道服務端已經收到訊息後,客戶端就會解除等待。服務端執行完會,會主動呼叫客戶端(callback)。
Client doesn’t wait for reply from server. Once the client knows that server accepts the request, it unblocks itself. Server will inform client after it gets the result(Callback).
-MsHe