Networking


This is a reference page for networking concepts that are essential for building web applications. It covers network architectures, the TCP/IP protocol stack, HTTP, and the key trade-offs you need to understand when designing networked systems.

How to use this page: Keep it open as a reference while working on your projects. The concepts here underpin everything you build with Node.js and React — every time your browser talks to a server, it relies on these protocols.

Network Architectures

When designing a networked application, the first decision is how your devices will communicate. There are two fundamental models, plus a practical combination of both.

Client-Server Architecture

The client-server model is the most common architecture for web-based systems. It defines two distinct roles:

Role Responsibility
Client Initiates requests; consumes resources (e.g., your web browser)
Server Listens for requests; provides resources (e.g., your Node.js backend)

Key characteristics:

  • Multiple clients can connect to the same server simultaneously
  • Connections are always initiated by the client, never the server
  • It is a centralized architecture — all communication flows through the server

When you build a web app, you are building both sides: a server (Node.js/Express) that provides data and a client (React) that runs in the user’s browser.

Peer-to-Peer (P2P) Architecture

In a peer-to-peer architecture, there is no dedicated server. Every node in the network is both a supplier and a consumer of resources.

Key characteristics:

  • Decentralized — no single point of control
  • Peers are equally privileged participants
  • Each peer is both a supplier and consumer of resources

P2P is rare in pure form. BitTorrent is a well-known example: when you download a file via BitTorrent, your client receives chunks directly from other peers who already have parts of the file — no central file server is involved.

Hybrid Architectures

In practice, most systems that need P2P benefits use a hybrid approach: some communication goes through a central server, while some happens directly between peers.

Example — Apple FaceTime: For 1-on-1 calls, FaceTime attempts a direct peer-to-peer connection between devices for the lowest possible latency. If that fails (e.g., due to NAT or firewall restrictions), it routes communication through Apple’s relay servers. For Group FaceTime calls, all participants connect to Apple’s servers, since each device sending a separate video stream to every other participant would overwhelm its upload bandwidth.

Comparing Architectures

Aspect Client-Server Peer-to-Peer Hybrid
Structure Centralized Decentralized Mixed
Single point of failure Yes (the server) No Partial
Scalability Add more servers Scales with peers Flexible
Use case Web apps, APIs, databases File sharing, distributed backup Video calls, gaming

Throughput and Latency

Two critical quality attributes for any networked system:

Throughput measures the volume of work processed per unit of time. Example: “The API server handles 500 requests per second during peak load.”

Latency (response time) measures how long a single request takes to receive a reply. Example: “Each database query returns results in 40ms.”

These are related but not the same:

  • Duplicating servers increases throughput (more requests handled in parallel) without necessarily reducing latency.
  • Implementing caching reduces latency (individual requests are faster) and may also increase throughput.

Analogy: Think of a highway between two cities. Latency is the speed limit — it determines how fast a single truck makes the journey. Throughput is the number of lanes — adding lanes lets you move more total cargo per hour, but it doesn’t make any individual truck arrive faster. Scaling horizontally (more servers) adds lanes; optimizing code or adding caches raises the speed limit.

The TCP/IP Protocol Stack

The internet uses a layered architecture called the TCP/IP stack. Each layer solves a specific problem and relies only on the layer directly below it. This design provides reusability (lower layers can be shared) and flexibility (you can swap one layer’s implementation without affecting the others).

The Four Layers

Layer Responsibility Example Protocols
Application Layer Provides an interface for applications to access network services HTTP, HTTPS, SSH, DNS, FTP, SMTP, POP, IMAP
Transport Layer Provides end-to-end communication between applications on different hosts TCP, UDP
Internet Layer Enables communication between networks through addressing and routing IPv4, IPv6, ICMP
Link Layer Handles the physical transmission of data over local network hardware Ethernet, Wi-Fi, ARP

Where does TLS fit? TLS (and its predecessor SSL, now deprecated) sits between the transport and application layers — it wraps a TCP connection and exposes an encrypted channel that an application protocol like HTTP runs on top of. HTTPS is “HTTP over TLS over TCP.”

Encapsulation (Package Wrapping)

Higher-layer protocols use the protocols directly below them to send messages. Each layer wraps the higher-layer message as its payload and adds its own header — like sealing a letter inside successively larger envelopes, each addressed for a different step of the journey:

