Today's Learning Notes
October 20, 2026 · Networking Fundamentals & DSA
1. How a browser connects to an HTTPS website
When you open https://example.com, the flow is approximately:
1. DNS Resolution
Find the server's IP address
2. TCP Handshake
Establish a reliable connection
3. TLS Handshake
Establish encryption and authenticate the server
4. HTTP Request
GET, POST, OPTIONS, etc.
5. HTTP Response
HTML, JSON, images, or other data
Note: This describes the common HTTPS-over-TCP flow using HTTP/1.1 or HTTP/2. HTTP/3 uses QUIC over UDP and integrates TLS differently.
TCP vs. TLS
| Concept | Full form | Purpose |
|---|---|---|
| TCP | Transmission Control Protocol | Reliable, ordered delivery of bytes |
| TLS | Transport Layer Security | Encryption, integrity, and authentication |
Think of it this way:
- TCP: Establishes reliable communication.
- TLS: Makes that communication secure.
The TCP three-way handshake is:
- Client → Server:
SYN - Server → Client:
SYN-ACK - Client → Server:
ACK
After TCP connects, TLS negotiates security parameters and establishes keys for encrypted communication.
2. DNS and the recursive resolver
DNS translates domain names into IP addresses.
For example, your browser needs the IP address associated with google.com.
Your browser / OS cache
Is the IP already known?
Recursive DNS resolver
Does the lookup work and cache the result
Root DNS server
Points toward the .com DNS servers
TLD DNS server (.com)
Points toward the authoritative DNS
Authoritative DNS server
Returns the domain's DNS record
The resolver returns the result to your computer, which can cache it according to its TTL (Time to Live).
Important distinction:
- Recursive resolver: Does the lookup work on behalf of your computer.
- Root server: Directs the resolver to the appropriate top-level domain servers.
- Authoritative server: Provides the DNS records for the domain.
The resolver may skip some steps if it already has a cached result.
3. HTTP methods and CORS preflight
HTTP methods describe the action a client wants to perform.
| Method | Typical purpose |
|---|---|
GET | Retrieve data |
POST | Submit data or create a resource |
PUT | Replace a resource |
PATCH | Partially update a resource |
DELETE | Delete a resource |
OPTIONS | Ask which communication options are supported |
What is a CORS preflight request?
CORS stands for Cross-Origin Resource Sharing.
Suppose your frontend runs at http://localhost:3000 and your backend runs at http://localhost:8080. These are different origins because their ports differ.
For certain cross-origin requests, the browser first sends an OPTIONS request to ask the server whether the intended request is permitted.
Example:
OPTIONS /api/orders HTTP/1.1
Origin: http://localhost:3000
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, authorization
The server may respond with headers such as:
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: content-type, authorization
If the preflight succeeds, the browser can send the actual request, subject to the remaining CORS rules.
Remember:
- Not every cross-origin request triggers a preflight.
OPTIONSdoes not establish TLS or encrypt the connection.- CORS is enforced by browsers; it is not a replacement for backend authentication or authorization.
4. Keep-alive vs. long-lived connections
These sound similar, but they solve different problems.
| Feature | HTTP keep-alive | Long-lived streaming connection |
|---|---|---|
| Purpose | Reuse a connection | Keep communication open for ongoing data |
| Typical use | Fetch HTML, CSS, APIs | Live updates, AI token streaming |
| Behavior | Multiple requests over a reusable connection | One connection can deliver ongoing updates |
| Example | Browser loading page assets | Streaming an AI response |
Keep-alive example:
Your browser requests an HTML page, then CSS, then JavaScript. With connection reuse, it can send multiple HTTP requests over the same connection instead of establishing a new TCP/TLS connection for each one.
Long-lived connection example:
An AI chatbot keeps a response stream open so tokens arrive as the model generates them.
Keep-alive doesn't automatically mean a connection remains open forever. Servers, clients, proxies, and load balancers can all impose timeouts.
5. SSE vs. WebSockets vs. Webhooks
These are three different ways to communicate between systems.
Server-Sent Events (SSE)
- One-way: server → client.
- Uses an HTTP response stream, typically over a long-lived
GET. - Suitable for AI token streaming, live notifications, and progress updates.
- Browser API:
EventSource. - Primarily streams UTF-8 text.
WebSockets
- Two-way: client ↔ server.
- Both sides can send messages independently.
- Suitable for multiplayer games, collaborative editing, and real-time chat.
- Browser API:
WebSocket. - Supports text and binary messages.
Webhooks
- Event-driven HTTP requests, commonly server → server.
- One system sends an HTTP request to another system's endpoint when an event occurs.
- Suitable for payment confirmations, GitHub events, and order updates.
- Usually uses
POST. - Does not require the receiver to keep a connection continuously open.
Quick comparison
| Question | SSE | WebSockets | Webhooks |
|---|---|---|---|
| Continuous connection? | Yes | Yes | Usually no |
| Two-way communication? | No | Yes | Not inherently |
| Typical communication | Server → browser | Client ↔ server | Sender → receiver |
| Best example | AI streaming | Live chat | Payment notification |
Practical heuristic: Use SSE when the server needs to stream updates to the browser, WebSockets when both sides need ongoing real-time communication, and webhooks when another system needs to notify your backend about an event.
6. Cyclic Sort — DSA
Cyclic Sort is an in-place sorting technique useful when an array contains numbers in a known, bounded range, commonly 1 to n.
Core idea
Instead of repeatedly comparing neighboring elements, put each value directly into its correct index.
For an array containing values from 1 to n:
\[ \text{correctIndex} = \text{value} - 1 \]
Example:
Initial array
3
1
5
4
2
After cyclic sort
1
2
3
4
5
Java implementation
public class CyclicSort {
public static void cyclicSort(int[] arr) {
int i = 0;
while (i < arr.length) {
int correctIndex = arr[i] - 1;
if (arr[i] != arr[correctIndex]) {
int temp = arr[i];
arr[i] = arr[correctIndex];
arr[correctIndex] = temp;
} else {
i++;
}
}
}
}
This implementation assumes the array contains every integer from 1 to n exactly once.
Complexity
- Time: \(O(n)\) under the stated input constraints.
- Auxiliary space: \(O(1)\).
- Sorting style: In-place, without a separate array.
When is Cyclic Sort useful?
It is particularly useful for problems involving arrays of values from 1 to n, such as:
- Finding a missing number.
- Finding duplicate numbers.
- Finding all disappeared numbers.
- Finding the first missing positive integer, using related index-placement techniques.
The last problem requires extra care because negative values, zeros, and out-of-range values must be handled.
Quick revision quiz
1. Which component resolves a domain name by performing DNS lookups on your behalf?
TCP handshake
Recursive DNS resolver
TLS certificate
2. What does TLS primarily provide?
Reliable packet ordering
Domain-to-IP translation
Encryption and authentication
3. Which mechanism is best suited to stream AI-generated tokens to a browser?
SSE
Webhook
DNS
4. Which mechanism supports ongoing communication in both directions?
SSE
WebSockets
CORS preflight
5. For an array containing distinct values from 1 to n, where does value x belong in Cyclic Sort?
Index x
Index x - 1
Index 0
Reset quiz
The big picture: DNS finds the destination, TCP establishes reliable communication, TLS secures it, HTTP defines the request, and mechanisms such as SSE and WebSockets determine how ongoing application data flows. Cyclic Sort is a separate DSA technique that uses known value ranges to place elements directly at their correct indices.