Skip to content
Tutorials Center
My learning

Topics

Lesson 51 of 55 ยท Real-World Projects

Project: Follow a Packet Across the Internet

Follow one request from your laptop to a server: simulate a DNS lookup chain, route a packet hop by hop with a latency model, and recover lost data with acknowledgments.

When you type an address in a browser and press Enter, a quick journey begins. A name is turned into a number, your request is chopped into packets, each packet is passed from router to router across cities and oceans, and the answer makes its way back, with lost pieces resent along the way. All this normally happens in a fraction of a second, which is why it feels like magic.

In this project you will follow that journey by building three small simulations: a DNS lookup with a cache, a route with a latency model that adds up the delay on every hop, and a reliable transfer that survives a lossy network using acknowledgments, timeouts and retries. It brings together Networking Basics, The Internet, IP Addresses and DNS and How the Web Works. Real networks cannot run in a browser tab, so each piece is a model with invented numbers; the shape of the behavior is real, but the exact milliseconds are not measurements.

The whole trip

sequenceDiagram
  participant L as Laptop
  participant R as DNS resolver
  participant S as Web server
  L->>R: Where is www.example.com?
  R-->>L: 203.0.113.7
  L->>S: SYN (let us talk)
  S-->>L: SYN-ACK
  L->>S: ACK, then the HTTP request
  S-->>L: Response in several packets
  L->>S: ACKs for the packets received

The browser first asks DNS for an address, then opens a connection and exchanges data in acknowledged packets.

Each stage costs time. Look-ups cost a few round trips, the connection setup costs one more, and the data transfer costs at least one. This is why websites that make many requests to far-away servers feel slow, and why caches and nearby servers (see Email, the Cloud and Everyday Online Services) help so much.

Stage 1: DNS, the phone book with a chain of command

Computers route traffic by IP address, but people prefer names. DNS (the Domain Name System) translates one into the other. No single computer knows every name. Instead, the work is split into a hierarchy, and your resolver (usually run by your internet provider or a public service) walks down it:

  1. Ask a root server: "who handles .com?" It answers with the addresses of the .com servers.
  2. Ask a top-level domain (TLD) server: "who handles example.com?" It names the domain's own servers.
  3. Ask the authoritative server for example.com: "what is www.example.com?" It gives the final answer.

The resolver then caches the answer for a time (the record's time to live), so the next person who asks gets it immediately. In the stand-in, each server has a made-up delay. The addresses use 203.0.113.x, a range reserved for documentation, so they never point at a real machine.

JS Stage 1: a DNS lookup with a cache

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first lookup walks the whole chain and takes 75 ms in this model. The second is answered from the cache in 2 ms. The third asks for a name that does not exist, so the chain is walked and ends with the DNS error called NXDOMAIN. Real DNS also has extras: answers can come from several servers, caches exist at several levels (browser, operating system, resolver), and IPv6 addresses are returned in AAAA records.

Stage 2: routing and the latency model

With an address in hand, the laptop starts sending packets. A router reads each packet's destination address, picks the best next link and forwards it, with no knowledge of the whole route. Routers share information with their neighbours (through protocols such as BGP between large networks), and the sum of these local choices is the path.

How long does a packet take? Each hop adds three kinds of delay:

  • Propagation delay: the time for the signal to travel along the link. Light in glass covers roughly 200 km per millisecond, so crossing the Atlantic alone costs about 30 ms, however fast your plan is. This is a physical limit.
  • Transmission delay: the time to push all the bits onto the link, which is size in bits / bandwidth. A bigger packet or a slower link makes it longer.
  • Processing and queuing delay: time spent in the router deciding and waiting its turn. Congestion makes queues long.
flowchart LR
  L["Laptop"] --> H["Home router"]
  H --> I["ISP"]
  I --> X["Exchange"]
  X -->|"short route"| E["Backbone EU"]
  X -->|"longer route"| A["Backbone alt"]
  E --> U["Backbone US"]
  A --> U
  U --> D["Data center"]
  D --> S["Server"]

The network used by the simulation: two ways to cross the ocean.

The stand-in below holds a map like that, with each link's length and bandwidth. It uses a shortest-path search (Dijkstra's algorithm, which also appears in graph-search lessons) to choose the lowest-delay path, then prints each hop's delays. It compares a full-size 1,500-byte packet to a tiny 64-byte one, and estimates the sending time for a 1 MB file on the slowest link.

JS Stage 2: pick a route and add up the delays

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Study the numbers. The ocean crossing contributes about 29 ms, which is almost all of the roughly 31.6 ms one-way time. The size of the packet hardly matters for latency (1,500 and 64 bytes differ by a few hundredths of a millisecond), but the slowest link, the bottleneck, limits the bandwidth for big transfers. That difference between delay and throughput is the heart of Networking Basics. Moving a server closer to the user is the only way to reduce propagation delay, which is why companies use data centers around the world and content delivery networks.