Ethernet
Header
IP
Header
TCP
Header
HTTP
Header
Payload
(data)
Link Layer Internet Transport Application

Each message consists of a header (meta information like destination, origin, content type, checksums) and a payload (the actual content of the message).

IP Addresses

Every device on the internet needs a unique address. IP addresses solve this by having two parts: a network portion (like a city) and a host portion (like a street address within that city). Routers use the network portion to forward packets toward the right destination network; once there, the host portion identifies the specific device.

  • IPv4 addresses are 32-bit numbers written as four decimal octets: 0.0.0.0 to 255.255.255.255 (about 4 billion possible addresses)
  • IPv6 was created because the world ran out of IPv4 addresses — it uses 128-bit addresses, providing vastly more unique values

Localhost and the Loopback Interface

127.0.0.1 (or its alias localhost) is a special address called the loopback address. Unlike a normal IP address that routes packets out through your network hardware, loopback traffic never leaves your machine — the operating system short-circuits it internally.

This is why it is indispensable for local development:

  • When you run node server.js, your server listens on localhost:3000 (or whichever port you choose)
  • Your browser — also running on the same machine — sends an HTTP request to localhost:3000
  • The OS intercepts the request before it ever touches Wi-Fi or Ethernet and routes it directly to your server process
  • No internet connection is required; the traffic is entirely internal to your computer

Practical consequence: A server listening on localhost is only reachable from the same machine. If a classmate tries to connect to your laptop’s localhost:3000 from their machine, it will fail — localhost on their machine refers to their machine, not yours.

Public vs. Private IP Addresses

Not all IP addresses are reachable from the internet:

Range Type Example
127.0.0.0/8 Loopback (your own machine) 127.0.0.1
192.168.x.x, 10.x.x.x, 172.16–31.x.x Private (local network only) 192.168.1.42
Everything else Public (internet-reachable) 142.250.80.46

Your laptop typically has a private IP address assigned by your router (e.g. 192.168.1.42). Your router holds the single public IP address that the internet sees. When you deploy a server to the cloud, it gets a public IP — that is what makes it reachable by anyone.

Ports

An IP address identifies a machine, but a single machine can run many networked applications simultaneously (a web server, a database, an SSH daemon…). Ports identify which application on that machine should receive a given message.

The combination of an IP address and a port — written IP:port — is called a socket address and uniquely identifies a communication endpoint:

192.168.1.42:3000   →  your Node.js server
192.168.1.42:5432   →  your PostgreSQL database
  • Port numbers range from 0 to 65535
  • Well-known ports (0–1023) are reserved for standard services: 80 (HTTP), 443 (HTTPS), 22 (SSH), 5432 (PostgreSQL)
  • Ephemeral ports (typically 49152–65535) are assigned automatically by the OS for the client side of a connection — you never type these in, but every outgoing TCP connection uses one
  • When developing locally, you pick an unprivileged port like 3000 or 5000 to avoid needing administrator privileges (ports below 1024 require root/admin on most systems)

DNS (Domain Name System)

Humans use names like github.com; computers use IP addresses like 140.82.121.4. DNS is the distributed directory that translates one into the other — effectively the phone book of the internet.

When you type github.com into your browser:

  1. Your OS checks its local DNS cache — if it recently resolved this name, it reuses the answer
  2. If not cached, it sends a DNS query (over UDP, port 53) to a DNS resolver — typically provided by your ISP or configured manually (e.g. Google’s 8.8.8.8)
  3. The resolver works through a hierarchy of DNS servers to find the authoritative answer
  4. Your OS receives the IP address, caches it for a configurable time (the TTL), and the browser proceeds with the HTTP request

This is why DNS uses UDP: each lookup is a single independent question-and-answer pair. If the response is lost, the client simply retries — no persistent connection is needed.

Transport Layer Protocols: TCP vs. UDP

The transport layer offers two protocols with fundamentally different trade-offs. Choosing between them is one of the most important networking decisions you will make.

UDP (User Datagram Protocol)

UDP simply “throws” messages at the receiver without establishing a connection first.

  • Fast and lightweight — no connection setup overhead
  • Connectionless — just sends the data
  • Does not guarantee delivery or order
  • Includes a checksum for error detection (mandatory in IPv6), but does not recover from errors — corrupted packets are silently discarded
  • If a message is lost, it is simply gone

