Coroutines performance question

Hi,
I’ve been searching about this but I couldn’t find an answer.

We are a travel website (so we use kotlin in JVM) and we’been using kotlin for a few months and so far we like it. We sell hotels, flights, activities, etc. and we have several external providers for each type of service and we communicate with them via a soap or rest webservice (we use cxf for that).

So every time a user searches for a trip (lets say a weekend in paris) we open several threads searching across different hotel and flight providers. Typically we open around 30 threads for every search. Web service calls are usually slow because of providers (between 5 and 20 seconds).

So my questions are:

-I suspect that we can have a big performance benefit switching to coroutines as they are lightweight threads. Am I right? (I am not talking about the syntax here)

-Can we benefit using CXF as is or do we have to switch a “suspend friendly” webservices library to fully benefit?

Many thanks in advance

Yes, in general I would say coroutines are a good choice. As you said your threads doing nothing for several seconds. Using coroutines you could dispatch the work to a thread pool with only a handful of threads. This also prevents that too many threads are started if you have too many concurrent user requests.

I’m not familiar with CXF but you have to make sure that your network calls are asynchronous, e.g. they are using Futures, callbacks or similar. If your network calls block they will also block the coroutine which means you can not even perform your 30 requests per user in parallel (because the thread pool usually doesn’t has that many threads).

No,
coroutines are syntax sugar to write callback style code, so it is not strictly correlate to thread execution model.

Take a look here

You could obviously choose another library, or try

https://docs.oracle.com/javase/9/docs/api/jdk/incubator/http/HttpClient.html#sendAsync-jdk.incubator.http.HttpRequest-jdk.incubator.http.HttpResponse.BodyHandler-

You should gain the main benefit using a good library to reduce the thread number, anyway using the coroutines with an asynchronous library will be a great enhancement for legibility.

1 Like

Perhaps invest in Ktor