分布式系统中的核心网络协议
学习核心网络协议(如 TCP、UDP 和 HTTP)如何成为设计健壮且可扩展的分布式系统的基础。
HTTP request format
1POST /articles/index.html HTTP/1.1 2Host: www.example.com 3Connection: Keep-Alive 4User-Agent: Mozilla/5.0 5Content-Type: application/json 6Content-Length: 80 7Accept-Language: en-us 8Accept-Encoding: gzip, deflate 9{ 10 "Id": 45321, 11 "Customer": "Mark Brown", 12 "Quantity": 10, 13 "Price": 199.0 14}
HTTP response format
1// Status code 2HTTP/1.1 200 OK 3//Headers 4Connection: close 5Date: Thu, 15 Sep 2025 13:22:33 GMT 6Server: Apache/2.2.3 (CentOS) 7Last-Modified: Thu, 15 Sep 2025 13:22:18 GMT 8Content-Length: <number of characters> 9Content-Type: application/json 10//Body 11{ 12 data... 13}
HTTP methods like GET, PUT, and DELETE are expected to be idempotent. This means making the same request multiple times has the same effect as making it once, a critical property for building fault-tolerant systems that may need to retry failed requests.
Table
| Version | Methods Supported | Support for headers | Connection Nature | Other Features |
|---|---|---|---|---|
| HTTP/0.9 | GET only | No | Closed after response | Only supports hypertext |
| HTTP/1.0 | HEAD, GET, POST | Yes | Closed after response | Supports hypertext, hypermedia, and files |
| HTTP/1.1 | HEAD, GET, POST, PUT, DELETE, TRACE, OPTIONS | Yes | Persistent | Extended methods, pipelining, encoding, virtual hosting, upgrade header, cache headers |
| HTTP/2.0 | HEAD, GET, POST, PUT, DELETE, TRACE, OPTIONS | Yes | Persistent | Multiplexing, header compression, backward compatibility |
| HTTP/3.0 | HEAD, GET, POST, PUT, DELETE, TRACE, OPTIONS | Yes | Persistent | Built on QUIC (over UDP), avoids head-of-line blocking, ~3× faster than HTTP/1.1 |
Table
| Feature | Transmission Control Protocol (TCP) | User Datagram Protocol (UDP) |
|---|---|---|
| Reliability | High, guaranteed delivery | Low, best-effort delivery |
| Connection | Connection-oriented (three-way handshake) | Connectionless |
| Ordering | Guarantees that packets are delivered in order | No ordering guarantees |
| Speed | Slower, due to overhead from acknowledgments and ordering | Faster, with minimal overhead |
| Header size | 20 bytes minimum (can be larger with options) | 8 bytes (fixed) |
| Common use cases | Web (HTTPS), file transfers (FTP), email (SMTP) | Streaming, online gaming, DNS, VoIP |
The OSI Model is a conceptual framework that organizes network communication into seven layers. It provides a common language for understanding and troubleshooting networks, but it is rarely implemented directly in real-world systems. It consists of the following seven layers:
- Application: Defines how applications communicate (HTTP, FTP, SMTP).
- Presentation: Translates data formats, handles compression and encryption (SSL/TLS).
- Session: Manages sessions and dialog control between systems.
- Transport: Ensures reliable or fast delivery (TCP, UDP).
- Network: Handles addressing and routing (IP).
- Data Link: Transfers data on the same local network (Ethernet, Wi-Fi).
- Physical: The hardware signals and media (fiber, copper, radio).
The OSI Model is a conceptual framework that organizes network communication into seven layers. It provides a common language for understanding and troubleshooting networks, but it is rarely implemented directly in real-world systems. It consists of the following seven layers:
- Application: Defines how applications communicate (HTTP, FTP, SMTP).
- Presentation: Translates data formats, handles compression and encryption (SSL/TLS).
- Session: Manages sessions and dialog control between systems.
- Transport: Ensures reliable or fast delivery (TCP, UDP).
- Network: Handles addressing and routing (IP).
- Data Link: Transfers data on the same local network (Ethernet, Wi-Fi).
- Physical: The hardware signals and media (fiber, copper, radio).
Table
| TCP/IP Layer | Common Protocols | System Design Relevance |
|---|---|---|
| Application | HTTP, gRPC, WebSocket | API design, service communication patterns |
| Transport | TCP, UDP | The core trade-off between reliability and speed |
| Internet | IP, ICMP | Understanding network topology and routing |
| Link | Ethernet, Wi-Fi | Handled by network infrastructure |