11

I'm evaluating AsyncHttpClient for big loads (~1M HTTP requests). For each request I would like to invoke a callback using the AsyncCompletionHandler which will just insert the result into a blocking queue

My question is: if I'm sending asynchronous requests in a tight loop, how many threads will the AsyncHttpClient use? (I know you can set the max but apparently you take a risk of losing requests, I've seen it here)

I'm currently using the Netty implementation with these versions:

  • async-http-client v1.9.33
  • netty v3.10.5.Final

I don't mind using other versions if there are any optimization in later versions

EDIT:

I read that Netty uses the reactor pattern for reacting to HTTP responses which means it allocates a very small number of threads to act as selectors. This also means that the number of allocated threads doesn't increase with high requests volume. However that contradicts the need to set the max number of connections.

Can anyone explain what I'm missing?

Thanks in advance

Community
  • 1
  • 1
Gideon
  • 2,211
  • 5
  • 29
  • 47
  • Hint: HTTP requests don't necessarily reuse the same connection ;) – Eran Harel Feb 19 '16 at 13:48
  • @EranHarel not sure I understood the hint but now I'm thinking max connections just means number of open connections which does not directly affect the number of threads (since one selector listens to several sockets), am I in the right path? – Gideon Feb 19 '16 at 14:53
  • When you don't use HTTP keep-alive the connection isn't being reused, so when you send many requests to a specific host or domain - you will create many connections (one per request). In some cases you can overload a site / service, or get blocked when you flood. This is why clients have this setting. Regardless, netty will not increase the number of threads no matter how many concurrent requests / connections you create. – Eran Harel Feb 19 '16 at 19:21
  • This definitely organizes things better in my head. Write it as an answer and I'll accept it, thanks for the help – Gideon Feb 19 '16 at 21:19

2 Answers2

11

The AsyncHttpClient client (and other non-blocking IO clients for the matter), don't need to allocate a thread per request, and the client need not resize its thread pool even if you bombard it with requests. You do initiate many connections if you don't use HTTP keep-alive, or call multiple hosts, but it can all be handled by a single threaded client (there may be more than one IO thread, depending on the implementation).

However, it's always a good idea to limit the max requests per host, and max requests per domain, to avoid overloading a service on a specific host, or a site, and avoid getting blocked. This is why HTTP clients add a maxConnectionsPerXxx setting.

Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
Eran Harel
  • 2,325
  • 19
  • 28
5

AHC has two types of threads:

  1. For I/O operation. On your screen, it's AsyncHttpClient-x-x threads. AHC creates 2*core_number of those.
  2. For timeouts. On your screen, it's AsyncHttpClient-timer-1-1 thread. Should be only one.

And as you mentioned:

maxConnections just means number of open connections which does not directly affect the number of threads

Source: issue on GitHub: https://github.com/AsyncHttpClient/async-http-client/issues/1658

Mike Mike
  • 598
  • 7
  • 15