Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Modern Web Protocols
CN

Modern Web Protocols

Going beyond simple requests: Master WebSockets, HTTP Methods, and the concept of Statelessness.

HTTP Methods (Verbs)

MethodPurposeIdempotentSafeBody
GETRetrieve resourceYesYesNo
POSTCreate resourceNoNoYes
PUTReplace entire resourceYesNoYes
PATCHPartial updateNoNoYes
DELETERemove resourceYesNoOptional

Idempotent: multiple identical requests produce the same result. Safe: does not modify the resource (read-only).

Stateless vs Stateful

HTTP is stateless — the server does not retain client information between requests. Each request is independent.

StatelessStateful
Server memoryNone per clientStores session data
ScalabilityTrivial (any server handles any request)Must route client to same server
ExamplesHTTP, RESTFTP, TCP connections

Statelessness is what makes the web scalable. Load balancers can distribute requests to any server in a pool.

WebSockets

WebSocket provides a full-duplex persistent connection over a single TCP socket after an HTTP upgrade handshake.

Client                                 Server
  │── HTTP GET (Upgrade: websocket) ──→
  │←─ 101 Switching Protocols ──────────
  │── bidirectional frames ────────────→
  │←───────────────────────────────────
FeatureHTTPWebSocket
DirectionHalf-duplex (request-response)Full-duplex
OverheadHeaders per request (500-800 bytes)2-6 bytes per frame
PersistenceConnection closed after responseConnection stays open
Use caseREST APIs, web pagesReal-time apps, chat, games

Statelessness in Practice

Modern web apps simulate state using:

  • Cookies: server sends a token (session ID), client sends it back
  • JWT (JSON Web Tokens): self-contained tokens with user info and expiry
  • Client-side storage: localStorage, sessionStorage

These shift state management to the client while keeping the server stateless.

Q: Why use WebSockets instead of frequent HTTP requests?

A: Efficiency. HTTP polling requires a new TCP/TLS handshake and large headers each time. WebSockets upgrade once, then exchange tiny frames bidirectionally. For real-time apps (chat, trading, games), WebSockets are far more efficient.

Q: What is the difference between PUT and PATCH?

A: PUT replaces the entire resource. PATCH updates only the specified fields. Sending {"name": "Alice"} via PUT would delete all other fields; via PATCH only updates the name.

Q: Why is HTTP stateless?

A: Scalability. Stateless servers don’t need to remember clients, so any server can handle any request. This makes horizontal scaling trivial — just add more servers behind a load balancer.

Q: How do web apps maintain session if HTTP is stateless?

A: The server sends a signed token (session cookie or JWT) that the client includes with every request. The server validates the token on each request but doesn’t store session data — this keeps the server stateless.

My Private Notes

Notes are auto-saved locally to this device.