Sub topic 16
Cookies and Caching
Prerequisites: HTTP request and response structure, status codes, headers, round-trip time (RTT), and basic client-server communication.
HTTP is stateless: each request is independent. A server does not automatically remember earlier requests from the same client. Cookies add a small amount of client-held state. Caching avoids repeating network transfers for data that has not changed.
Cookies: carrying client state
A server creates a cookie by including a Set-Cookie response header. The browser stores the cookie and sends it in later matching requests through the Cookie request header.
HTTP/1.1 200 OK
Set-Cookie: session_id=abc123; Max-Age=3600; Secure; HttpOnly; SameSite=LaxA later request may contain:
GET /account HTTP/1.1
Host: example.com
Cookie: session_id=abc123The session_id value identifies a server-side session. The cookie itself usually contains only an identifier, not the complete session data.
| Attribute | Meaning |
|---|---|
| `Max-Age=3600` | Cookie expires after 3600 seconds |
| `Secure` | Send only over HTTPS |
| `HttpOnly` | Do not expose to browser scripts |
| `SameSite=Lax` | Restrict many cross-site requests |
| `Path=/account` | Send only for matching paths |
Domain controls which hosts can receive a cookie. A cookie without an explicit Domain is generally restricted to the host that created it.
Caching: reusing an HTTP response
A cache stores an earlier response and reuses it when a later request can use the same representation. A browser cache is local. A shared cache, such as a proxy or content delivery network, can serve many clients.
The freshness lifetime determines whether a cached response can be used immediately:
Cache-Control: public, max-age=60For 60 seconds, a cache can use the stored response without contacting the origin server. After it becomes stale, the cache can revalidate it.
A validator lets the server confirm that the cached representation is still current:
HTTP/1.1 200 OK
ETag: "v17"
Cache-Control: max-age=60The next validation request can be:
GET /notes HTTP/1.1
If-None-Match: "v17"If the content is unchanged, the server sends:
HTTP/1.1 304 Not ModifiedThe client keeps using its cached body. A 304 response saves response-body transmission, but the validation still requires a network exchange.
If-Modified-Since and Last-Modified provide a time-based alternative. An ETag is usually more precise because timestamps can have limited resolution.
Cookies and cache safety
Responses containing personalized data should not normally be stored in a shared cache:
Cache-Control: private, no-storeprivate permits browser caching but forbids shared-cache storage. no-store forbids storing the response. no-cache has a different meaning: storage is allowed, but reuse requires validation.
If a response varies according to a request header, the response can identify that dependency:
Vary: Accept-EncodingFor cookie-dependent content, careless shared caching can return one user's response to another user. A server must use suitable cache controls or carefully define the cache key.
A cache decision model
def cache_action(age, max_age, etag, current_etag):
if age < max_age:
return "use cached response"
if etag == current_etag:
return "send conditional request; use body after 304"
return "download a new response"
print(cache_action(age=80, max_age=60, etag='"v17"', current_etag='"v17"'))The output is:
send conditional request; use body after 304Exercise
A cached response has max-age=30 seconds and is 45 seconds old. It has ETag "a7", and the server's current ETag is also "a7". What happens, and what does the server send if the representation is unchanged?
Show answer
The response is stale, so the client sends a conditional request with If-None-Match: "a7". The server sends 304 Not Modified, and the client reuses the cached body.