简体   繁体   English

通过 HTTP2 批处理请求

[英]Batching requests over HTTP2

Is it possible to get a better throughput from our servers, if we make one large HTTP request, as opposed to multiple smaller HTTP requests over HTTP2.如果我们发出一个大型 HTTP 请求,而不是通过 HTTP2 发出多个较小的 HTTP 请求,是否有可能从我们的服务器获得更好的吞吐量?

As per my understanding it should not produce any significant difference in performance since with HTTP2 we can have multiple requests multiplexed over one TCP connection.根据我的理解,它不会对性能产生任何显着差异,因为使用 HTTP2,我们可以在一个 TCP 连接上多路复用多个请求。

Yes at a network level one large request will be more efficient than multiple small requests.是的,在网络级别,一个大请求比多个小请求更有效。 This is due to the overhead of making a network request.这是由于发出网络请求的开销。

This is also why concatenating CSS and JavaScript and spriting for images were recommended under HTTP/1.1 so the amount of data sent was the same, but the amount of requests was considerably lower.这也是为什么在 HTTP/1.1 下推荐将 CSS 和 JavaScript 连接并为图像进行 spriting 的原因,因此发送的数据量相同,但请求量要低得多。 In fact due to the way compression like gzip works the amount of data was often smaller when sending large requests.事实上,由于像 gzip 这样的压缩方式,发送大请求时数据量通常较小。

HTTP/2 was designed to make the costs of HTTP requests a lot smaller though the reuse of a single TCP connection using multiplexing . HTTP/2 旨在通过使用多路复用重用单个 TCP 连接来降低 HTTP 请求的成本。 In theory this would allow us to give up concatenation and spriting.理论上,这将允许我们放弃串联和精灵。 The reality has been a little less than perfect though - usually due to browser inefficiencies rather than HTTP/2's fault.然而,现实情况并不完美——通常是由于浏览器效率低下而不是 HTTP/2 的错误。 The bottleneck has just moved and we need to optimise browsers for the new world.瓶颈刚刚转移,我们需要为新世界优化浏览器。 So, for now, some level of concenation and spriting is still recommended.所以,就目前而言,仍然推荐一定程度的专注和精灵。

Getting back to your question, then yes it should have single effects at that network level and in fact HTTP/1.1 and HTTP/2 may even be similar in performance if you do this.回到您的问题,是的,它应该在该网络级别具有单一效果,事实上,如果您这样做,HTTP/1.1 和 HTTP/2 的性能甚至可能相似。

However beyond the network level you may discover other reasons not to bundle into fewer files.然而,在网络级别之外,您可能会发现不捆绑到更少文件中的其他原因。 If you have one large JavaScript file for example then the browser must wait for it all to be downloaded before it can be parsed, compiled and run.例如,如果您有一个大型 JavaScript 文件,那么浏览器必须等待所有文件下载完毕,然后才能对其进行解析、编译和运行。 You may be better getting smaller, more important JavaScript downloaded first.您最好先下载更小、更重要的 JavaScript。 Similarly with image spriting you may be waiting for the entire sprite file to download before a single image is display.与图像精灵类似,您可能会在显示单个图像之前等待整个精灵文件下载。

Then there are the caching implications.然后是缓存的影响。 Changing a single line of JS or adding a single image to the image Sprite requires creating a whole new large file, meaning the older one cannot be used and the whole thing needs to be downloaded again in its entirety.更改一行 JS 或将单个图像添加到图像 Sprite 需要创建一个全新的大文件,这意味着不能使用旧文件,需要重新下载整个文件。

Plus having large files can be more complicated to implemented and managed.此外,大文件的实施和管理可能会更加复杂。 They require a build step (maybe not a big deal as many sites do) and creating and managing image sprites through CSS is often more difficult.它们需要一个构建步骤(可能不像许多站点那样重要)并且通过 CSS 创建和管理图像精灵通常更加困难。

Also if using this to stick with HTTP/1.1, then you may be missing out on are the other benefits of HTTP/2 including HPACK header compression and HTTP/2 push (though this is also more tricky to get right than initially thought/hoped!).此外,如果使用这种坚持使用HTTP / 1.1,那么你可能会错过了是HTTP / 2的其他好处,包括HPACK报头压缩和HTTP / 2推(虽然这也 更棘手得到的权利比最初认为/希望!)。

It's a fascinating topic that I've spent a lot of time on , and best advice (as always!) is to understand the technology and test, test, test!这是一个有趣的话题,我花了很多时间,最好的建议(一如既往!)是了解技术并测试、测试、再测试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM