问题原文
原帖链接 · user2250246 · CC BY-SA 4.0
Why is creating a new TCP connection regarded as expensive? was the question I had in mind when another one came to my mind.
What exactly is meant by keeping a TCP connection alive and what is meant by caching a connection through connection pools? (I do understand thread pools concept but confused with "connection" part of connection pools).
Isn't the TCP traffic supposed to flow through a number of Internet-based IPs (computers and routers etc) before it reaches the destination? If that is so, then what does a connection even mean? Does it refer to the path established once for TCP traffic and re-using the connection means that the same path is used again and again until some IPs in that path are not available anymore?
已采纳解答
原帖链接 · Tero Kilkanen · CC BY-SA 4.0
TCP connection is initialised by a three-way handshake, where both ends of the connection agree on sequence numbers they are using for packets they send.
When three-way handshake is finished, the connection is in ESTABLISHED state, which means that data can flow bi-directionally.
The individual TCP segments in the connection can take different paths between the sender and receiver. All TCP cares about is that both ends have the same idea about connection state.
Keepling TCP connection alive means that the connection is kept in ESTABLISHED state, even though there is no immediate need to transmit any data via the connection. Web servers use connection pools to manage these open connections to clients.
A practical example of keeping TCP connection open. A website visitor loads https://www.example.com.
- The browser opens a TCP connection to https://www.example.com port 443.
- The browser does TLS handshake with the web server.
- The browser sends
GET /request to the web server and server sends response page back. - The browser leaves TCP connection open with the web server.
Visitor then browses through the page and clicks on https://www.example.com/example link.
- Now browser can send
GET /examplerequest directly to the web server, re-using the existing TCP connection and TLS transport.
If the web server / client did not keep TCP connection open, browser would have to repeat steps 1 and 2 for the second HTTPS request.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
