Simon
Simon

Reputation: 65

HttpClient in Micronaut doesn't reuse thread

I've found that my Java Micronaut application, which essentially makes a blocking HTTP-call, handles low load well. But if the amount of requests increase, the application falls in a patter that looks somewhat like this JMX analysis

The ramp up in Memory is directly related to the amount of threads. The amount of active threads stays roughly the same while the amount of parked threads increases until GC.

Upvotes: 0

Views: 29

Answers (1)

Simon
Simon

Reputation: 65

Imho the problem is that if the standard amount of threads assigned to the HttpClient is not enough, additional threads are created. But as soon as they finish, they don't get reused in order to scale back to the set amount of threads.

So the initial solution for dealing with this issue was creating a dedicated thread-pool for the HttpClient and specifying it in application.yml.

micronaut:
  http:
    client:
      event-loop-group: other

This definitely works, but has some overhead at startup and requires maintenance if the load changes.

From Java 21 and ongoing, VirtualThreads are activated per default. So annotating with @ExecuteOn(TaskExecutors.BLOCKING) is already enough to switch the blocking operation to a virtual thread. For this to work you need to use the netty http client and not the one from the jdk (see section 4.1) because only the Netty one is part of the (n)io-event-loop.

Upvotes: 0

Related Questions