SD
System Design Textbook CHAPTER 02 • NETWORKING, PROTOCOLS & APIS
CHAPTER 02

Networking Architectures, Transport Protocols & API Design

Network protocols dictate how bytes stream across physical sockets, manage packet loss recovery, compress headers, and handle binary vs textual serialization.

1. The OSI 7-Layer Model & Real-World TCP/IP Stack

7. Application
HTTP, gRPC
6. Presentation
TLS, Protobuf
5. Session
Sockets
4. Transport
TCP, UDP
3. Network
IP, BGP
2. Data Link
Ethernet, MAC
1. Physical
Fiber, Copper

2. TCP vs UDP & Handshake Mechanics

TCP (Transmission Control Protocol)

Connection-oriented, reliable in-order byte stream delivery. Uses 3-Way Handshake (SYN \(\rightarrow\) SYN-ACK \(\rightarrow\) ACK) and Sliding Window Congestion Control (TCP Slow Start, Reno/CUBIC).

Cost: Minimum 1 RTT before any application data can be sent.
UDP (User Datagram Protocol)

Connectionless, unreliable datagram protocol. Zero handshake overhead, zero head-of-line blocking, zero flow control. Ideal for real-time video streaming, gaming, and HTTP/3 QUIC.

Cost: Application layer must handle packet loss if reliability is required.
TLS 1.3 Security Handshake: TLS 1.3 reduces security handshake overhead from 2 RTTs (in TLS 1.2) down to **1 RTT**. For returning clients, TLS 1.3 supports **0-RTT Early Data resumption**, combining TCP SYN+data on the initial flight.

3. HTTP Evolution: HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)

HTTP/1.1: Plaintext & Head-of-Line Blocking Textual headers, sequential requests over single TCP socket. Browser opens up to 6 parallel TCP connections per origin. Subject to application-level Head-of-Line (HoL) blocking.
HTTP/2: Binary Framing & Multiplexing Splits request/response streams into binary frames over a single TCP connection. Uses HPACK header compression. Eliminates application HoL, but TCP-level packet loss still stalls all multiplexed streams.
HTTP/3: QUIC over UDP Replaces TCP with QUIC over UDP. Streams are independent at the transport layer; packet loss in Stream 1 does NOT block Stream 2. Connection IDs allow seamless connection migration from WiFi to 5G cellular networks without dropping sessions.

4. API Architecture Paradigms: REST vs GraphQL vs gRPC

Feature REST GraphQL gRPC
ProtocolHTTP/1.1 or HTTP/2HTTP/1.1 or HTTP/2HTTP/2 (Multiplexed)
Payload FormatJSON / XMLJSONProtocol Buffers (Binary)
Schema ContractingOptional (OpenAPI/Swagger)Strict GraphQL SchemaStrict `.proto` Definition
Over-FetchingCommonEliminated (Client Queries)Eliminated
Streaming SupportLimited (SSE/WS)Subscriptions (WS)Native Bi-directional Streaming

5. Distributed Rate Limiting Algorithms

Token Bucket Algorithm

Bucket holds max \(B\) tokens. Refill rate \(R\) tokens/sec. Request consumes 1 token. Handles bursty traffic well.

tokens = min(capacity, tokens + (now - lastRefill) * refillRate);
if (tokens >= 1) { tokens--; allow(); } else { deny(); }
Sliding Window Counter Algorithm

Combines past window count and current window count using weighted interpolation to prevent burst spikes at boundary borders.

\(Count = Count_{prev} \times (1 - \frac{t_{current}}{Window}) + Count_{curr}\)