UDP is ideal when speed matters more than reliability: DNS name resolution (a fast, independent lookup where a retry is cheap — though DNS falls back to TCP when a response is too large for a single UDP packet), live GPS position broadcasts in navigation apps, and live financial-market tick streams pushed to traders’ dashboards (where a stale price is no longer worth waiting for).

@startuml
participant sender: Sender
participant receiver: Receiver

sender ->> receiver: Datagram [1]
sender ->> receiver: Datagram [2]
note right of receiver: checksum failed — discard silently
sender ->> receiver: Datagram [3]
sender ->> receiver: Datagram [4]
note right of receiver: packet lost — never arrives
sender ->> receiver: Datagram [5]
note over sender: sender never knows about the lost or corrupted packets
@enduml

TCP (Transmission Control Protocol)

TCP is more complex but provides reliable, ordered delivery. It uses a three-way handshake to establish a connection:

Connection Setup (3-Way Handshake):

@startuml
participant client: Client
participant server: Server

client ->> server: SYN
server ->> client: SYN-ACK
client ->> server: ACK
note over client, server: Connection established
@enduml

Data Transfer: Messages are sent in order, each with a checksum for error detection (like UDP, but TCP goes further). The receiver sends ACKs to confirm receipt. If the sender doesn’t receive an ACK within a timeout, it retransmits the message — this error recovery is what distinguishes TCP from UDP.

@startuml
participant client: Client
participant server: Server

client ->> server: Data [seq=1]
server ->> client: ACK [seq=1]
client ->> server: Data [seq=2]
note right of server: packet lost — no ACK sent
note over client: timeout — retransmit
client ->> server: Data [seq=2]
server ->> client: ACK [seq=2]
@enduml

Connection Teardown:

@startuml
participant client: Client
participant server: Server

client ->> server: FIN
server ->> client: ACK
server ->> client: FIN
client ->> server: ACK
note over client, server: Connection closed
@enduml

The cost of reliability: For N data messages, TCP sends significantly more total messages than UDP — the handshake, ACKs, and teardown all add overhead. UDP would send just N messages.

TCP vs. UDP — Trade-Offs at a Glance

Aspect TCP UDP
Message order Preserved Any order
Error detection Included (checksums) Included (checksums), but no error recovery
Lost messages Retransmitted Lost forever
Speed Slower (overhead) Fast (no overhead)

When to Use Each

Protocol Best For Examples
TCP Data that must arrive completely and in order Pushing code to a Git repository, submitting an online tax return, transferring files via SFTP, web browsing
UDP Real-time data where speed beats reliability DNS queries (primarily), live GPS updates, live screen sharing during remote presentations, live IoT sensor telemetry

Live online stock-trading platforms use a hybrid: UDP for high-frequency price-tick broadcasts (often hundreds of updates per second per symbol), since a missed tick is harmless — the next one carries the current price milliseconds later. TCP handles trade orders, account balance updates, and trade confirmations, where a lost or reordered message would corrupt the user’s account state. UDP ticks include the absolute current price of each symbol, so a single dropped packet never causes lasting inconsistency.

HTTP (Hypertext Transfer Protocol)

HTTP is the foundation of data communication on the World Wide Web. It is an application-layer protocol that runs on top of TCP.

Key Property: Stateless

HTTP is a stateless protocol — each request is independent, and the server does not remember anything about previous requests from the same client. Every request must contain all the information the server needs to respond. (Real applications layer state on top of HTTP using mechanisms like cookies, sessions, or bearer tokens such as JWTs.)

HTTP versions. HTTP/1.1 (1997) introduced persistent connections and pipelining. HTTP/2 (2015) added binary framing and multiplexing over a single TCP connection. HTTP/3 (standardized 2022) replaces TCP with QUIC, which runs over UDP and integrates TLS — so an HTTP/3 connection avoids head-of-line blocking and can establish in fewer round trips.

HTTPS is HTTP wrapped in TLS (the successor to the now-deprecated SSL). It provides confidentiality (no eavesdropping), integrity (no tampering), and server authentication (you really are talking to ucla.edu).

HTTP Verbs (Methods)

