Why HTTP is stateless
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A ShopLite customer logs in, adds a notebook to her cart and opens the checkout page. The server must still know who she is, yet HTTP treats every request like a stranger. How can the app remember her?
Stateless: every request stands alone
Stateless means each request carries everything the server needs, and the server forgets it after answering. HTTP works this way: two requests are never linked by the protocol.
Send one request to the demo server and look for anything in it that says who is asking.
Example 1 · One request, as the server sees it
Runs in: the demo login server on localhost:8080
curl -v http://localhost:8080/products/1* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET /products/1 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.8.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 49
< Connection: close
< Date: Fri, 16 Oct 2026 02:38:34 GMT
<
{"id":1,"name":"Java notebook","price":"120.00"}
* Closing connectionWhat this code does
- Lines starting with
>are the whole request: method, path, version,Host,User-AgentandAccept. Nothing in them says who is asking or what they asked before. - Lines starting with
<are the response, then the JSON body. Once it is sent, the server keeps nothing about this request (apart from a log line, if it writes one). Ask again and it does the full work again.
Two requests from the same laptop, one second apart, are strangers to the server. The server sees a method, a path, some headers and maybe a body, and nothing links the second request to the first.
localhost:8080; the outputs are real runs. Any backend language can build such a server, and later sections of the course build a real one. On Windows PowerShell, type curl.exe instead of curl.Why stateless is good: simple servers, easy scaling
A load balancer is a program in front of several copies of a server. It receives every request and passes it to one copy, usually taking turns.
Send the same request three times through a load balancer that stands in front of two server copies.
Example 2 · Three requests, two servers, one answer
Runs in: a demo load balancer on localhost:8000 in front of two copies of the demo server
curl -i http://localhost:8000/products/1curl -i http://localhost:8000/products/1curl -i http://localhost:8000/products/1$ curl -i http://localhost:8000/products/1
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 49
X-Served-By: server-a
Connection: close
Date: Fri, 16 Oct 2026 02:38:35 GMT
{"id":1,"name":"Java notebook","price":"120.00"}
$ curl -i http://localhost:8000/products/1
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 49
X-Served-By: server-b
Connection: close
Date: Fri, 16 Oct 2026 02:38:36 GMT
{"id":1,"name":"Java notebook","price":"120.00"}
$ curl -i http://localhost:8000/products/1
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 49
X-Served-By: server-a
Connection: close
Date: Fri, 16 Oct 2026 02:38:37 GMT
{"id":1,"name":"Java notebook","price":"120.00"}What this code does
- The load balancer on port 8000 passes requests to two copies of the server in turn.
X-Served-Byis a custom header the demo adds so you can see which copy answered. - Both copies give exactly the same body, because the answer depends only on the request. That is why copies can be added or removed freely.
Because the server remembers nothing, any copy can answer any request. This is called scaling out: when traffic grows, you start more copies.
A copy can crash or restart and users do not notice, because it held nothing the other copies need. A server with no memory is also simpler to write and to test.
The problem: logins and carts need memory
State is remembered information, such as "Asha is logged in" or "2 items in the cart". Real apps must keep state between requests, but plain HTTP does not.
Log in, then ask /me who you are, using plain curl for both requests.
Example 3 · Logged in, then forgotten
Runs in: the demo login server on localhost:8080
curl -i -d "username=asha" -d "password=campus123" http://localhost:8080/logincurl -i http://localhost:8080/me$ curl -i -d "username=asha" -d "password=campus123" http://localhost:8080/login
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 28
Connection: close
Date: Fri, 16 Oct 2026 02:38:37 GMT
Set-Cookie: SID=b7f7eaee9a3314f275531bc12ac4723a; Path=/; HttpOnly; Secure; SameSite=Lax
{"message":"Welcome, asha"}
$ curl -i http://localhost:8080/me
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Content-Length: 26
Connection: close
Date: Fri, 16 Oct 2026 02:38:38 GMT
{"error":"Not logged in"}What this code does
- The login works:
200 OKandWelcome, asha. The response also carries aSet-Cookieheader, which is the server offering to remember this login. - curl keeps no cookies unless you ask it to, so the second request carries nothing that links it to the login. To the server it is a stranger, and
/meanswers401 Unauthorized.
A login is just one request and one response. The next request arrives with no sign that it comes from the same person, unless the client adds one. The same is true for a cart: items added during a sale must still be there on the checkout page.
Predict the output
Is the user remembered?
The login always succeeds. Pick what the last command prints.
Round 1 of 3
The cookie jar is saved at login and sent on the second request.
Three answers: cookies, server-side sessions and tokens
A cookie is a small name=value pair that the server sets and the client sends back on every later request to that site.
Run the same login again, this time saving the cookie in a cookie jar file and sending it back.
Example 4 · The same login, now with a cookie jar
Runs in: the demo login server on localhost:8080
curl -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/logincurl -b cookies.txt http://localhost:8080/mecat cookies.txt$ curl -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/login
{"message":"Welcome, asha"}
$ curl -b cookies.txt http://localhost:8080/me
{"user":"asha","cartItems":0}
$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_localhost FALSE / TRUE 0 SID 6230737d0867a87a99b702330cc1b28bWhat this code does
-c cookies.txttells curl to save the cookies from the response into a file, called a cookie jar.-b cookies.txtsends them back on the next request, the way a browser does.- This time
/meknows Asha. The jar holds one cookie,SID, whose value is a random session id; the Cookies lesson explains every column.
Since the protocol will not remember, the client sends a reminder with every request. Three common ways to do it, each covered in this module:
- Cookies: the server sends a
Set-Cookieheader, and the browser stores the pair and returns it in aCookieheader. - Server-side sessions: the cookie holds only a random session id, and the real data (who you are, your cart) stays on the server.
- Tokens: after login the server hands out a signed string, a token, which the client sends in an
Authorization: Bearer ...header. Mobile apps and APIs use this a lot.
Find the bug
Find the faulty curl command
The login works, but /me always answers 401. Click the faulty line.
Which line stops the cookie from being saved?
Round 1 of 2Where state can live: client, server memory, database or cache
Reuse the saved cookie after the demo server restarts and see what the old session id points to.
Example 5 · A restart wipes sessions kept in memory
Runs in: the demo login server on localhost:8080
# cookies.txt still holds the SID from Example 4.# The demo server keeps sessions in memory, and it was just restarted.curl -i -b cookies.txt http://localhost:8080/meHTTP/1.1 401 Unauthorized
Content-Type: application/json
Content-Length: 26
Connection: close
Date: Fri, 16 Oct 2026 02:38:43 GMT
{"error":"Not logged in"}What this code does
- curl sends the same cookie as before. But the session it points to lived in the memory of the old server process, which ended at the restart, so the id now points to nothing.
- Every logged-in user is logged out by one restart. Apps that run on several servers, or restart often, keep sessions in a database or a shared cache instead.
Where remembered data lives decides what survives a restart and what every server copy can see:
- Client: inside a cookie or token that the client sends back. Every copy can read it, but the user can see it, so it must be small and either harmless or signed.
- Server memory: fast and simple, but lost when the server restarts, and other copies cannot see it.
- Database: survives restarts and is shared by every copy, but each lookup is slower.
- Cache: a fast shared store kept in memory, such as Redis, made for short-lived data like sessions.
Next step
Check your understanding
11 quick questions