DEV Community

Cover image for What Is HTTP? Why Is It Stateless? So how does login work? | Deeper Dive into HTTP #1
Ashay Tiwari
Ashay Tiwari

Posted on

What Is HTTP? Why Is It Stateless? So how does login work? | Deeper Dive into HTTP #1

Have you ever wondered how your browser communicates with a backend server? Or how a website remembers that you're logged in, even though HTTP is a stateless protocol?

These questions might seem simple, but understanding them gives you a much stronger foundation for building web applications, debugging APIs, and understanding what happens behind the scenes when you interact with a website.

In this first article of the Deeper Dive into HTTP series, we'll explore three fundamental questions:

  • What exactly is HTTP?
  • Why is HTTP considered stateless?
  • If HTTP is stateless, how does login work?

Let's understand them quickly.


What Is HTTP?

HTTP stands for Hypertext Transfer Protocol. It is an application-layer protocol that defines how clients and servers communicate by exchanging messages.

HTTP

Whenever you open a website, fetch data from an API, submit a form, or load resources for a web application, HTTP is likely involved in that communication.

Consider a typical web application:

  • Client: Your browser or mobile application.
  • Server: A backend application that processes requests and returns responses.
  • HTTP: The protocol that defines how those requests and responses are structured and exchanged.

For example, imagine opening a user profile page.

Your browser might send a request like this:

GET /api/users/123 HTTP/1.1
Host: example.com
Accept: application/json
Enter fullscreen mode Exit fullscreen mode

This request tells the server that the client wants to retrieve information about a user.

The server might respond with:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": "123",
  "name": "Ashay"
}
Enter fullscreen mode Exit fullscreen mode

Here, GET is the HTTP method, /api/users/123 is the request target, and 200 OK is the status code indicating success.

The response contains a status code, headers, and a body with the requested data.

This is the fundamental communication pattern:

Client → HTTP Request → Server → HTTP Response → Client
Enter fullscreen mode Exit fullscreen mode

HTTP defines the format and structure of this exchange. It doesn't, by itself, define how your application stores users, queries a database, or implements its business logic.

The application and its infrastructure handle those responsibilities.


What Does It Mean That HTTP Is Stateless?

Let's start with a simple example.

HTTP Statless

Imagine sending the following request to a server:

GET /home HTTP/1.1
Host: example.com
Enter fullscreen mode Exit fullscreen mode

The server processes the request and returns:

HTTP/1.1 200 OK
Content-Type: text/html
Enter fullscreen mode Exit fullscreen mode

Now, suppose you send another request to a server:

GET /profile HTTP/1.1
Host: example.com
Enter fullscreen mode Exit fullscreen mode

The server processes the request and returns:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": "123",
  "name": "Ashay"
}
Enter fullscreen mode Exit fullscreen mode

From the perspective of HTTP's request semantics, the second request does not automatically carry the server's memory of the first request.

The server doesn't inherently know whether this is your first request, your tenth request, or a request made by someone who visited the website yesterday.

That's the central idea behind HTTP statelessness.

HTTP is designed so that each request can be understood independently, without requiring the server to remember the context of previous requests.


Why Was HTTP Designed This Way?

Statelessness offers several advantages.

1. Simplicity

Each request can be handled independently.

The server doesn't have to maintain the complete history of every client request just because HTTP exists.

2. Scalability

Imagine you have millions of users.

If every request depended on a particular server remembering the user's previous requests, scaling your application could become more complicated.

With stateless requests, different servers can handle different requests as long as the required state is available through other mechanisms.

3. Reliability

Because requests aren't inherently tied to one server's HTTP-level memory, the system can be designed to distribute requests across multiple servers.


But Then... How Does Login Work?

Let's say you open a website and enter:

Username: ashay
Password: ********
Enter fullscreen mode Exit fullscreen mode

Your browser sends a login request:

POST /login
Enter fullscreen mode Exit fullscreen mode

The server verifies your credentials.

If they're correct, the server needs some way to identify you on future requests.

One common approach is a session.

The server creates a session and gives the browser a session identifier.

That identifier can be stored in a cookie.

For example:

Session ID → abc123
Enter fullscreen mode Exit fullscreen mode

Then later, when your browser requests:

GET /profile
Enter fullscreen mode Exit fullscreen mode

the browser sends the cookie along with the request.

The server can use that session identifier to determine which authenticated session the request belongs to.

So the server can say:

"This request belongs to the authenticated session associated with this session ID."

And that's how it can recognize you across multiple requests.

But Wait...
Does this mean HTTP is no longer stateless?

No.

This is a very important distinction.

HTTP itself is still stateless.

We're simply using an additional mechanism, such as cookies and server-side sessions, to carry authentication state between requests.


Point to remember

HTTP is stateless.

Authentication state is managed on top of HTTP.

Understanding this distinction is an important step toward understanding web applications beyond the code we write every day.

And this is just the beginning of our Deeper Dive into HTTP series.

In the next article, we'll explore HTTP requests, responses, and headers in more detail, including what these messages actually look like and how to interpret them when working with APIs.

If you found this useful, follow AshayCodes - Instagram and YouTube for more practical explanations of the technologies we use every day.

Happy learning!

Top comments (0)