Verb Purpose Response Contains
GET Retrieve a resource (web page, data, image, file). Safe and idempotent. The resource content + status code
POST Send data for processing — typically to create a new resource (form submission, file upload). Not idempotent. Status code (and often the new resource or its location)
PUT Create or replace the resource at a specific URI. Idempotent. Status code
PATCH Apply a partial update to an existing resource. Status code
DELETE Delete a resource on the server. Idempotent. Status code
HEAD Retrieve only headers of a resource, not the body. Headers + status code

URLs (Uniform Resource Locators)

A URL is the web address of a resource:

{protocol}://{domain}(:{port})(/{resource})

http://localhost:5000/courses/cs101
https://myapp.com/about.html
Component Example Required?
Protocol http://, https:// Yes
Domain localhost, myapp.com Yes
Port :5000, :3000 No (defaults: 80 for HTTP, 443 for HTTPS)
Resource path /courses/cs101, /about.html No (defaults to /)

HTTP Status Codes

Every HTTP response includes a status code that tells the client what happened:

Category Meaning Common Codes
2xx Success 200 OK — request succeeded; 201 Created — new resource created
4xx Client error 400 Bad Request — malformed syntax; 401 Unauthorized; 403 Forbidden; 404 Not Found — resource doesn’t exist
5xx Server error 500 Internal Server Error — generic server failure; 502 Bad Gateway; 503 Service Unavailable

Rule of thumb: 2xx = you did it right, 4xx = you messed up, 5xx = the server messed up.

HTTP Headers

Each HTTP message includes headers with metadata about the request or response. A critical header:

Content-Type — tells the receiver what kind of data is in the body:

Content-Type Used For
text/html; charset=utf-8 HTML web pages
text/plain Plain text
application/json JSON data (the standard for API communication)

HTTPS (HTTP Secure)

HTTPS uses SSL/TLS encryption to secure communication. It is essential whenever sensitive data is transferred (passwords, personal information, private messages) and has become the default for all public web pages, even for non-sensitive content.

Building a Server with Node.js

Node.js ships with a built-in http module that lets you create an HTTP server from scratch:

const http = require('http');
const PORT = 3000;

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, World!\n');
});

server.listen(PORT, 'localhost', () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});

For real applications, the Express framework provides much cleaner routing:

const express = require('express');
const app = express();
const port = 5000;

// GET /courses/:courseId — route parameter
app.get('/courses/:courseId', (req, res) => {
  res.send(`GET request for course ${req.params.courseId}`);
});

// POST /enrollments — create a new enrollment
app.post('/enrollments', (req, res) => {
  res.send('POST request to enroll in a course');
});

// Catch-all 404 handler — must be last
app.all('*', (req, res) => {
  res.status(404).send('404 - Page not found');
});

app.listen(port, () => {
  console.log(`Express server listening on port ${port}`);
});

For a hands-on walkthrough, work through the Node.js Essentials Tutorial.

Practice

Networking Concepts

Review key networking concepts: architectures, protocols, HTTP, and the TCP/IP stack.

Difficulty: Basic

What are the two roles in a client-server architecture, and who initiates the connection?

Difficulty: Basic

How does a peer-to-peer (P2P) architecture differ from client-server?

Difficulty: Intermediate

What is a hybrid architecture? Give a real-world example.

Difficulty: Basic

Explain the difference between throughput and latency.

Difficulty: Advanced

You type a URL into your browser and press Enter. Trace the journey of that HTTP request down the four layers of the TCP/IP stack — name each layer and describe what it contributes.

Difficulty: Intermediate

What is encapsulation (package wrapping) in the TCP/IP stack?

Difficulty: Advanced

What is the TCP three-way handshake and why is it needed?

Difficulty: Advanced

How does TCP guarantee reliable delivery during data transfer?

Difficulty: Basic

What does it mean that HTTP is stateless?

Difficulty: Basic

Name at least three main HTTP verbs and what each does.

Difficulty: Basic

What is 127.0.0.1 and what is it commonly called?

Difficulty: Intermediate

What is a URL and what are its components?

Difficulty: Basic

What does HTTPS add on top of HTTP, and why is it important?

Networking Fundamentals Quiz

Test your understanding of network architectures, the TCP/IP protocol stack, HTTP, and how the internet works.

Difficulty: Basic

In a client-server architecture, which statement is TRUE?

Correct Answer:
Difficulty: Basic

What is the key advantage of peer-to-peer (P2P) architecture over client-server?