Stage 3: reliable delivery

The routers of the internet make no promises. Under load a router may simply drop a packet, and a wire or Wi-Fi may corrupt another. TCP, the protocol that carries most web traffic, builds reliability on top of that unreliable foundation with a few ideas:

  • Data is cut into segments with sequence numbers, so the receiver can put them back in order and spot gaps.
  • The receiver sends an acknowledgment (ACK) for what arrived.
  • The sender starts a timer for each segment. If no ACK arrives before the timeout, it retransmits.
  • A window lets the sender have several segments in flight at once instead of waiting for each acknowledgment. Waiting for every ACK ("stop-and-wait") wastes most of the time on a long path.

Our stand-in loses 30% of packets, in both directions, using a seeded random generator so that the result is the same each run. It sends six small segments of a message, once with a window of 1 (stop-and-wait) and once with a window of 4. A round that has a loss costs a 100 ms timeout; a clean round costs one 60 ms round trip. The receiver stores segments by number, so a retransmitted duplicate does no harm.

JS Stage 3: acknowledgments, timeouts and retries

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In both runs the message arrives complete and in order, although 4 of 10 transmissions were lost. The reliability costs time: segment 1 failed four times in a row in the stop-and-wait run (three lost data packets, then a lost acknowledgment), and each failure cost a whole timeout. The window of 4 finished in 260 ms instead of 760 ms because the sender kept several segments in flight, so the losses overlapped instead of stacking up. A real TCP also adapts the window to the network ("congestion control") so that it does not flood a slow link, and it detects loss faster than a fixed timer by noticing duplicate acknowledgments.

If a program needs speed more than perfection, such as a live video call or a game, it may use UDP, which has no acknowledgments or retries. A lost packet is simply a glitch, because resending old video frames would arrive too late to matter.

Putting the stages together

A total estimate for opening a page is the sum of the pieces. Using the numbers from the three simulations, a first visit costs about 75 ms of DNS, one round trip (about 63 ms) to open the TCP connection, one more to send the request and receive the first answer bytes, and extra time for any lost packet. Add the secure-connection handshake of HTTPS, covered in How the Web Works, and it is easy to see why browsers reuse connections, cache files and look up names ahead of time.

Ideas to extend it

  • Add a cache to the route model for repeated visits, and show a CDN node near the laptop that shortens the path to a few milliseconds.
  • Add TTL expiry to the DNS cache with a simulated clock.
  • Let a link fail: remove one and show that the route table finds the longer way around the ocean.
  • Add random queuing delay per hop and compute the average and the 95th percentile of many trips (jitter).
  • Give each segment a checksum, corrupt some in transit, and have the receiver reject them.
  • Implement slow start: the window begins at 1 and doubles after every clean round.

What is real and what is the stand-in? The hierarchy of DNS servers and caching, hop-by-hop forwarding, the three kinds of delay, sequence numbers, acknowledgments, timeouts and retransmission, sliding windows, and the trade-off between TCP and UDP are all real. The topology, distances, bandwidths, delays and the 30% loss rate are invented. Real routers compute routes with distributed protocols rather than a central search, and real TCP has handshakes, congestion control and byte-oriented sequence numbers.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does a DNS resolver walk through root, TLD and authoritative servers?

    No server knows every name. Root servers point to TLD servers, which point to the domain's own servers that hold the final answer.
  2. 2. Which part of a packet's delay can only be reduced by moving the server closer?

    Signals in fibre cover a fixed distance each millisecond, so only a shorter path shortens propagation.
  3. 3. A 1 MB download is slow although latency is low. What is the most likely limit?

    Large transfers are limited by throughput, which is set by the slowest link.
  4. 4. What does a TCP sender do when an acknowledgment does not arrive before the timeout?

    A missing ACK is treated as a loss, so the segment is sent again.
  5. 5. Why is a window of several segments faster than stop-and-wait on a long path?

    With a window, round trips overlap and a lost segment delays less of the transfer.

Summary

  • DNS turns a name into an address through a hierarchy (root, TLD, authoritative) and caches the answer to skip the chain next time.
  • Routers forward packets hop by hop; the path is the result of many local decisions.
  • Delay on a hop is propagation (distance), transmission (size over bandwidth) and processing or queuing.
  • Latency and bandwidth are different things: the ocean limits one, the slowest link limits the other.
  • TCP makes an unreliable network reliable with sequence numbers, acknowledgments, timeouts, retransmission and windows; UDP trades that away for speed.
  • A page load is the sum of DNS, connection setup, the request and the transfer, which is why caching and nearby servers matter.

You have now built the key layers yourself. Test them with questions: Practice: Hardware and Data (with Solutions).