InterviewNoodle
Published in

InterviewNoodle

System design — Communication

Client server communication

Now that we got data storage out of the way, let's discuss the next topic which is communication between client and server. It usually involves two components — client and server where the client sends request to the server, while the server responds with the resources. The communication needs to be set up between client and server to share various kinds of content(images, documents, multimedia, …)

In this blog, we are going to cover different protocols that exist for data transfer between client and server.

OSI Model — Open System Interconnection Model

The OSI (Open System Interconnection) defines the networking framework for implementing protocols in 7 layers. OSI model is used for data exchange between client and server. For more details, you can read my blog here.

Hypertext Transfer Protocol (HTTP)

HTTP is a method of encoding and transferring data between the client(web browser) and the web server. HTTP is an application level protocol that runs over lower-level protocols such as TCP and UDP. HTTP is the primary model used over internet to communicate between client and server.

Information is exchanged in form of hypertext document between client and server. Hypertext is structured text that uses hyperlinks between nodes containing text. HTML (HyperText Markup Language) is used to manipulate hypertext documents. With the help of HTTP and HTML, servers can send various kinds of resources including text, images, videos and application data.

HTTP is a request/response protocol where client makes a request and server issues response along with completion status info about the request. This self-contained information allows for the distributed nature of Internet where request/response passed through many routers and intermediate servers(performs cacheing, load balancing, encryption and deception).

Each HTTP server is identified over the internet by assigning unique identifier known as Uniform Resource Locator(URL).

Transmission Control Protocol (TCP)

TCP connection teardown

TCP is one of the main communication protocols used over the internet. TCP is a part of Internet Protocol suite (TCP/IP) and complements Internet Protocol (IP).

TCP is a connection-oriented protocol over an IP network i.e. connection needs to be established before sending data. The server must be passive-open to accept connection requests from client. Once connection is established, data transfer takes place and connection is terminated using handshake.

The data received from application needs to be broken down before sending to destination over network. All the packets sent via TCP are guaranteed to reach destination in correct order and without data corruption.

TCP handles data corruption problems by error verification and automatic retransmission. When client receives a packet, the server verifies sequence number(counter to keep track of every bytes sent) and checksum field of the data received. If client detects error, negative acknowledgement is sent back for re-transmission by sender, else positive acknowledgement is sent.

In case of timeout, sender transfers the packet again, but in case of multiple timeouts, the connection is dropped.

Flow control and congestion control are implemented by TCP for traffic control. Flow control tells the sender the maximum speed at which data can be sent to the receiver to avoid loss. This ensures that data isn’t sent when receiver’s buffer has reached capacity. Congestion control limits the flow of packets at each node of the network preventing any node from receiving excessive packets. Congestion occurs because routers and switches have queue buffers that hold the packets for processing.

These controls cause delays and result in more latency compared to UDP making TCP useful for applications that require reliability and are less time critical.

High throughput can be achieved by connection pooling. Connection pool is a cache of open connections that can be reused when future connection are required. Opening a new connection for every user is costly and wastes resources. In connection pool, after a connection is created and used, it is added to the pool to be re-used so that new connection need not be established. This cuts down the time user has to wait to establish a connection.

User Datagram Protocol (UDP)

UDP request-response

User Datagram Protocol is one of the main protocols of Internet Protocol suite. UDP is connection-less protocol i.e. connection doesn’t need to be established before data transfer.

UDP is datagram oriented. If datagrams arrive at destination, they could arrive disorganized or not arrive at all. It is more efficient compared to TCP as communication happens real time. UDP is preferred in scenarios where few packet loss is preferable over increased latency.

Each datagram contains a header and payload. Header contains sufficient information to route packets from source to destination, while payload contains actual data to be transferred.

UDP is suitable for the applications where error checking isn’t necessary or is handled by the application. UDP avoids overhead of such processing.

When is UDP preferred over TCP?

  • Time-sensitive applications prefer UDP because of lowest latency
  • Delayed data is far worse than loss of data/unreliable transfer
  • User wants to implement error correction within application

UDP is preferable for video calls, streaming, VoIP and real-time multiplayer games.

Remote Procedure Call (RPC)

Breakdown of RPC protocol

Remote Procedure call is a protocol where one programs requests to use service of another program located on another computer without having to understand network details.

RPC is a client-server model where the calls are synchronous i.e. requesting program is suspended until response from local procedure is received.

Working of RPC:

  1. When the program that uses remote procedure code is compiled, a stub is generated that is a representative of the remote procedure code.

2. When the program runs and stub is invoked, the params are pushed onto stack like a local procedure call. The procedure id and arguments are marshaled into a request message and message is sent from client to server by OS.

3. The calling environment is suspended, and the request transferred to the environment where the procedure is to executed.

4. The procedure is executed and the results are transferred back to the calling environment. The OS passes the incoming packets to the stub where its un-marshaled and execution proceeds as if it were a local procedure call.

Advantages of RPC:

  • Internal mechanism of RPC is hidden from user
  • Supports process-oriented and thread-oriented models
  • Can work in distributed and local environment

Disadvantages of RPC:

  • Clients is tightly coupled with the server
  • Can be difficult to debug RPC
  • Increase in cost due to additional calls

Representational State Transfer (REST)

REST request and response

REST is an architectural style for building web services that interact via HTTPs protocol. REST is used throughout software industry and is widely known for creating scalable, stateless, cacheable and reliable web APIs. It enforces client-server architecture where client sends an HTTP request while server generates an HTTP response.

Clients can only access resources by using URIs. The server provides a representation of resources and actions that can be done on the resources. The responses are sent in hypertext format so they can be understood by majority of clients. The REST is loosely based on HTTP methods to access resources. The request is sent by adding http method, server url, in request headers and the data is transmitted in JSON, XML document.

Sample rest calls:

POST /payments/authorize

{“user”: “abc”,

“amount”: “10”}

GET /payments/info/{abc}

The goal of REST architecture is to achieve loose coupling between user agent and web server. This is achieved by creating a layer of abstraction on server side which can be done by exposing the functionalities and hiding the implementation details.

Disadvantages of REST:

  • More fields might be added in response with time. Even the clients who don’t need new fields, will still receive new payload resulting in increasing latency and bloated payload size.
  • REST typically relies on few operations -GET, POST, DELETE, PATCH and PUT. This might result in some use case that doesn’t fit these operation.
  • Not all resources can be accessed with REST. Only resources that are naturally organized in simple hierarchy can be accessed.
  • Fetching complicated resources with nested hierarchical might result in multiple rounds between client and server to render single view

Difference between RPC and REST in terms of HTTP calls:

  • RPC only uses GET, POST http calls, while REST uses GET, POST, PUT, DELETE and PATCH.
Difference between RPC and REST calls

Thank you for checking out my blog!

Follow me to get notified about my upcoming blog. My next blog in system design series will be about security.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SAKSHI CHHABRA

SAKSHI CHHABRA

Master's student in Computer Science from University of Florida. I love to write and help others, so here am i.