1

I read about ratpack and my-first-ratpack-app-what-i-learned states:

The threadpool is sized to the number of processor cores available in the system. This will typically mean a much smaller threadpool than with Spring Boot and Tomcat, for example. When a request comes in, a compute thread handles it. If blocking IO needs to be performed, that work is passed off to an asynchronous blocking thread and the compute thread goes back to handling requests. When the blocking thread is ready, a compute thread picks execution back up from the blocking thread.

let's take sample code from Ratpack-and-Spring-Boot

...

  // construct a "promise" for the requested user object. This will
  // be subscribed to within the respective handlers, according to what
  // they must do. The promise uses the "blocking" fixture to ensure
  // the DB call doesn't take place on a "request taking" thread.
  Promise<User> userPromise = ctx.blocking(() -> userRepository.findByUsername(username));
  ctx.byMethod(method -> method
    .get(() ->
      // the .then() block will "subscribe" to the result, allowing
      // us to send the user domain object back to the client
      userPromise.then(user -> sendUser(ctx, user))
    )

...

with repository defined as below:

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

  User findByUsername(String username); (1)
}

Let's assume, that

  • userRepository.findByUsername(username) is slow and takes 10s to execute, it uses synchronous blocking io
  • we have 10 concurrent clients, each client sends a new request every 5 seconds
  • client expects to receive response after max 12 seconds (10s takes database, 2s should be over enough for other overhead)

so

  • we have 2 requests/second - really not so much
  • but client sends a request 2 times more often than its take to service it
  • as we see repository is not asynchronous, we could even assume it's not spring data repo, but our custom repo using synchronous blocking java io

My questions are:

  • I assume, that if it were tomcat, it would kneel down in a finite time, regardless of thread pool size, in a finite time, please correct me if I'm wrong
  • Will ratpack withstand such a load for an infinite time?
  • If yes, what java solutions and mechanisms does it use to do it???
  • That is how does that mysterious blocking thread work? How can it be implemented??

It seems to me impossible to achieve. If we were using async io, than ok we can do it with one thread alone, but without async io not. And our thread poll, no matter how big, will finally deplete.

Michael
  • 41,989
  • 11
  • 82
  • 128
bastiat
  • 1,799
  • 2
  • 19
  • 38
  • Isn't it intuitive that no implementation can last infinitely under such conditions? The requests are served slower than they are issued. Even if you consider nothing else, after "infinite time" you will have an infinite number of active requests. Do you think any web server, regardless of implementation, can keep track of an infinite number of active requests? – Michael Jul 16 '20 at 16:17
  • Michael, you are right. The aim of my story was to create much more parallel requests than it is possible to create so many threads, and that is what I was thinking about :( So much writing in vain :( Not only web server will fail, also open ports will depleat - each client can open max 64k. – bastiat Jul 17 '20 at 10:29
  • @bastiat - Sorry for the stupid question, but why aren't you cooking the promise inside the `get` block? Seems you're creating lots of promises. – Tim Baverstock Aug 04 '23 at 11:44

0 Answers0