Unit 3.2 · about 4 h
250 / 324TCP and UDP transport behavior
Sockets expose streams, not messages. Misreading TCP as a message protocol causes every framing bug in the next unit, and confusing flow control with congestion control misdirects tuning.
- explain
- measure
You can now
- Explain the TCP service contract: reliable ordered duplicate-suppressed byte stream, setup cost, and what an
ACKdoes not promise. - Contrast UDP datagrams with TCP streams and choose between them from loss, ordering and latency needs.
- Read zero-window, retransmission and congestion evidence in a capture and name the limiting mechanism.
Covers
- TCP connection setup, byte streams and flow control intuition
- UDP datagrams and loss, duplicate and reorder behavior
- Congestion-control mechanics and packet-capture analysis depth
Study, in this order
6 steps
- TCP versus UDP contractsAlgoMaster Networks - pcap and concurrent server labs
TCP's Service Contract (reliable, ordered, duplicate suppression, byte stream); UDP's Service Contract; Message Size Matters
- The TCP three-way handshakeAlgoMaster Networks - pcap and concurrent server labs
Step 1 through Step 3; Sequence Number Arithmetic; Why Three Messages Are Necessary; Listening Queues and Server Load
- Reliability and flow controlAlgoMaster Networks - pcap and concurrent server labs
Reliability Is Defined Over Bytes; An ACK Is Not an Application Receipt; Gaps and Duplicate ACKs; Retransmission Timeout; then Flow Control and the Sliding Window: Receive Buffers and Application Reads, Zero Window, Diagnosing a Flow-Control Stall
Depth · Use https://algomaster.io/learn/computer-networks/flow-control-sliding-window for the flow-control sections.
- TCP and UDP path in Beej's Network ConceptsBeej's Guide to Network Concepts - TCP and UDP path
Chapter 14, TCP (Sections 14.4.1, 14.4.2 and 14.6); Chapter 15, UDP (Sections 15.4, 15.6 and 15.7)
- A real zero-window stallAlgoMaster Networks - pcap and concurrent server labs
Turn the Stream into a Timeline; Inspect the Edges of the Gap (zero-window evidence); the host-correlation conclusion
Depth · Follow the phase method and the window evidence. The audit-pipe root cause is one example of host correlation, not a pattern to memorize.
- Congestion control vocabulary (optional topic)AlgoMaster Networks - pcap and concurrent server labs
Flow Control and Congestion Control Solve Different Problems; Slow Start; Congestion Avoidance and AIMD
Depth · Vocabulary only for the optional topic. No measurement required; stop before the variant comparison.
Focus on
- TCP numbers bytes, not messages. The
ACKadvances over the contiguous prefix; a gap stalls acknowledgment even when later bytes arrived. - An
ACKmeans the peer kernel holds the bytes. It does not mean the application read or acted on them. - Flow control (receiver window) and congestion control (sender network estimate) are independent limits; the sender obeys the smaller.
- UDP preserves message boundaries and promises nothing else. Loss, duplication and reordering are normal and must be handled above it.
Reproduce closed-book
create_connection plus close calls after 10 warm-up ones; then time 200 round trips of a 4-byte length-prefixed echo (TCP_NODELAY, 20 warm-up) at 64 bytes and 64 KiB. Pinned to two performance cores with taskset -c 2,4 on an i9-12900K with Linux 7.0, three runs gave connect plus close medians of 12 to 16 us, a 64 B echo p50 of about 9 us and a 64 KiB p50 of about 29 us; unpinned runs that landed on efficiency cores were up to about twice as slow. Record yours with the same setup and say which cores ran it.Traps
- Calling a retransmission proof of packet loss. Delayed ACKs, reordering and spurious timeouts cause retransmissions on healthy paths.
- Expecting
closeto deliver pending application work. A gracefulFINends the byte stream; it says nothing about processed requests. - Treating UDP as TCP without acknowledgments. Without sequence numbers and reassembly above it, duplicates and reordering reach the application.
Practice
AlgoMaster: Reassemble TCP Segments
nextExpected over out-of-order overlapping [seq, length] intervals under the contiguous-prefix rule
The gap example ([[0,3],[5,2]] returns 3) is the ACK rule in code; overlapping intervals must not double-count.
Self-test
-
Segments \([0,3)\) and \([5,7)\) arrived. Which byte does the receiver ACK next?
Byte 3. The ACK covers only the contiguous prefix from 0; bytes 5 and 6 wait behind the gap at 3 and 4. -
The client advertised a zero window for five seconds, then the transfer resumed with no retransmissions. Where was the stall?
At the receiving endpoint: its buffer stayed full because the application stopped reading. The network and server were waiting on window updates. -
Why does a fresh TCP connection cost at least one round trip before data flows?
SYN,SYN-ACKandACKmust synchronize sequence numbers and open state on both ends; data normally starts after the exchange. -
Two identical datagrams arrive over UDP. Did the network duplicate one, or did the sender retransmit?
UDP alone cannot tell. Both look identical; only an application sequence number distinguishes a duplicate from a retry.
Completion criterion
Stop when
You classify a stall as receiver-window, loss-recovery or endpoint silence from window and ACK evidence, and your reassembler returns 3 and 5 on the two worked examples.
Leads to
- 3.4 Sockets, partial reads and writes, and message framing Build message framing on top of the byte stream.
- 3.7 Connection shutdown, pools and latency decomposition Turn setup cost and window stalls into pool and latency policy.
Comes after
- 3.1 Network layers, addressing, routing and DNSMove one layer up to transport behavior on the path you can now address.
Resources for this unit
- Beej's Guide to Network Concepts - TCP and UDP path Read the OS and sockets, protocol layering, IP, UDP and TCP chapters alongside packet captures from your own client and server. Use it to explain transport behavior rather than memorize layer names.
- AlgoMaster Networks - pcap and concurrent server labs Use Reading a Packet Capture: A Real Bug and Building a Concurrent TCP Echo Server after the Beej implementation. Reproduce one packet-level observation and one admission, deadline or shutdown behavior in your independently written service.
Also in this section
- Beej's Guide to Network Programming - sockets to multiplexing Read What is a socket?, IP addresses and data structures, Client-server background, System calls or bust, Client-server examples, Slightly advanced techniques and sendall. Build the simple TCP pair first, then replace delimiter assumptions with a bounded length-prefixed protocol.
- MIT 6.5840 - RPC, threads and fault vocabulary Read the introductory RPC and threads material plus the lab guidance. Use it to enumerate timeout ambiguity and duplicate effects. Stop before Raft and the full lab sequence.
- Replicated Log and Stream Processor - protocol slice Use only the protocol requirements, deterministic network fault matrix and delivery-contract questions. Build the single-node framed service now; keep replication and consensus for the later project.
- Designing Data-Intensive Applications, 2nd edition - Chapter 5 Read Formats for Encoding Data (printed pp. 163 to 177) for schema merits and compatibility, and Dataflow Through Services: REST and RPC (printed pp. 180 to 186) for service dataflow, version coexistence and the idempotence pointer.
- gRPC Deadlines guide Read Overview, Deadlines on the Client and Deadline Propagation for absolute deadlines, remaining-time forwarding and clock-skew shielding.
- gRPC Status Codes Read the full status list, including
DEADLINE_EXCEEDED,UNAVAILABLEand the codes never generated by the library, for the error taxonomy. - W3C Trace Context Read Section 3.2 for the
traceparentformat and Sections 3.4 to 3.5 for the legaltraceparentandtracestatemutations when forwarding.