HTTP in Networking

Shubham AgrawalShubham Agrawal
9 min read

It is necessary to understand a few concepts before diving deep into what HTTP is.

What it means by the phrase “concurrent requests to the server”?

The term concurrent requests to the server can refer to either multiple requests from a single client or multiple requests from different clients, depending on the context.

Often, in real-world, both scenarios occur simultaneously:
A server might handle concurrent requests from multiple clients, and each client could itself be making concurrent requests (e.g., multiple resources for their page).

For example, on a large e-commerce platform:
- Single client: A user loads a product page with multiple images, CSS, and JavaScript (concurrent requests by a single client).
- Multiple clients: Thousands of users browse the website at the same time (concurrent requests by multiple clients).

So, to handle real-world scenario

  • Servers need to be optimized for scalability to handle both types of concurrency efficiently.

  • Protocols like HTTP/2 and HTTP/3 focus on making single-client concurrency more efficient with multiplexing.

  • Load balancers, thread management, and asynchronous programming handle multi-client concurrency.

What is a persistent connection?

A persistent connection in HTTP refers to a single network connection that remains open for subsequent requests and responses between a client and a server, rather than closing after each individual exchange (non-persistent connection).

Keep-Alive Timeout

Persistent connections are not kept open indefinitely. They have a timeout period:

  • If no activity occurs during this period, the server closes the connection to free up resources.

  • The timeout can be configured by the server (e.g., Keep-Alive: timeout=5 for a 5-second timeout).

Advantages of a persistent connection

  1. Reduced Latency:- Avoids the time needed for setting up new connection for every request.

  2. Improved Resource Utilization:- Fewer connections mean reduced load on the server and network.

  3. Better Performance for Complex Pages:- A webpage can fetch multiple resources (images, scripts, stylesheets) efficiently over the same connection.

  4. Reduced Network Congestion:- By reusing connections, fewer simultaneous connections are needed, reducing congestion.

HTTP Versions and Persistent Connections

  • HTTP/1.0:
    — Connections are non-persistent by default.
    — Persistent connections are optional and enabled with the Connection: keep-alive header.

  • HTTP/1.1:
    Persistent connections are enabled by default.
    — Connections are closed explicitly with the Connection: close header.

  • HTTP/2:
    — Always uses persistent connections with multiplexing, allowing multiple streams over a single connection.

  • HTTP/3:
    — Uses persistent connections based on QUIC (UDP), further optimizing latency and reliability.

What is multiplexing?

Multiplexing, in telecommunications, is a technique used to combine and send the multiple data streams over a single medium. The process of combining the data streams is known as multiplexing and the device used for multiplexing is known as a multiplexer (MUX).

Why Multiplexing?

  • The transmission medium is used to send the signal from sender to receiver. The medium can only have one signal at a time.

  • If there are multiple signals to share one medium, then the medium must be divided in such a way that each signal is given some portion of the available bandwidth. For example: If there are 10 signals and bandwidth of medium is 100 units, then the 10 units is shared by each signal.

  • When multiple signals share the common medium, there is a possibility of collision. Multiplexing is used to avoid such collision.

Multiplexing in context of HTTP/2 and HTTP/3

It refers to the ability to send concurrent requests and receive concurrent responses simultaneously over a single connection.

👉 How multiplexing works?

  • Unlike HTTP/1.1, which often opens multiple TCP connections to handle concurrent requests, HTTP/2 and HTTP/3 use a single connection to send and receive all data.

  • Each HTTP request/response pair is assigned a unique stream identifier within the single connection.

  • Streams are independent and can carry data concurrently without interfering with each other.

👉 Benefits of Multiplexing

  1. Eliminates Head-of-Line Blocking (in HTTP/2):- In HTTP/1.1, if one request in a connection was delayed, subsequent requests would also be delayed (head-of-line blocking). Multiplexing ensures that streams are independent, so a delay in one does not block others.

  2. Better Utilization of Resources (Efficient Use of Bandwidth):- With multiplexing, the single connection remains open and active, reducing the overhead of repeatedly opening and closing connections.

  3. Reduced Network Congestion

  4. Reduced Latency:- Multiple requests are processed in parallel, speeding up page load times.

👉 Examples without multiplexing and with multiplexing

Without Multiplexing (HTTP/1.1):

  • Request A starts → Completes → Request B starts → Completes.

  • If Request A is delayed, Request B cannot proceed (Head-of-Line blocking).

With Multiplexing (HTTP/2 or HTTP/3):

  • Request A and Request B start simultaneously.

  • Request A faces a delay, but Request B completes without waiting for A.


Now, let's dive deeper into what HTTP is...

What is HTTP?

HTTP (Hypertext Transfer Protocol) is an application layer protocol used for communication between web clients (like browsers) and web servers. It is a stateless protocol, that means, HTTP does not retain information about previous interactions. Each request is independent.

Different HTTP versions

HTTP Methods

HTTP defines methods for the type of action to perform

  • GET: Retrieve data from a server (e.g., a webpage).

  • POST: Send data to the server (e.g., form submissions).

  • PUT: Update existing resources.

  • DELETE: Remove a resource.

  • HEAD: Retrieve metadata about a resource.

  • OPTIONS: Discover available HTTP methods for a resource.

