{"id":"communication","title":"Communication","sections":[{"id":"overview","title":"Overview","html":"<p class=\"figure\">\n  <img src=\"/repo/images/5KeocQs.jpg\" loading=\"lazy\" decoding=\"async\" alt=\"Diagram\">\n  <br>\n  <i><a href=\"http://www.escotal.com/osilayer.html\" target=\"_blank\" rel=\"noopener noreferrer\">Source: OSI 7 layer model</a></i>\n</p>","headings":[]},{"id":"hypertext-transfer-protocol-http","title":"Hypertext transfer protocol (HTTP)","html":"<h3 id=\"hypertext-transfer-protocol-http\">Hypertext transfer protocol (HTTP)</h3>\n<p>HTTP is a method for encoding and transporting data between a client and a server.  It is a request/response protocol: clients issue requests and servers issue responses with relevant content and completion status info about the request.  HTTP is self-contained, allowing requests and responses to flow through many intermediate routers and servers that perform load balancing, caching, encryption, and compression.</p>\n<p>A basic HTTP request consists of a verb (method) and a resource (endpoint).  Below are common HTTP verbs:</p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div class=\"table-wrap\"><table><thead><tr><th>Verb</th><th>Description</th><th>Idempotent*</th><th>Safe</th><th>Cacheable</th></tr></thead><tbody><tr><td>GET</td><td>Reads a resource</td><td>Yes</td><td>Yes</td><td>Yes</td></tr><tr><td>POST</td><td>Creates a resource or trigger a process that handles data</td><td>No</td><td>No</td><td>Yes if response contains freshness info</td></tr><tr><td>PUT</td><td>Creates or replace a resource</td><td>Yes</td><td>No</td><td>No</td></tr><tr><td>PATCH</td><td>Partially updates a resource</td><td>No</td><td>No</td><td>Yes if response contains freshness info</td></tr><tr><td>DELETE</td><td>Deletes a resource</td><td>Yes</td><td>No</td><td>No</td></tr></tbody></table></div>\n<p>*Can be called many times without different outcomes.</p>\n<p>HTTP is an application layer protocol relying on lower-level protocols such as <strong>TCP</strong> and <strong>UDP</strong>.</p>\n<h4 id=\"sources-and-further-reading-http\">Source(s) and further reading: HTTP</h4>\n<ul>\n<li><a href=\"https://www.nginx.com/resources/glossary/http/\" target=\"_blank\" rel=\"noopener noreferrer\">What is HTTP?</a></li>\n<li><a href=\"https://www.quora.com/What-is-the-difference-between-HTTP-protocol-and-TCP-protocol\" target=\"_blank\" rel=\"noopener noreferrer\">Difference between HTTP and TCP</a></li>\n<li><a href=\"https://laracasts.com/discuss/channels/general-discussion/whats-the-differences-between-put-and-patch?page=1\" target=\"_blank\" rel=\"noopener noreferrer\">Difference between PUT and PATCH</a></li>\n</ul>","headings":[{"id":"hypertext-transfer-protocol-http","title":"Hypertext transfer protocol (HTTP)","depth":3},{"id":"sources-and-further-reading-http","title":"Source(s) and further reading: HTTP","depth":4}]},{"id":"transmission-control-protocol-tcp","title":"Transmission control protocol (TCP)","html":"<h3 id=\"transmission-control-protocol-tcp\">Transmission control protocol (TCP)</h3>\n<p class=\"figure\">\n  <img src=\"/repo/images/JdAsdvG.jpg\" loading=\"lazy\" decoding=\"async\" alt=\"Diagram\">\n  <br>\n  <i><a href=\"http://www.wildbunny.co.uk/blog/2012/10/09/how-to-make-a-multi-player-game-part-1/\" target=\"_blank\" rel=\"noopener noreferrer\">Source: How to make a multiplayer game</a></i>\n</p>\n<p>TCP is a connection-oriented protocol over an <a href=\"https://en.wikipedia.org/wiki/Internet_Protocol\" target=\"_blank\" rel=\"noopener noreferrer\">IP network</a>.  Connection is established and terminated using a <a href=\"https://en.wikipedia.org/wiki/Handshaking\" target=\"_blank\" rel=\"noopener noreferrer\">handshake</a>.  All packets sent are guaranteed to reach the destination in the original order and without corruption through:</p>\n<ul>\n<li>Sequence numbers and <a href=\"https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation\" target=\"_blank\" rel=\"noopener noreferrer\">checksum fields</a> for each packet</li>\n<li><a href=\"https://en.wikipedia.org/wiki/Acknowledgement_(data_networks)\" target=\"_blank\" rel=\"noopener noreferrer\">Acknowledgement</a> packets and automatic retransmission</li>\n</ul>\n<p>If the sender does not receive a correct response, it will resend the packets.  If there are multiple timeouts, the connection is dropped.  TCP also implements <a href=\"https://en.wikipedia.org/wiki/Flow_control_(data)\" target=\"_blank\" rel=\"noopener noreferrer\">flow control</a> and <a href=\"https://en.wikipedia.org/wiki/Network_congestion#Congestion_control\" target=\"_blank\" rel=\"noopener noreferrer\">congestion control</a>.  These guarantees cause delays and generally result in less efficient transmission than UDP.</p>\n<p>To ensure high throughput, web servers can keep a large number of TCP connections open, resulting in high memory usage.  It can be expensive to have a large number of open connections between web server threads and say, a <a href=\"https://memcached.org/\" target=\"_blank\" rel=\"noopener noreferrer\">memcached</a> server.  <a href=\"https://en.wikipedia.org/wiki/Connection_pool\" target=\"_blank\" rel=\"noopener noreferrer\">Connection pooling</a> can help in addition to switching to UDP where applicable.</p>\n<p>TCP is useful for applications that require high reliability but are less time critical.  Some examples include web servers, database info, SMTP, FTP, and SSH.</p>\n<p>Use TCP over UDP when:</p>\n<ul>\n<li>You need all of the data to arrive intact</li>\n<li>You want to automatically make a best estimate use of the network throughput</li>\n</ul>","headings":[{"id":"transmission-control-protocol-tcp","title":"Transmission control protocol (TCP)","depth":3}]},{"id":"user-datagram-protocol-udp","title":"User datagram protocol (UDP)","html":"<h3 id=\"user-datagram-protocol-udp\">User datagram protocol (UDP)</h3>\n<p class=\"figure\">\n  <img src=\"/repo/images/yzDrJtA.jpg\" loading=\"lazy\" decoding=\"async\" alt=\"Diagram\">\n  <br>\n  <i><a href=\"http://www.wildbunny.co.uk/blog/2012/10/09/how-to-make-a-multi-player-game-part-1/\" target=\"_blank\" rel=\"noopener noreferrer\">Source: How to make a multiplayer game</a></i>\n</p>\n<p>UDP is connectionless.  Datagrams (analogous to packets) are guaranteed only at the datagram level.  Datagrams might reach their destination out of order or not at all.  UDP does not support congestion control.  Without the guarantees that TCP support, UDP is generally more efficient.</p>\n<p>UDP can broadcast, sending datagrams to all devices on the subnet.  This is useful with <a href=\"https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol\" target=\"_blank\" rel=\"noopener noreferrer\">DHCP</a> because the client has not yet received an IP address, thus preventing a way for TCP to stream without the IP address.</p>\n<p>UDP is less reliable but works well in real time use cases such as VoIP, video chat, streaming, and realtime multiplayer games.</p>\n<p>Use UDP over TCP when:</p>\n<ul>\n<li>You need the lowest latency</li>\n<li>Late data is worse than loss of data</li>\n<li>You want to implement your own error correction</li>\n</ul>\n<h4 id=\"sources-and-further-reading-tcp-and-udp\">Source(s) and further reading: TCP and UDP</h4>\n<ul>\n<li><a href=\"https://gafferongames.com/post/udp_vs_tcp/\" target=\"_blank\" rel=\"noopener noreferrer\">Networking for game programming</a></li>\n<li><a href=\"http://www.cyberciti.biz/faq/key-differences-between-tcp-and-udp-protocols/\" target=\"_blank\" rel=\"noopener noreferrer\">Key differences between TCP and UDP protocols</a></li>\n<li><a href=\"http://stackoverflow.com/questions/5970383/difference-between-tcp-and-udp\" target=\"_blank\" rel=\"noopener noreferrer\">Difference between TCP and UDP</a></li>\n<li><a href=\"https://en.wikipedia.org/wiki/Transmission_Control_Protocol\" target=\"_blank\" rel=\"noopener noreferrer\">Transmission control protocol</a></li>\n<li><a href=\"https://en.wikipedia.org/wiki/User_Datagram_Protocol\" target=\"_blank\" rel=\"noopener noreferrer\">User datagram protocol</a></li>\n<li><a href=\"http://www.cs.bu.edu/~jappavoo/jappavoo.github.com/451/papers/memcache-fb.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">Scaling memcache at Facebook</a></li>\n</ul>","headings":[{"id":"user-datagram-protocol-udp","title":"User datagram protocol (UDP)","depth":3},{"id":"sources-and-further-reading-tcp-and-udp","title":"Source(s) and further reading: TCP and UDP","depth":4}]},{"id":"remote-procedure-call-rpc","title":"Remote procedure call (RPC)","html":"<h3 id=\"remote-procedure-call-rpc\">Remote procedure call (RPC)</h3>\n<p class=\"figure\">\n  <img src=\"/repo/images/iF4Mkb5.png\" loading=\"lazy\" decoding=\"async\" alt=\"Diagram\">\n  <br>\n  <i><a href=\"http://www.puncsky.com/blog/2016-02-13-crack-the-system-design-interview\" target=\"_blank\" rel=\"noopener noreferrer\">Source: Crack the system design interview</a></i>\n</p>\n<p>In an RPC, a client causes a procedure to execute on a different address space, usually a remote server.  The procedure is coded as if it were a local procedure call, abstracting away the details of how to communicate with the server from the client program.  Remote calls are usually slower and less reliable than local calls so it is helpful to distinguish RPC calls from local calls.  Popular RPC frameworks include <a href=\"https://developers.google.com/protocol-buffers/\" target=\"_blank\" rel=\"noopener noreferrer\">Protobuf</a>, <a href=\"https://thrift.apache.org/\" target=\"_blank\" rel=\"noopener noreferrer\">Thrift</a>, and <a href=\"https://avro.apache.org/docs/current/\" target=\"_blank\" rel=\"noopener noreferrer\">Avro</a>.</p>\n<p>RPC is a request-response protocol:</p>\n<ul>\n<li><strong>Client program</strong> - Calls the client stub procedure.  The parameters are pushed onto the stack like a local procedure call.</li>\n<li><strong>Client stub procedure</strong> - Marshals (packs) procedure id and arguments into a request message.</li>\n<li><strong>Client communication module</strong> - OS sends the message from the client to the server.</li>\n<li><strong>Server communication module</strong> - OS passes the incoming packets to the server stub procedure.</li>\n<li><strong>Server stub procedure</strong> -  Unmarshalls the results, calls the server procedure matching the procedure id and passes the given arguments.</li>\n<li>The server response repeats the steps above in reverse order.</li>\n</ul>\n<p>Sample RPC calls:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span>GET /someoperation?data=anId</span></span>\n<span class=\"line\"><span></span></span>\n<span class=\"line\"><span>POST /anotheroperation</span></span>\n<span class=\"line\"><span>{</span></span>\n<span class=\"line\"><span>  \"data\":\"anId\";</span></span>\n<span class=\"line\"><span>  \"anotherdata\": \"another value\"</span></span>\n<span class=\"line\"><span>}</span></span></code></pre>\n<p>RPC is focused on exposing behaviors.  RPCs are often used for performance reasons with internal communications, as you can hand-craft native calls to better fit your use cases.</p>\n<p>Choose a native library (aka SDK) when:</p>\n<ul>\n<li>You know your target platform.</li>\n<li>You want to control how your \"logic\" is accessed.</li>\n<li>You want to control how error control happens off your library.</li>\n<li>Performance and end user experience is your primary concern.</li>\n</ul>\n<p>HTTP APIs following <strong>REST</strong> tend to be used more often for public APIs.</p>\n<h4 id=\"disadvantages-rpc\">Disadvantage(s): RPC</h4>\n<ul>\n<li>RPC clients become tightly coupled to the service implementation.</li>\n<li>A new API must be defined for every new operation or use case.</li>\n<li>It can be difficult to debug RPC.</li>\n<li>You might not be able to leverage existing technologies out of the box.  For example, it might require additional effort to ensure <a href=\"https://web.archive.org/web/20170608193645/http://etherealbits.com/2012/12/debunking-the-myths-of-rpc-rest/\" target=\"_blank\" rel=\"noopener noreferrer\">RPC calls are properly cached</a> on caching servers such as <a href=\"http://www.squid-cache.org/\" target=\"_blank\" rel=\"noopener noreferrer\">Squid</a>.</li>\n</ul>","headings":[{"id":"remote-procedure-call-rpc","title":"Remote procedure call (RPC)","depth":3},{"id":"disadvantages-rpc","title":"Disadvantage(s): RPC","depth":4}]},{"id":"representational-state-transfer-rest","title":"Representational state transfer (REST)","html":"<h3 id=\"representational-state-transfer-rest\">Representational state transfer (REST)</h3>\n<p>REST is an architectural style enforcing a client/server model where the client acts on a set of resources managed by the server.  The server provides a representation of resources and actions that can either manipulate or get a new representation of resources.  All communication must be stateless and cacheable.</p>\n<p>There are four qualities of a RESTful interface:</p>\n<ul>\n<li><strong>Identify resources (URI in HTTP)</strong> - use the same URI regardless of any operation.</li>\n<li><strong>Change with representations (Verbs in HTTP)</strong> - use verbs, headers, and body.</li>\n<li><strong>Self-descriptive error message (status response in HTTP)</strong> - Use status codes, don't reinvent the wheel.</li>\n<li><strong><a href=\"http://restcookbook.com/Basics/hateoas/\" target=\"_blank\" rel=\"noopener noreferrer\">HATEOAS</a> (HTML interface for HTTP)</strong> - your web service should be fully accessible in a browser.</li>\n</ul>\n<p>Sample REST calls:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span>GET /someresources/anId</span></span>\n<span class=\"line\"><span></span></span>\n<span class=\"line\"><span>PUT /someresources/anId</span></span>\n<span class=\"line\"><span>{\"anotherdata\": \"another value\"}</span></span></code></pre>\n<p>REST is focused on exposing data.  It minimizes the coupling between client/server and is often used for public HTTP APIs.  REST uses a more generic and uniform method of exposing resources through URIs, <a href=\"https://github.com/for-GET/know-your-http-well/blob/master/headers.md\" target=\"_blank\" rel=\"noopener noreferrer\">representation through headers</a>, and actions through verbs such as GET, POST, PUT, DELETE, and PATCH.  Being stateless, REST is great for horizontal scaling and partitioning.</p>\n<h4 id=\"disadvantages-rest\">Disadvantage(s): REST</h4>\n<ul>\n<li>With REST being focused on exposing data, it might not be a good fit if resources are not naturally organized or accessed in a simple hierarchy.  For example, returning all updated records from the past hour matching a particular set of events is not easily expressed as a path.  With REST, it is likely to be implemented with a combination of URI path, query parameters, and possibly the request body.</li>\n<li>REST typically relies on a few verbs (GET, POST, PUT, DELETE, and PATCH) which sometimes doesn't fit your use case.  For example, moving expired documents to the archive folder might not cleanly fit within these verbs.</li>\n<li>Fetching complicated resources with nested hierarchies requires multiple round trips between the client and server to render single views, e.g. fetching content of a blog entry and the comments on that entry. For mobile applications operating in variable network conditions, these multiple roundtrips are highly undesirable.</li>\n<li>Over time, more fields might be added to an API response and older clients will receive all new data fields, even those that they do not need, as a result, it bloats the payload size and leads to larger latencies.</li>\n</ul>","headings":[{"id":"representational-state-transfer-rest","title":"Representational state transfer (REST)","depth":3},{"id":"disadvantages-rest","title":"Disadvantage(s): REST","depth":4}]},{"id":"rpc-and-rest-calls-comparison","title":"RPC and REST calls comparison","html":"<h3 id=\"rpc-and-rest-calls-comparison\">RPC and REST calls comparison</h3>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div class=\"table-wrap\"><table><thead><tr><th>Operation</th><th>RPC</th><th>REST</th></tr></thead><tbody><tr><td>Signup</td><td><strong>POST</strong> /signup</td><td><strong>POST</strong> /persons</td></tr><tr><td>Resign</td><td><strong>POST</strong> /resign<br>{<br>\"personid\": \"1234\"<br>}</td><td><strong>DELETE</strong> /persons/1234</td></tr><tr><td>Read a person</td><td><strong>GET</strong> /readPerson?personid=1234</td><td><strong>GET</strong> /persons/1234</td></tr><tr><td>Read a person’s items list</td><td><strong>GET</strong> /readUsersItemsList?personid=1234</td><td><strong>GET</strong> /persons/1234/items</td></tr><tr><td>Add an item to a person’s items</td><td><strong>POST</strong> /addItemToUsersItemsList<br>{<br>\"personid\": \"1234\";<br>\"itemid\": \"456\"<br>}</td><td><strong>POST</strong> /persons/1234/items<br>{<br>\"itemid\": \"456\"<br>}</td></tr><tr><td>Update an item</td><td><strong>POST</strong> /modifyItem<br>{<br>\"itemid\": \"456\";<br>\"key\": \"value\"<br>}</td><td><strong>PUT</strong> /items/456<br>{<br>\"key\": \"value\"<br>}</td></tr><tr><td>Delete an item</td><td><strong>POST</strong> /removeItem<br>{<br>\"itemid\": \"456\"<br>}</td><td><strong>DELETE</strong> /items/456</td></tr></tbody></table></div>\n<p class=\"figure\">\n  <i><a href=\"https://apihandyman.io/do-you-really-know-why-you-prefer-rest-over-rpc/\" target=\"_blank\" rel=\"noopener noreferrer\">Source: Do you really know why you prefer REST over RPC</a></i>\n</p>\n<h4 id=\"sources-and-further-reading-rest-and-rpc\">Source(s) and further reading: REST and RPC</h4>\n<ul>\n<li><a href=\"https://apihandyman.io/do-you-really-know-why-you-prefer-rest-over-rpc/\" target=\"_blank\" rel=\"noopener noreferrer\">Do you really know why you prefer REST over RPC</a></li>\n<li><a href=\"http://programmers.stackexchange.com/a/181186\" target=\"_blank\" rel=\"noopener noreferrer\">When are RPC-ish approaches more appropriate than REST?</a></li>\n<li><a href=\"http://stackoverflow.com/questions/15056878/rest-vs-json-rpc\" target=\"_blank\" rel=\"noopener noreferrer\">REST vs JSON-RPC</a></li>\n<li><a href=\"https://web.archive.org/web/20170608193645/http://etherealbits.com/2012/12/debunking-the-myths-of-rpc-rest/\" target=\"_blank\" rel=\"noopener noreferrer\">Debunking the myths of RPC and REST</a></li>\n<li><a href=\"https://www.quora.com/What-are-the-drawbacks-of-using-RESTful-APIs\" target=\"_blank\" rel=\"noopener noreferrer\">What are the drawbacks of using REST</a></li>\n<li><a href=\"http://www.puncsky.com/blog/2016-02-13-crack-the-system-design-interview\" target=\"_blank\" rel=\"noopener noreferrer\">Crack the system design interview</a></li>\n<li><a href=\"https://code.facebook.com/posts/1468950976659943/\" target=\"_blank\" rel=\"noopener noreferrer\">Thrift</a></li>\n<li><a href=\"http://arstechnica.com/civis/viewtopic.php?t=1190508\" target=\"_blank\" rel=\"noopener noreferrer\">Why REST for internal use and not RPC</a></li>\n</ul>","headings":[{"id":"rpc-and-rest-calls-comparison","title":"RPC and REST calls comparison","depth":3},{"id":"sources-and-further-reading-rest-and-rpc","title":"Source(s) and further reading: REST and RPC","depth":4}]}],"sourceFiles":[]}