{ "topic": "http-connection-cost", "type": "single_choice", "schema_version": "1.0.0", "generated": "2026-09-02T21:20:00+08:00", "questions": [ { "id": "sc-001", "type": "single_choice", "difficulty": 2, "tags": [ "TCP", "内存", "内核" ], "question": "操作系统为每个 TCP 连接分配的内核内存大约是多少?", "options": { "A": "300-1000 KB", "B": "3-10 KB", "C": "1-2 KB", "D": "50-100 KB" }, "answer": "B", "explanation": "操作系统为每个 TCP 连接分配的内核内存大约是 3-10 KB,包括发送/接收缓冲区和 TCP 控制块。50-100 KB 是 HTTPS 场景下 TLS 会话的额外消耗。", "source": null, "related": [] }, { "id": "sc-002", "type": "single_choice", "difficulty": 3, "tags": [ "线程模型", "协程模型" ], "question": "传统线程模型(Java BIO)每连接的内存消耗大约是多少?", "options": { "A": "约 4 KB", "B": "约 10 KB", "C": "约 1 MB", "D": "约 10 MB" }, "answer": "C", "explanation": "传统线程模型每连接约 1 MB(主要是线程栈),协程模型(Go/async)仅需约 4 KB,异步事件驱动(Nginx)约 2 KB。", "source": null, "related": [] }, { "id": "sc-003", "type": "single_choice", "difficulty": 4, "tags": [ "百万并发", "内存" ], "question": "百万并发场景下,使用传统线程模型需要多少内存?", "options": { "A": "约 10 GB", "B": "约 50 GB", "C": "约 1 TB", "D": "约 4 GB" }, "answer": "C", "explanation": "传统线程模型每连接约 1 MB,百万并发需要约 1 TB 内存,这在物理上不可行。协程模型约 4 GB 是可行的。", "source": null, "related": [] }, { "id": "sc-004", "type": "single_choice", "difficulty": 2, "tags": [ "TCP", "端口" ], "question": "客户端临时端口的理论上限是多少?", "options": { "A": "1024", "B": "32768", "C": "65535", "D": "1048576" }, "answer": "C", "explanation": "客户端端口是 16 位整数,理论上限 65535。每个 TCP 连接需要一个唯一的四元组(源IP:源端口:目标IP:目标端口)。", "source": null, "related": [] }, { "id": "sc-005", "type": "single_choice", "difficulty": 3, "tags": [ "Go", "goroutine", "协程" ], "question": "Go goroutine 的初始栈大小是多少?", "options": { "A": "1 KB", "B": "2 KB", "C": "4 KB", "D": "8 KB" }, "answer": "B", "explanation": "Go goroutine 初始栈大小为 2 KB(可动态增长到 1 GB),Java 线程默认栈大小为 1 MB。相同内存下 Go 能创建的并发单元数量是 Java 的约 500 倍。", "source": null, "related": [] }, { "id": "sc-006", "type": "single_choice", "difficulty": 3, "tags": [ "TLS", "HTTPS", "内存" ], "question": "HTTPS 场景下 TLS 会话缓存每个连接额外消耗多少内存?", "options": { "A": "3-10 KB", "B": "50-100 KB", "C": "200-500 KB", "D": "1-2 MB" }, "answer": "B", "explanation": "HTTPS 场景下 TLS 会话缓存每个连接额外消耗 50-100 KB 内存,百万并发下可能吃掉 50 GB 内存。因此考虑在 CDN 边缘终止 TLS。", "source": null, "related": [] }, { "id": "sc-007", "type": "single_choice", "difficulty": 2, "tags": [ "文件描述符", "ulimit" ], "question": "每个 TCP 连接占多少个文件描述符?", "options": { "A": "0 个", "B": "1 个", "C": "2 个", "D": "4 个" }, "answer": "B", "explanation": "每个 TCP 连接占 1 个文件描述符(fd),受系统 ulimit 限制。百万并发需要调大 ulimit 到至少 1048576。", "source": null, "related": [] }, { "id": "sc-008", "type": "single_choice", "difficulty": 4, "tags": [ "异步模型", "Nginx", "事件驱动" ], "question": "异步事件驱动模型(如 Nginx)每连接的内存消耗大约是多少?", "options": { "A": "约 1 MB", "B": "约 10 KB", "C": "约 4 KB", "D": "约 2 KB" }, "answer": "D", "explanation": "异步事件驱动模型(Nginx)每连接约 2 KB,几乎不占位置。这是最高效的连接模型,但编程复杂度最高。", "source": null, "related": [] }, { "id": "sc-009", "type": "single_choice", "difficulty": 3, "tags": [ "Epoll", "Kqueue", "IO多路复用" ], "question": "撑住百万并发的关键手段中,Epoll/Kqueue 的作用是什么?", "options": { "A": "为每个连接开一个线程", "B": "不为每个连接开线程,一个线程监控百万 fd", "C": "加密所有连接", "D": "压缩数据传输" }, "answer": "B", "explanation": "Epoll/Kqueue 是 IO 多路复用机制,不为每个连接开线程,一个线程就能监控百万文件描述符,是撑住百万并发的关键手段之一。", "source": null, "related": [] }, { "id": "sc-010", "type": "single_choice", "difficulty": 2, "tags": [ "TCP", "资源" ], "question": "TCP 连接的四元组中不包括以下哪项?", "options": { "A": "源 IP", "B": "源端口", "C": "协议号", "D": "目标端口" }, "answer": "C", "explanation": "TCP 连接的四元组是:源 IP、源端口、目标 IP、目标端口。协议号(TCP=6)是固定的,不属于四元组。", "source": null, "related": [] } ] }