Explain http keep-alive mechanism

good_evening

Keep-alives were added to HTTP to basically reduce the significant overhead of rapidly creating and closing socket connections for each new request. The following is a summary of how it works within HTTP 1.0 and 1.1:

HTTP 1.0 The HTTP 1.0 specification does not really delve into how Keep-Alive should work. Basically, browsers that support Keep-Alive appended an additional header to the request as [edited for clarity] explained below:

When the server processes the request and generates a response, it also adds a header to the response:

Connection: Keep-Alive

When this is done, the socket connection is not closed as before, but kept open after sending the response. When the client sends another request, it reuses the same connection. The connection will continue to be reused until either the client or the server decides that the conversation is over, and one of them drops the connection.

The above explanation comes from here. But I don't understand one thing

When this is done, the socket connection is not closed as before, but kept open after sending the response.

As I understand we just send tcp packets to make requests and responses, how this socket connection helps and how does it work? We still have to send packets, but how can it somehow establish the persistent connection? It seems so unreal.

Remy Lebeau

There is overhead in establishing a new TCP connection (DNS lookups, TCP handshake, SSL/TLS handshake, etc). Without a keep-alive, every HTTP request has to establish a new TCP connection, and then close the connection once the response has been sent/received. A keep-alive allows an existing TCP connection to be re-used for multiple requests/responses, thus avoiding all of that overhead. That is what makes the connection "persistent".

In HTTP 0.9 and 1.0, by default the server closes its end of a TCP connection after sending a response to a client. The client must close its end of the TCP connection after receiving the response. In HTTP 1.0 (but not in 0.9), a client can explicitly ask the server not to close its end of the connection by including a Connection: keep-alive header in the request. If the server agrees, it includes a Connection: keep-alive header in the response, and does not close its end of the connection. The client may then re-use the same TCP connection to send its next request.

In HTTP 1.1, keep-alive is the default behavior, unless the client explicitly asks the server to close the connection by including a Connection: close header in its request, or the server decides to includes a Connection: close header in its response.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

HTTP Keep-Alive 到不同的主机

来自分类Dev

HTTP Keep-Alive是否可以维持长时间的轮询?

来自分类Dev

带有HTTPS,SSL客户端证书和Keep-Alive支持的Ruby的HTTP库?

来自分类Dev

如何查看HTTP服务器的Keep-alive超时时间?

来自分类Dev

您可以通过HTTP代理使用CONNECT请求的Keep-Alive吗?

来自分类Dev

Connection:keep-alive没有在NodeJS中保持HTTP请求的套接字连接

来自分类Dev

websocket与带有header connection-keep-alive = million的http有何不同

来自分类Dev

处理http Keep-Alive连接时,如何使用netty将响应映射到请求url

来自分类Dev

在C#(.net)下具有HTTP服务器的Keep-Alive套接字如何发送几个查询?

来自分类Dev

由于缺少Keep-Alive标头而导致HTTP 2上的Socket.io永久断开连接

来自分类Dev

Keep function namespace alive for debugging in IPython

来自分类Dev

python keep-alive连接并下载图片

来自分类Dev

支持CoAP的Keep-alive功能

来自分类Dev

HAProxy Keep-Alive无法正常工作

来自分类Dev

Vuejs <keep-alive>有什么意义?

来自分类Dev

PoolingHttpClientConnectionManager 和 keep-alive 设置

来自分类Dev

尝试重用HTTP连接来处理Keep-Alive标头时,通过从客户端读取第二个请求来阻止recv

来自分类Dev

建立httpheader连接:保留小写的“ keep-alive”

来自分类Dev

Python请求使用Keep-alive加快速度

来自分类Dev

建立httpheader连接:保留小写的“ keep-alive”

来自分类Dev

jQuery keep Alive /任何客户端交互?

来自分类Dev

PHP curl Keep-Alive - CURLOPT_FORBID_REUSE

来自分类Dev

Keep HTTP or HTTPS on .htaccess

来自分类常见问题

如何在docker image中禁用SpringBoot应用程序中的Keep alive

来自分类Dev

如何通过HttpClient以小写形式设置“ Connection:keep-alive”标头?

来自分类Dev

Firefox Keep-Alive,升级通过Apache反向代理破坏了Websocket

来自分类Dev

如何在配置了Apache-Nginx的Web服务器中启用Keep-Alive [DirectAdmin]

来自分类Dev

如何在Codename One中的ConnectionRequest中使用keep-alive?

来自分类Dev

VueJS和Keep-Alive:为什么控制台出错?

Related 相关文章

  1. 1

    HTTP Keep-Alive 到不同的主机

  2. 2

    HTTP Keep-Alive是否可以维持长时间的轮询?

  3. 3

    带有HTTPS,SSL客户端证书和Keep-Alive支持的Ruby的HTTP库?

  4. 4

    如何查看HTTP服务器的Keep-alive超时时间?

  5. 5

    您可以通过HTTP代理使用CONNECT请求的Keep-Alive吗?

  6. 6

    Connection:keep-alive没有在NodeJS中保持HTTP请求的套接字连接

  7. 7

    websocket与带有header connection-keep-alive = million的http有何不同

  8. 8

    处理http Keep-Alive连接时,如何使用netty将响应映射到请求url

  9. 9

    在C#(.net)下具有HTTP服务器的Keep-Alive套接字如何发送几个查询?

  10. 10

    由于缺少Keep-Alive标头而导致HTTP 2上的Socket.io永久断开连接

  11. 11

    Keep function namespace alive for debugging in IPython

  12. 12

    python keep-alive连接并下载图片

  13. 13

    支持CoAP的Keep-alive功能

  14. 14

    HAProxy Keep-Alive无法正常工作

  15. 15

    Vuejs <keep-alive>有什么意义?

  16. 16

    PoolingHttpClientConnectionManager 和 keep-alive 设置

  17. 17

    尝试重用HTTP连接来处理Keep-Alive标头时,通过从客户端读取第二个请求来阻止recv

  18. 18

    建立httpheader连接:保留小写的“ keep-alive”

  19. 19

    Python请求使用Keep-alive加快速度

  20. 20

    建立httpheader连接:保留小写的“ keep-alive”

  21. 21

    jQuery keep Alive /任何客户端交互?

  22. 22

    PHP curl Keep-Alive - CURLOPT_FORBID_REUSE

  23. 23

    Keep HTTP or HTTPS on .htaccess

  24. 24

    如何在docker image中禁用SpringBoot应用程序中的Keep alive

  25. 25

    如何通过HttpClient以小写形式设置“ Connection:keep-alive”标头?

  26. 26

    Firefox Keep-Alive,升级通过Apache反向代理破坏了Websocket

  27. 27

    如何在配置了Apache-Nginx的Web服务器中启用Keep-Alive [DirectAdmin]

  28. 28

    如何在Codename One中的ConnectionRequest中使用keep-alive?

  29. 29

    VueJS和Keep-Alive:为什么控制台出错?

热门标签

归档