简体   繁体   English

通过异步http提高吞吐量

[英]Enhancing throughput with async http

In a java web-app I would like to maximize my server throughput with the following requirement: 在一个Java Web应用程序中,我希望通过以下要求最大化服务器的吞吐量:

  • A system that gets multiple concurrent http requests. 一个获取多个并发http请求的系统。
  • For each request some processing is required. 对于每个请求,都需要进行一些处理。
  • Each request processing is consisted of a few steps. 每个请求处理都由几个步骤组成。
  • The last step calls an external API (http invocation). 最后一步调用外部API(http调用)。

Here is a basic diagram describing my system: 这是描述我的系统的基本图:

在此处输入图片说明

So for each request I'm: 因此,对于每个请求,我是:

  • Creating 3 runnables and submitting (Using my executor). 创建3个可运行对象并提交(使用我的执行器)。
  • Then I'm waiting for all 3 to finish and keep on processing the results (doing it using Guava ListenableFuture ) 然后,我等待所有三个完成并继续处理结果(使用Guava ListenableFuture进行
  • When done processing all results returning response 处理完所有结果后返回响应

In this design I'm using a synchronous http client (when invoking the external services) meaning that each of my tasks is blocked once it gets to the 3rd step untill the http response arrives back. 在此设计中,我使用的是同步http客户端(在调用外部服务时),这意味着一旦进入第三步,我的每个任务都会被阻塞,直到http响应返回为止。 This could take up to a few hundreds of milliseconds. 这可能需要数百毫秒。

My question is - I wonder if using an asynchronous http client can help me increase the throughput? 我的问题是-我想知道使用异步http客户端是否可以帮助我提高吞吐量? With an asynchronous http client once I launch my http request the thread of the task is released back to the pool and available for additional processing and once the http response is back a new thread is assigned to continue the task. 使用异步http客户端,一旦启动我的http请求,该任务的线程就会释放回池中,并可以进行其他处理,一旦http响应返回,就会分配一个新线程来继续执行任务。 Does it make sense? 是否有意义? If so, Does it depend upon the time it takes for the http external invocations to finish? 如果是这样,这是否取决于http外部调用完成所需的时间? If the response arrives back after 5ms will I still gain additional throughput? 如果5毫秒后响应返回,我是否还会获得额外的吞吐量? Has anyone measured something similar? 有人测量过类似的东西吗?

Assuming that by asynchronous client you do not mean the Servlet async context, but an implementation of HTTP client which supports Futures or Callbacks, you won't get any benefit from using asynchronous http client. 假设异步客户端不是Servlet异步上下文,而是支持Futures或Callbacks的HTTP客户端实现,那么使用异步http客户端将不会带来任何好处。

As you'll still have to wait on the Future returned by the HTTP client in the container thread responsible for request processing. 因为您仍然必须等待HTTP客户端在负责请求处理的容器线程中返回的Future。 However if you choose to use callback, I'm not sure how that callback will relaunch a container thread to return the response. 但是,如果您选择使用回调,我不确定该回调如何重新启动容器线程以返回响应。 It seems that correct way to do it with callback will be by using Servlet 3 async capability. 似乎使用回调的正确方法将是使用Servlet 3异步功能。

By using the Servlet async you'll be able to save time on the container thread and would be able to process more rquests in the same amount of time. 通过使用Servlet异步,您将能够节省容器线程上的时间,并且能够在相同的时间内处理更多的rquest。 Also in this case there will be no need to use a async HTTP client as HTTP API call is already in Servlet async thread. 同样在这种情况下,由于Servlet异步线程中已经存在HTTP API调用,因此无需使用异步HTTP客户端。

It's unlikely that you'll gain throughput if the response (HTTP API call) gets back in 5ms. 如果响应(HTTP API调用)在5毫秒内恢复,则不太可能获得吞吐量。 That said I haven't done any bench marking of my own for this and it would be interesting to see the results of such bench marking. 就是说,我还没有为此做任何基准测试,看到这种基准测试的结果会很有趣。

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

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