Skip to content

Commit

Permalink
docs: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Feb 16, 2024
1 parent a7634c2 commit f4502ee
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 38 deletions.
34 changes: 17 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/en/guide/concepts/database/1-database-basic.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
order: 1
title: "Database basic"
title: "Basic"
---

## Relationship Keys
Expand Down
14 changes: 7 additions & 7 deletions src/en/guide/concepts/network/1-network.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
order: 1
title: "Computer Network"
title: "Basic"
---

### OSI Seven-Layer Model
Expand Down Expand Up @@ -137,12 +137,12 @@ The `HTTPS` protocol will encrypt the transmitted data, and the encryption proce
### HTTP 1.0 vs 1.1 vs 2.0

::: tip Differences
| Feature | HTTP 1.0 | HTTP 1.1 | HTTP 2.0 |
|--------|-----------------------------|------------------------------------|-----------------------------------|
| Connection | Non-persistent, each request requires a new connection | Persistent, reduces the overhead of repeated TCP connection establishment and termination | Multiplexing, multiple HTTP requests can be concurrent on a single TCP connection |
| Head-of-line blocking | Exists, the next request can only be sent after the response of the previous request arrives | Exists, although multiple requests can be initiated, the server must send responses in the order of received requests | Solved, multiple requests or responses can be concurrent in a single connection without having to correspond one by one |
| Header compression | Not supported | Not supported | Supported, uses HPACK algorithm to compress headers |
| Server push | Not supported | Not supported | Supported |
| Feature | HTTP 1.0 | HTTP 1.1 | HTTP 2.0 |
| --------------------- | -------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| Connection | Non-persistent, each request requires a new connection | Persistent, reduces the overhead of repeated TCP connection establishment and termination | Multiplexing, multiple HTTP requests can be concurrent on a single TCP connection |
| Head-of-line blocking | Exists, the next request can only be sent after the response of the previous request arrives | Exists, although multiple requests can be initiated, the server must send responses in the order of received requests | Solved, multiple requests or responses can be concurrent in a single connection without having to correspond one by one |
| Header compression | Not supported | Not supported | Supported, uses HPACK algorithm to compress headers |
| Server push | Not supported | Not supported | Supported |
:::

### Digital Certificate
Expand Down
54 changes: 48 additions & 6 deletions src/en/guide/concepts/network/2-tcp-udp.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The process of TCP establishing a connection is the process of synchronizing seq
The reason why a four-way handshake is needed is because TCP is a **full-duplex** protocol, i.e., both the client and the server can actively send messages, so both ends need to send disconnect instructions after the transmission is completed, and need to send `FIN=1` separately to disconnect, and use `ACK` to determine whether the sending was successful.
:::

::: danger What happens if SYN times out during connection?
::: caution What happens if SYN times out during connection?

::: details
If the client sends a `SYN` command, and drops the line before the server returns the `SYN` command, the server will try to resend the `SYN-ACK` command. Under Linux, the default is to retry 5 times, the interval time starts to double from 1s, i.e., `1s, 2s, 4s, 8s, 16s`, so the timeout time is `1s + 2s + 4s+ 8s+ 16s + 32s = 63s`. After the timeout, TCP will disconnect.
Expand All @@ -85,7 +85,7 @@ If the client sends a `SYN` command, and drops the line before the server return

TCP uses sequence numbers to sort and deduplicate packets, and through the ACK acknowledgment mechanism, it ensures that packets are successfully delivered, ensuring the integrity of the data.

#### Timeout retransmission
##### Timeout retransmission

TCP can retransmit packets until an ACK acknowledgment is received when packets are lost or delayed, through the timeout retransmission mechanism.

