走https要100ms

走https要100ms

事件背景

开发同学咨询容器内部服务之间请求的时候走域名和走ip+端口之间速度差不少,用curl测试两者相差有150ms多,如下图:

HTTPS 在 HTTP 与 TCP 层之间加入了 TLS 协议,ip+端口(http)比https少一个SSL

走公网请求多100ms差不多属于合理范围,但是容器直接请求访问的域名是在IDC内部的,这个100ms延迟有些“不可思议”,更奇怪的在其他容器里面访问同样的请求这个时间只有20+ms,需要排查一下。走起!~

排查问题

开发同学用的curl命令打印的访问连接信息比较少,改一下配置,如下:

#curl-format.txt
time_namelookup 解析: %{time_namelookup}\n
time_connect : %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
----------\n
time_total: %{time_total}\n
content_type: %{content_type}\n
filename_effective: %{filename_effective}\n
http_code: %{http_code}\n
ftp_entry_path: %{ftp_entry_path}\n
http_connect: %{http_connect}\n
local_ip: %{local_ip}\n
local_port: %{local_port}\n
num_connects: %{num_connects}\n
num_redirects: %{num_redirects}\n
redirect_url: %{redirect_url}\n
remote_ip: %{remote_ip}\n
remote_port: %{remote_port}\n
size_download: %{size_download}\n
size_header: %{size_header}\n
size_request: %{size_request}\n
size_upload: %{size_upload}\n
speed_download: %{speed_download}\n
speed_upload: %{speed_upload}\n
ssl_verify_result: %{ssl_verify_result}\n
url_effective: %{url_effective}\n

在容器里面重新请求一下,顺便看一下请求慢的容器系统信息。

time_appconnect 146ms
time_appconnect 14ms

能看到容器镜像用centos7的发起https请求时time_appconnect(SSL/SSH 等上层协议建立连接的时间)耗时146ms,alpine v3.14(内核版本更新一些)的只需要14ms。怀疑会不会是这个镜像的版本导致的,在容器中不是很好排查,找系统版本相近的两个物理机发起请求看看。

在物理机上系统版本centos7的time_appconnect也是100ms多,下面看一下ssl的详细信息。

在centos7发起请求, SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,查询加密算法找到对应的TLS协议是TLSv.12,如下:

https://developers.cloudflare.com/ssl/reference/cipher-suites/supported-cipher-suites

在Rocky Linux release 8.8 发起请求,SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384,走的TLSv1.3协议。

TLS的不同

https://xiaolincoding.com/network/2_http/https_optimize.html#密钥交换算法优化

TLS 1.2 和 TLS 1.3的区别:

TLS 1.2 版本如果使用的是 RSA 密钥交换算法,那么需要 4 次握手,也就是要花费 2 RT;

TLS 1.3 大幅度简化了握手的步骤,完成 TLS 握手只要 1 RTT;

OpenSSL的版本也不同,centos7默认的还是1.0.2,各种文档也建议升级一下,新版本不仅提供了最新的特性,也优化了性能,速度上会有提升。

写在最后

服务的基础镜像从centos7换成centos8后走https请求的时间恢复了正常。这块慢其实是首次建立https慢,服务中使用连接池的话,整体的P95时间也在可接受范围之内,替换了基础镜像后服务的访问时间有一些提升,稳定性也没有变差。

留下回复

error: Content is protected !!