Verified Public Note•Oct 20, 2026•4 views

TCP -> TLS Handshake & SSE Webhooks and Websockts

system-designos
Share this NoteDrive viral discussions across engineering channels

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

ConceptFull formPurpose
TCPTransmission Control ProtocolReliable, ordered delivery of bytes
TLSTransport Layer SecurityEncryption, integrity, and authentication

Think of it this way:

  • TCP: Establishes reliable communication.
  • TLS: Makes that communication secure.

The TCP three-way handshake is:

  1. Client → Server: SYN
  2. Server → Client: SYN-ACK
  3. 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.

MethodTypical purpose
GETRetrieve data
POSTSubmit data or create a resource
PUTReplace a resource
PATCHPartially update a resource
DELETEDelete a resource
OPTIONSAsk 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:

CODE
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:

CODE
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.
  • OPTIONS does 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.

FeatureHTTP keep-aliveLong-lived streaming connection
PurposeReuse a connectionKeep communication open for ongoing data
Typical useFetch HTML, CSS, APIsLive updates, AI token streaming
BehaviorMultiple requests over a reusable connectionOne connection can deliver ongoing updates
ExampleBrowser loading page assetsStreaming 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

QuestionSSEWebSocketsWebhooks
Continuous connection?YesYesUsually no
Two-way communication?NoYesNot inherently
Typical communicationServer → browserClient ↔ serverSender → receiver
Best exampleAI streamingLive chatPayment 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

CODE
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.

Share this NoteDrive viral discussions across engineering channels
Accelerate Your Tech Interview Preparation

Prepare for Senior Engineering Rounds with PrepTrack

Track 518+ curated DSA problems, Low-Level Design scenarios, and High-Level Distributed Architecture blueprints. Build your own central notes vault with automatic GitHub synchronization.