Expand Down Expand Up @@ -137,12 +137,54 @@ The SACK method ([RFC 2018](https://www.ietf.org/rfc/rfc2883.txt)) adds a buffer
D-SACK ([RFC 2883](https://datatracker.ietf.org/doc/html/rfc2018)) mainly solves the problem of ACK loss. D-SACK uses the first segment of SACK as a flag to mark the packets that have been ACKed. When the receiving end receives a duplicate packet, it will write the packet into the D-SACK flag, telling the sender that the packet has been received, and the ACK may have been lost.
:::

#### Checksum

TCP also calculates a checksum on the data to ensure that the data was not lost or corrupted during transmission.

### UDP

### Common interview questions
UDP, short for User Datagram Protocol, is a connectionless transport layer protocol in the OSI (Open Systems Interconnection) reference model. It provides a simple, unreliable transaction-oriented message delivery service, and its official specification is `IETF RFC 768`.

::: tip What is a SYN Flood attack?
#### Characteristics

::: details
- Connectionless: UDP does not need to establish a connection between the client and the server before transmitting data.
- Fast: Since UDP does not need to handshake or check whether the data has arrived correctly, it can transmit data faster than TCP.
- Unreliability: Once the datagram is sent, it is impossible to know whether it has arrived safely and completely. If the UDP datagram is lost during transmission, it will not be resent.
- Widely used: UDP is used to support network applications that need to transmit data between computers. Many client/server model network applications, including network video conferencing systems, need to use the UDP protocol. For example, many Internet phone services use IP voice (VoIP) usually sent using UDP.

#### Structure

| Field | Size | Description |
| ---------------- | ------ | --------------------------------------------------------------------- |
| Source Port | 16-bit | Source port number. If all are 0, no reply is allowed |
| Destination Port | 16-bit | Destination port number |
| Length | 16-bit | Length of the UDP user datagram, its minimum value is 8 (header only) |
| Checksum | 16-bit | Detect whether the data is lost or modified during transmission |

#### Broadcast Types

:::
| Type | Description |
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Unicast | Used for end-to-end communication between two hosts. That is, one-to-one (client and server point-to-point connection) |
| Broadcast | Used for a host to communicate with all hosts on the entire LAN. That is, one to all. Broadcasting is prohibited from transmitting on the Internet broadband network (broadcast storm) |
| Multicast | Communicate with a specific group of hosts, not all hosts on the entire LAN. That is, one to a group |

::: warning
- TCP only supports one-to-one
- UDP multicast is more commonly used, broadcast is only used for LAN
:::

::: info Difference between TCP and UDP

| Feature | TCP | UDP |
| ----------------------------------- | ------------------- | -------------- |
| Reliable Transmission | Yes | No |
| Connection | Connection-oriented | Connectionless |
| Data Orderliness | Yes | Not guaranteed |
| Data Boundary | Not preserved | Preserved |
| Transmission Speed | Relatively slow | Fast |
| Flow Control and Congestion Control | Yes | No |
| Protocol Type | Heavyweight | Lightweight |
| Header Length | 20 bytes | 8 bytes |

:::
2 changes: 1 addition & 1 deletion src/guide/concepts/database/1-database-basic.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
order: 1
title: "数据库基础"
title: "基础"
---

## 关系键
Expand Down
2 changes: 1 addition & 1 deletion src/guide/concepts/network/1-network.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
order: 1
title: "计算机网络基础"
title: "基础"
---

### OSI七层模型
Expand Down
85 changes: 80 additions & 5 deletions src/guide/concepts/network/2-tcp-udp.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TCP 建立连接的过程就是同步序列号的过程,SYN (Synchronize Seque
之所以需要四次挥手,是因为 tcp 是**全双工**协议,即客户端和服务端都可以主动发送消息,因此需要两端分别在传输完成后发送断开连接的指令,需要分别发送 `FIN=1` 指令断开,通过 `ACK` 判断是否发送成功。
:::

::: danger 如果连接时 SYN 超时会发生什么?
::: caution 如果连接时 SYN 超时会发生什么?

::: details
假如客户端发送 `SYN` 指令,在服务器返回 `SYN` 指令之前掉线了,服务器会尝试重发 `SYN-ACK` 指令,linux 下默认重试 5 次,间隔时间从 1s 开始翻倍增长,即 `1s, 2s, 4s, 8s, 16s`, 因此超时时间为 `1s + 2s + 4s+ 8s+ 16s + 32s = 63s`。在超时之后 TCP 才会断开连接。
Expand Down Expand Up @@ -151,21 +151,96 @@ TCP 使用滑动窗口来控制流量,使得发送端可以根据接收端的
#### 拥塞控制

::: tip 算法
- 慢启动
- 慢开始
- 拥塞避免
- 快重传
- 快恢复
:::

![拥塞控制](/assets/image/article/network/tcp-congestion-control.png)

::: info 相关名词
- cwnd - 拥塞窗口
- ssthresh - 慢开始门限
:::

::: important

- `cwnd < ssthresh`,使用慢开始算法。

- `cwnd > ssthresh`,使用拥塞避免算法。

- `cwnd = ssthresh`,使用慢开始与拥塞避免算法任意一个。
:::

##### 慢开始

在慢开始阶段, `cwnd` 初始值为 `1`, 每经过一个传播轮次,`cwnd` 都会翻倍,直到达到 `ssthresh`

##### 拥塞避免

`cwnd` 达到 `ssthresh` ,则会执行拥塞避免算法,此时 `cwnd` 的增长从翻倍增长变为每经过一个传播轮次就加 `1` 。若发送方检测到网络拥塞(即发送的消息没得到及时的回应),就会将 `ssthresh` 设置为发生网络拥塞时的 `cwnd` 值的一半,同时 `cwnd` 的值将会重置为 `1` ,重新执行慢开始。

##### 快重传和快恢复

快重传在前面已介绍过,即如果连续收到三个重复的确认就会立即发送尚未接收到的报文段。快恢复算法需要配合快重传算法使用。图中的 5 号为快恢复阶段。

::: info 快恢复算法
- 当发送端连续收到三个重复的确认时,将 `ssthresh` 减半。
-`cwnd` 设置为 `ssthresh` 的大小
:::

#### 校验和

TCP 还会对数据计算校验和,确保数据在传输过程中未丢失或出现错误。

### UDP

### 常见面试题
UDP,全称用户数据报协议(User Datagram Protocol),是OSI(开放系统互联)参考模型中的一种无连接的传输层协议。它提供面向事务的简单不可靠信息传送服务,其正式规范是 `IETF RFC 768`

::: tip 什么是 SYN Flood 攻击?
#### 特点

::: details
- 无连接:UDP在传输数据前不需要在客户和服务器之间建立一个连接。
- 速度快:由于UDP不需要进行握手或检查数据是否正确到达,所以它能够比TCP更快地传输数据。
- 不可靠性:当报文发送之后,是无法得知其是否安全完整到达的。如果UDP数据报在传输过程中丢失,它不会重新发送。
- 应用广泛:UDP用来支持那些需要在计算机之间传输数据的网络应用。包括网络视频会议系统在内的众多的客户/服务器模式的网络应用都需要使用UDP协议。例如,许多互联网电话服务使用的IP语音 (VoIP) 通常是使用UDP发送的。

#### 结构

![udp](/assets/image/article/network/udp.png)

| 字段 | 大小 | 描述 |
| -------- | ---- | ----------------------------------------- |
| 源端口 | 16位 | 源端口号。若全为 0 则意味着不允许回信 |
| 目的端口 | 16位 | 目标端口号 |
| 长度 | 16位 | UDP用户数据报的长度,其最小值是8(仅有首部) |
| 检验和 | 16位 | 检测数据在传输过程中是否丢失或修改 |

#### 广播类型

| 类型 | 描述 |
| ------------ | ---------------------------------------------------------------------------------------------- |
| 单播 | 用于两个主机之间端对端的通信。即一对一(客户端与服务器端点到点连接) |
| 广播 | 用于一个主机对整个局域网上所有主机通信。即一对所有。广播禁止在Internet宽带网上传输(广播风暴) |
| 组播(多播) | 对一组特定的主机进行通信,而不是整个局域网上的所有主机。即一对一组 |

::: warning
- TCP 只支持一对一
- UDP 多播较为常用,广播仅用于局域网
:::

::: info TCP 和 UDP 区别

| 特性 | TCP | UDP |
| ------------------ | -------- | ------ |
| 可靠传输 |||
| 连接 | 面向连接 | 无连接 |
| 数据有序性 || 不保证 |
| 数据边界 | 不保存 | 保留 |
| 传输速度 | 相对慢 ||
| 流量控制和拥塞控制 || 没有 |
| 协议类型 | 重量级 | 轻量级 |
| 首部长度 | 20字节 | 8字节 |

:::

0 comments on commit f4502ee

Please sign in to comment.