joesan
joesan

Reputation: 15385

Scala ExecutionContext for REST API Calls

In my application which is a HTTP service that exposes several API's that can be consumed by other services, I have a situation in which I have to call 2 different external services which would be a messaging service and another REST service.

I understand that for these I/O bound operations, it is a good practice to use a separate thread pool or ExecutionContext. I'm using the following to create a configuration for the custom ExecutionContext in my application.conf:

execution-context {
  fork-join-executor {
    parallelism-max = 10
  }
}

I have a couple of questions:

  1. Is this going to create 10 dedicated threads?
  2. How do I know the size of the parallelism-max?
  3. Say if I'm going to use this execution context to make REST API calls, how should I size this?

Upvotes: 2

Views: 394

Answers (1)

Aivean
Aivean

Reputation: 10882

  1. Is this going to create 10 dedicated threads?

    Close, but not exactly. As you can read from Akka documentation, three properties, parallelism-min, parallelism-factor and parallelism-max are used to calculate parallelism parameter that is then supplied to underlying ForkJoinPool. The formula is parallelism = clamp(parallelism-min, ceil(available processors * factor), parallelism-max).

    Now about parallelism. As you can read from the docs, it roughly corresponds to the number of "hot" threads, but under some circumstances additional threads might be spawned. Namely, when some threads are blocked inside ManagedBlocking. Read that answer for additional details.

  2. How do I know the size of the parallelism-max

    It depends on your use case. If you block one thread per task, how many simultaneous task executions do you expect?

  3. Say if I'm going to use this execution context to make REST API calls, how should I size this?

    Again, how many simultaneous requests you want to make? If you are going to block your threads, and you expect to have large number of simultaneous http calls, and you want them to be processed as soon as possible, you want large thread pool.

    However, if you application makes that many http requests, why not use existent library. Libraries like ApacheHttpClient allow you to configure parallelism in terms of http connections or connection per host.

    Also for making http calls from actors, it's natural to use non-blocking http clients, like netty-based AsyncHttpClient. It also has thread pool inside (obviously), but it is fixed and any number of simultaneous connections are handled by this fixed amount of threads in non-blocking way.

Upvotes: 1

Related Questions