Correct Answer:
Difficulty: Basic

What is the difference between throughput and latency?

Correct Answer:
Difficulty: Intermediate

In the TCP/IP stack, what is the purpose of the Transport Layer?

Correct Answer:
Difficulty: Intermediate

When data travels down through the TCP/IP stack before being sent, what happens at each layer?

Correct Answer:
Difficulty: Basic

A student runs node server.js and their terminal shows: Server listening on http://localhost:5000. They open a browser on the same machine. Which URL should they visit?

Correct Answer:
Difficulty: Basic

HTTP is described as a ‘stateless’ protocol. What does this mean?

Correct Answer:
Difficulty: Basic

Your Express route handler queries the database for a course by ID, but no matching course exists. Which HTTP status code should the handler return?

Correct Answer:
Difficulty: Basic

Why was HTTPS created, and what does it add on top of HTTP?

Correct Answer:
Difficulty: Intermediate

Arrange the TCP/IP layers in order from bottom (closest to hardware) to top (closest to the application).

Drag lines into the solution area in the correct order (some items are distractors that should not be used). Keyboard: focus a line and press Space or Enter to move it between the bank and the answer area. Use Arrow Up or Arrow Down to reorder within the answer area.
Correct order:
Link Layer
Internet Layer
Transport Layer
Application Layer
Difficulty: Intermediate

Which of the following are guarantees provided by TCP but NOT by UDP? (Select all that apply)

Correct Answers:

Networking: Making Decisions

Given real-world application scenarios, choose the right network architecture, transport protocol, and application protocol. These questions test your ability to analyze trade-offs and justify design decisions.

Difficulty: Intermediate

You are building a collaborative coding interview platform where the candidate and the interviewer edit the same file at the same time, character by character. The candidate types def foo():, then immediately replaces it with def bar():. If those two edits arrive at the interviewer in the wrong order, the interviewer’s screen ends up showing def foo(): even though the candidate’s screen shows def bar():. Which transport protocol should the editing channel use?

Correct Answer:
Difficulty: Intermediate

You’re building a smart doorbell with a live camera feed. When a visitor presses the button, the homeowner’s phone displays the camera in real time so the homeowner can see who’s there before deciding to answer. Which transport protocol should carry the camera video stream?

Correct Answer:
Difficulty: Advanced

An indie team is building an online multiplayer racing game. Each player’s car position and speed update 60 times per second so all players see each other accurately on the track. The game also records lap completion events, awards podium finishes, and lets players spend earned currency on car cosmetic upgrades that persist between matches. What transport-protocol strategy fits best?

Correct Answer:
Difficulty: Basic

You are building a cloud file storage service similar to Dropbox or Google Drive. A user clicks ‘Upload’ on a 200 MB folder of design files. The folder must arrive at the server bit-for-bit identical so that other devices syncing the same folder see the exact same files. Which transport protocol should carry the upload?

Correct Answer:
Difficulty: Intermediate

A startup is launching an online concert ticketing platform. Fans browse upcoming shows, pay with a credit card, and receive a unique QR-code ticket. The platform must prevent two fans buying the same seat, and it must keep an immutable record of every sale for tax and refunds. Should the backend be client-server or peer-to-peer?

Correct Answer:
Difficulty: Intermediate

A research consortium is designing a distributed scientific data archive: each participating university hosts a copy of selected genome datasets and serves them directly to other universities that request a copy. There must be no single institution that controls or can take down the archive, and the system should keep functioning even if several universities go offline at once. Which architecture fits these requirements best?

Correct Answer:
Difficulty: Basic

You are building a walkie-talkie style voice app for outdoor crews — a hiker holds the talk button, speaks for a few seconds, and any teammate within range hears the audio in real time. The audio must feel immediate, and a brief audio gap is far less disruptive than a hesitation in the middle of a sentence. Which transport protocol should carry the voice audio?

Correct Answer:
Difficulty: Basic

A smart-home product ships a phone app that refreshes every 5 seconds to show the current state of the user’s connected devices — lights on/off, thermostat temperature, door-lock status. The phone app sends a request to the company’s central hub server, which responds with the latest readings collected from devices in the home. Which architecture pattern is this?

Correct Answer:
Difficulty: Intermediate

For which of the following would TCP be the better choice over UDP? (Select all that apply)

Correct Answers: