How requests are processed in Rails?

·

2 min read

While working on my project adopting Ruby on Rails for the backend server, I had to know this.

Question:

By default, Rails with puma create 2 processes with 5 threads. Here is a question; Let's say processing a certain request takes 20 seconds. If the server receives the requests 20 times almost simultaneously, how many requests are processed? and What would happen for those that are not run?

Answer:

To answer this question, we need to understand how Puma work. Puma is a concurrent web server for Ruby applications. It is designed to be fast and scalable by utilizing multiple processes and threads. By default, Puma creates two processes, each with five threads. This means that Puma can handle up to 10 concurrent requests at a time. When a request comes in, Puma assigns it to an available thread in one of the processes. If all threads in both processes are busy, the request will be rejected.

Let's consider the scenario where 20 requests come in almost simultaneously, and each request takes 20 seconds to process. Since Puma can handle up to 10 concurrent requests at a time, the first 10 requests will be processed immediately, and the remaining 10 requests will be queued. Puma can handle more requests than the number of threads it has, as long as the queue does not become full, but the queue is not big, so you are likely to receive an error.

Now, let's consider what would happen if there were more than 20 requests coming in almost simultaneously. If the number of requests exceeds the maximum number of concurrent requests that Puma can handle, some of the requests will be rejected or timed out. This is because Puma has a limited capacity and cannot handle an infinite number of requests. In such cases, the clients sending the requests will receive an error message indicating that the server is unable to process their request.

In conclusion, the number of processes and threads in Puma affects how many concurrent requests can be processed at a time. If the number of requests exceeds the maximum capacity of Puma, some requests will be rejected or delayed until a thread becomes available.