Idempotency in HTTP

It refers to the property of an HTTP method where making multiple identical requests produces the same effect as making a single request. This means that the state of the server remains consistent regardless of how many times the request is repeated.

HTTP Status Codes

The server response includes a status code indicating the outcome of the request:

  • 2xx: Success (e.g., 200 OK)

  • 3xx: Redirection (e.g., 301 Moved Permanently)

  • 4xx: Client errors (e.g., 404 Not Found)

  • 5xx: Server errors (e.g., 500 Internal Server Error)

Secure HTTP (HTTPS)

  • When combined with SSL/TLS encryption, HTTP becomes HTTPS.

  • This ensures secure data transfer between client and server.

HTTP Request and Response Components

👉 HTTP Request Components

An HTTP request consists of several components that work together to convey the client’s intentions to the server.

  1. Request Line: —
    The request line specifies the HTTP method, the resource being requested (URL), and the HTTP version.

  2. Headers: —

    Headers provide additional information about the request, such as the type of data being sent or accepted, authorization details, and more.

  3. Blank Line: —

    A blank line separates the headers from the body. If there is no body, the blank line still signals the end of the headers.

  4. Body (Optional): —
    The body carries data sent to the server, usually with methods like POST or PUT. The body is not used in methods like GET or DELETE, which focus on retrieving or deleting resources.

👉 HTTP Response Components

  1. Status Line: —
    specifies protocol version, status code, and status message.

  2. Headers: —

    Headers provide metadata about the response, such as the type and length of the content, caching instructions and much more.

  3. Blank Line: —

    A blank line separates the headers from the body. If there is no body, the blank line still signals the end of the headers.

  4. Body (Optional): —

    The body contains the data requested by the client, such as an HTML page, JSON data, an image, etc. The content of the body depends on the Content-Type header.

  5. Response Trailer (Optional): —

    For chunked responses (HTTP/1.1) or specific uses in HTTP/2/3, additional metadata can be sent after the body using trailers.

Communication techniques based on HTTP

There are three communication techniques viz. Short Polling, Long Polling and Server Sent Events (SSEs) in client server architecture based on HTTP that are used to check for updates or new data from the server.

👉 Short Polling

What is it?

It is a technique to check for updates or new data at regular intervals. In this approach, the client repeatedly sends HTTP requests to the server at predefined intervals, regardless of whether the server has new information to return.

Working of Short Polling

  1. Client Request: The client sends an HTTP request to the server to check for updates.

  2. Server Response: The server processes the request and sends a response. If new data is available, it is returned in the response. If no new data is available, the server sends an empty or “no new data” response.

  3. Repeat: After a specific interval (e.g., every 5 seconds), the client sends another request to the server, repeating the process.

Advantages and Disadvantages

When to use Short Polling?

Short polling is often used in simple applications or scenarios where:

  • Real-time updates are not critical.

  • Long-term scalability is not a primary concern.

  • The server cannot support more advanced techniques like WebSockets or server-sent events.

Real World Examples

  • Checking for new emails or notifications in low-traffic systems.

  • Fetching order status in an e-commerce app.

👉 Long Polling

What is it?

Unlike short polling, where the client repeatedly sends requests at fixed intervals, long polling involves the client sending a request that the server holds open until new data or an event is available.

Working of Long Polling

  1. Client Request: The client sends an HTTP request (usually GET) to the server to check for updates or new data.

  2. Server Processing: The server does not respond immediately if no new data is available. Instead, it keeps the connection open (often for a defined timeout period) and waits for new data or an event.

  3. Server Response: If new data becomes available before the timeout, the server sends the data back to the client in the response. If the timeout is reached without new data, the server sends a “no new data” response.

  4. Repeat: After receiving a response, the client immediately sends another request to the server, repeating the process.

Advantages and Disadvantages

Real World Examples

  • Chat Applications

  • Notification Systems

  • Real-Time Dashboards

  • Order Tracking

👉 Server Sent Events (SSEs)

What is it?

SSE allows a server to push real-time updates to a client over an HTTP connection. SSE is unidirectional, meaning only the server can send data to the client. It is based on the HTTP protocol and uses a simple text/event-stream format for transmitting data.

Working of Server-Sent Events

  1. The client establishes a connection to the server using an HTTP GET request. The request includes a special Accept: text/event-stream header to indicate that the client expects a stream of events.

  2. The server keeps the connection open and continuously sends updates to the client as new data becomes available.

  3. The server sends updates in a specific format called event stream, with each message consisting of one or more fields (e.g., data, id, event).

  4. If the connection is closed (e.g., due to network issues), the client automatically tries to reconnect to the server.

Advantages and Disadvantages

Real World Examples

  • Live Notifications

  • News Feeds

  • Stock Price Updates

  • Server Monitoring

  • IoT Device Updates


Thank you for your time! 😊

Connect with me on LinkedIn

0
Subscribe to my newsletter

Read articles from Shubham Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubham Agrawal
Shubham Agrawal