Client-Server Communication Protocols
Explore fundamental client-server communication protocols crucial for frontend system design. Understand how HTTP facilitates stateless interactions, RPC enables remote procedure calls, and WebSockets support real-time, full-duplex communication. This lesson helps you grasp protocol roles, message structures, and use cases to optimize frontend-backend interactions.
The internet is a global network of interconnected computers that enables communication and data exchange using standardized protocols like TCP/IP. The World Wide Web (WWW) is a system of interlinked documents and resources accessed via the internet using protocols like HTTP and HTTPS. The interconnected computers over the WWW are broadly divided into two categories–clients and servers. The clients request resources from the server, and the server entertains the requests based on certain conditions. This communication between the client and server is carried out with the help of an
We’ll start with the HTTP protocol, which is the primary use case in most applications. The other two (RPC and WebSockets) are used extensively in different use cases. We believe these three collectively cover the major concepts of client-server communications.
Web protocols
In frontend System Design, well-defined web protocols ensure seamless
Web protocols operate over TCP/IP, where a user clicks a link on a web page, and the browser uses DNS to resolve the domain name to its corresponding IP address before connecting to the server. It then establishes a TCP connection, initiates a TLS handshake (if using HTTPS), and requests the web page and any related resource. Finally, the browser renders the page, closing the TCP connections. This information flow between clients and servers is facilitated by a hypertext transfer protocol (HTTP) protocol, as shown below:
Application-layer protocols such as the file transfer protocol (FTP), simple mail transport protocol (SMTP), HTTP, and so on use lower-layer protocols like transmission control protocol (TCP), user datagram protocol (UDP), and internet protocol (IP) to provide services to end users.
Let’s explore how HTTP protocol works and enables client and server communication.
Hypertext transfer protocol (HTTP)
The hypertext transfer protocol (HTTP) is a
HTTP is based on request and response, where one program (the client) requests another program (the server), and the server returns a response. The client usually makes requests in the form of HTTP commands, and the server responds with data in the form of documents. These commands are structured by different API architecture styles discussed in the next lesson.
HTTP methods
The HTTP protocol is packed with several methods that clients use to request an action from the server. These methods are discussed in the following table:
Common HTTP Methods and their Purpose
HTTP Methods | Purpose and Features | Status Codes |
|
| 200 OK, 404 Not Found, 304 Not Modified |
|
| 201 Created, 400 Bad Request, 409 Conflict |
|
| 200 OK, 204 No Content, 404 Not Found |
|
| 200 OK, 204 No Content, 404 Not Found |
|
| 200 OK |
|
| 200 OK (successful tunnel), 403 Forbidden (blocked) |
|
| 200 OK (if methods are supported), 405 Method Not Allowed (if not supported) |
|
| 200 OK (if resource exists), 404 Not Found (if not found), 403 Forbidden (if access denied) |
After gaining a basic understanding of HTTP methods, let’s look at how an HTTP message flows between client and server using one of these methods.
HTTP message flow
HTTP communication is initiated by the clients sending a request to the server, and as long as the request conforms to HTTP standards, the server will respond with the requested resources, regardless of the type of client:
HTTP request structure
An HTTP request message is composed of the following four components:
Method: HTTP provides built-in methods that determine what action a client wants to request/perform.
The request-target and version: It can be a URL or URI, a unique address or identifier that uniquely determines a resource’s location over the internet. For example, the URL https://www.educative.io/courses/grokking-modern-system-design-interview uniquely identifies an Educative course titled Grokking Modern System Design Interview. It also shows which version of HTTP is being used. The version part contains the HTTP version used for the request, for example,
HTTP/1.1orHTTP/2.0.Headers: HTTP headers allow clients to pass additional information between the communicating entities. The request headers mainly contain the following information:
Server information from where data is requested.
Information about the browser being used by the user.
The kind of data formats a client can accept.
Body: The body part of the HTTP request is used to communicate any application data a client wants to communicate to the server.
HTTP response structure
The HTTP response has the same format as the request with the following modifications:
The method in the request is replaced with the HTTP version in response.
The URL in the request is replaced with a status code in response.
The HTTP version in the request is replaced with the phrase in response.
A sample HTTP request and response is shown below, including all the parts:
Note: HTTP is a stateless protocol used for communication between clients and servers, but it transmits data in plain text. HTTPS enhances security by encrypting data using
, ensuring secure and private communication. TLS/SSL Transport layer security/Secure socket layer
Which parts of the request and response messages are encrypted while using HTTPS?
But as we all know, HTTP has evolved and comes with different versions, i.e., HTTP/1.1, HTTP/2.0, etc. The following table summarizes the differences between different versions of HTTP:
Comparison of Different Versions of HTTP
Version | Methods Supported | Support for Headers | Connection Nature | Other |
HTTP/0.9 |
| No | Terminates immediately after the response. |
|
HTTP/1.0 |
| Yes | Terminates immediately after the response. |
|
HTTP/1.1 |
| Yes | Persistent |
|
HTTP/2.0 |
| Yes | Persistent |
|
HTTP/3.0 |
| Yes | Persistent |
|
Remote procedure call (RPC)
A remote procedure call (RPC) architecture is a service or action-oriented style of creating APIs, where resources are distributed among different services running on remote machines. RPC facilitates interprocess communication by enabling clients to run remote procedures (functions) in a simple local
The table below elaborates the individual components of a remote procedure call:
Components | Description | |
Client stub |
| |
Runtime RPC | IDL |
|
IDL compiler |
| |
IDL header |
| |
Server stub |
| |
The RPC runtime is the actual software program that does the heavy lifting and glues all the components together. It communicates with the underlying operating system and network interface to transfer data over the wire.
How does RPC work?
RPC follows a request-response model. The client initiates a call, and data is packed and forwarded to the RPC runtime. A network connection is established to transmit the request to the remote server. The server processes the request and sends back the response through the same process.
Request and response
Before we discuss the request initialization, we must understand the two IDL header types.
Import module: A component added to the client side, specifying a list of functions it can call.
Export module: A component added to the server side, specifying a list of functions it can perform.
The import and export modules defined in the IDL headers maintain a list of remote functions provided by the API, along with the format of input parameters and returned responses. A client initiates an RPC request, which must specify a function identifier and parameter list consistent with the signatures provided by the IDL import module.
Let’s look at a hypothetical example of an RPC available at the example.com/rpc-demo endpoint. We’ll call the demo RPC that accepts a single string parameter of greeting. The request and response headers sent and received by a JSON-RPC client are as follows:
Request
The HTTP post request headers for the JSON-RPC service are as follows:
POST example.com/rpc-demo HTTP/1.1
Content-Type: application/json
Content-Length: xxx
{
"jsonrpc": "2.0",
"method": "demo",
"params": ["greeting": "Hi"],
"id": "99"
}
Response
For a successful API call, the returned HTTP response headers look like this:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: xxx
{
"jsonrpc": "2.0",
"id": "99" ,
"result": "Hello!"
}
Note: For simplicity, we’ve removed unnecessary fields from the request and response headers given above. Also, we’ll learn more about data formats, for example, JSON, in the coming lessons.
How does the server know that a particular request is intended for it?
WebSockets
WebSocket was introduced in 2011 to enable
WebSocket leverages the core TCP channel utilizing its full-duplex nature. Data can be sent and received simultaneously by the client and the server. Websocket is a
Note: The WebSocket protocol is detailed in IETF's
, whereas its API documentation maintained by W3C is available RFC 6455 Fette, I., and A. Melnikov. 2011. “The WebSocket Protocol.” RFC. https://doi.org/10.17487/rfc6455. . here “WebSockets Standard.” n.d. Websockets.spec.whatwg.org. Accessed March 31, 2023. https://websockets.spec.whatwg.org//#the-websocket-interface.
How does it work?
A WebSocket connection starts with an HTTP connection established through a three-way TCP handshake. Afterward, an HTTP GET request is initiated to switch the protocol to WebSocket. The connection upgrade request can be accepted or rejected by the HTTP server. If the server is compatible with the WebSocket protocol and the upgrade request is valid, the connection is upgraded to a WebSocket connection. The response contains the status code 101 (Switching Protocols) and the value for the field, Sec-WebSocket-Accept.
HTTP Upgrade headers
The headers of the initial switching protocol request are shown below:
The Upgrade request
GET / HTTP/1.1
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Key: eB9AWsQe8+SDcwWRjpGSow==
sec-websocket-version: 13
The Upgrade response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: zglqWZt8l79gwpiFBgKihrobE8I=
Note: For simplicity, we have removed some fields from the headers above.
The headers above contain the following noticeable fields:
Status code
101shows that the protocol is successfully upgraded and can send WebSocket .frames The number of bytes, defined and used in different network protocols, is the unit of transmission. The
Sec-WebSocket-Keyis a -encoded 16-byte value that the server uses to verify that the upgrade request is coming from a legitimate client that understands the WebSocket protocol, and not a malformed HTTP request. This value is then encrypted using a hashing algorithm like SHA, MD5, and so on.base64 A binary encoding scheme. The server decrypts the value of the
Sec-WebSocket-Keyand generates theSec-WebSocket-Acceptfield by prepending a Globally Unique Identifier (GUID) value to the client-providedSec-WebSocket-Key. The value ofSec-WebSocket-Acceptis also encrypted and sent back to the client, indicating that the server has accepted the connection upgrade.
Initial control frames are exchanged once the connection is established and successfully upgraded to WebSocket protocol. WebSocket has two types of frames: control and data frames, identified by a 4-bit opcode
Why is it difficult to scale WebSockets horizontally?
After discussing different communication protocols, let’s recap the findings. The following table summarizes and compares the communication protocols against different features:
Feature | HTTP | RPC | WebSockets |
Communication type | Request-Response (Stateless) | Request-Response (Can be Stateful) | Full-Duplex (Bidirectional) |
Transport protocol | HTTP (Mostly over TCP) | TCP (Uses HTTP/2 in gRPC) | TCP |
Message format | Text (JSON, HTML, XML) | Binary (Protobuf, Thrift, etc.) | Binary or Text (JSON, Protobuf, etc.) |
Performance | Moderate | High (Optimized for speed) | High (Persistent connection) |
Use cases | Request-response interactions | Service-service communication, Remote Procedure Calls | Real-time applications, Chat, Live Streaming |
Connections | Opens and closes per request | Can maintain persistent connections | Persistent conncetion |
Overhead | High (Headers in every request) | Lower than HTTP (Binary format, efficient encoding) | Low (Single connection, minimal overhead) |
This lesson discussed HTTP, RPC, and WebSockets, comparing their communication models, performance, and use cases. Each protocol serves different needs, HTTP for standard web requests, RPC for efficient service-to-service communication, and WebSockets for real-time interactions, allowing us to choose the best fit based on our use case.